DevOps | Software Automation | Continuous Integration

Tag: SpecFlow

Automating The Running Of SpecFlow Tests

1. Create a bat file with the following code to kick off a test suite named “Regression” using NUnit:

nunit-console  Regression.dll

2. Add in the following code to use SpecFlow to generate the output in HTML format

specflow.exe nunitexecutionreport Regression.csproj /out:TestResult.html

3. Add the bat file to run as part of the Task Scheduler (Windows 7)

Go to Start->All Programs->Accessories->System Tools->Task Scheduler

4. At the right side, under Actions click on Create Task

5. Under General tab, give a name for the task and select options such as “Run only when user is logged on”

6. Under Triggers tab, click on New, and select the schedule you want the task to run. For example, Daily at 7am.

7. Under Actions, click on New and browse for the bat file

SpecFlow + C# – Read a row from database to check the value

 

 [Then(@”we verify that the (.*) row contains (.*) of (.*)”)]
        public void ThenWeVerifyThatTheFirstRowContainsLInstIDOf410(String row, String field, String value)
        {
            SqlConnection ThisConnection = new SqlConnection(@”Server=1.1.0.26;Database=pp5_410_LK;Trusted_Connection=True”);
            ThisConnection.Open();
            SqlCommand thisCommand = ThisConnection.CreateCommand();
            if (String.Compare(row,”first”)==0)
            {
                thisCommand.CommandText = “select * from CostCentre where sCostCentreNo in (select MAX(sCostCentreNo) from CostCentre where lInstID=410) and lInstID=410”;
            }
            if (String.Compare(row, “last”) == 0)
            {
                thisCommand.CommandText = “select * from CostCentre where sCostCentreNo in (select MIN(sCostCentreNo) from CostCentre where lInstID=410) and lInstID=410”;
            }
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            try
            {
                while (thisReader.Read())
                {
                    Console.WriteLine(thisReader[field].ToString());
                    Assert.AreEqual(value, thisReader[field].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            thisReader.Close();
            ThisConnection.Close();
        }

SpecFlow + C# – Execute a script and how to make step definitions reusable

//This is how we can make the step definitions reusable

[When(@”I run the (.*)”)]
            public void WhenIRunTheGenericImporter(String importer)
            {
               if (String.Compare(importer,”GenericImporter”)==0)
               {
                   StartInfo.FileName = @”C:\TrunkbinDebugWinExecutablesGenericImporter.exe”;
               }
               if (String.Compare(importer,”PageUpImporter”)==0)
               {
                   StartInfo.FileName = @”C:\TrunkbinDebugWinExecutablesPageUpImporter.exe”;
//Passing in argument to the command
                   StartInfo.Arguments = @”lk 410″;
               }

               ProcessStartInfo StartInfo = new ProcessStartInfo();

               Process Process = new Process();
               StartInfo.RedirectStandardOutput = true;
               StartInfo.RedirectStandardError = true;
               StartInfo.UseShellExecute = false;
               StartInfo.CreateNoWindow = true;
               Process.StartInfo = StartInfo;
               Process.Start();
            }

SpecFlow + C# – Connect to a SQL database

 

 [Given(@”we clean the database”)]
        public void GivenWeCleanTheDatabase()
        {
           SqlConnection ThisConnection = new SqlConnection(@”Server=1.10.26;Database=410_LK;Trusted_Connection=True”);
            ThisConnection.Open();
            SqlCommand thisCommand = ThisConnection.CreateCommand();
            thisCommand.CommandText = “delete from costcentre where linstid = 410”;
            SqlDataReader thisReader = thisCommand.ExecuteReader();
            thisReader.Close();
            ThisConnection.Close();
        }

Setting Up BDD Testing Framework For .NET Applications Using SpecFlow, NUnit And Selenium

1. Download and install the following files:

SpecFlow.msi

NuGet

NUnit.msi

Selenium webdriver for C#.zip

Resharper

2. Open up Visual Stdio 2010 and using NuGet, add reference for SpecFlow and NUnit.

3. To add reference for Selenium, you will need to right click->Add Reference-> browse to the Selenium folder and add all DLL files.

4. Include the following configuration for SpecFlow into App.config under the SpecFlow tag

unitTestProvider name=”NUnit”

5. Resharper is needed for integration of NUnit and Ms Visual Studio.

* Resharper 6 has problem integrating with Gallio (MbUnit) test

5. Right click on the project name->Add->New Item, you will see SpecFlow feature file

6. Use Class Library to create a normal Selenium project

7. To get Selenium working in IE, add the code below:

using OpenQA.Selenium.IE; (in library)

IWebDriver driver = new InternetExplorerDriver();

* Window switching does not work properly on IE8

* Have to set Tools->Internet Options->Security->Protected Mode to be the same for all zone for IE7 or higher on Windows Vista or Windows 7

© 2023 Chuan Chuan Law

Theme by Anders NorenUp ↑