Selenium with SpecFlow+SpecRun
SpecFlow+ Runner is a dedicated test runner for SpecFlow and integrates directly with Visual Studio.
In this article we will create Selenium script and execute as SpecRun. We will first configure the SpecFlow+SpecRun in Visual Studio and then execute the script.
Step-1: Install Visual Studio Extensions for Nunit and SpecFlow
- Install “Nunit 3 Test Adapter” and “SpecFlow for Visual Studio 2019” plugins.
2. Restart the visual studio to complete the installation
Step-2: Create Class Library project
- Launch Visual Studio
- Click on “Create a new project” option.
3. Select the project type “Class Library (.Net Framework)”
4. Click on Next button. Enter the Project Name and other details –
5. Click on Create button. Project gets created and shows below screen –
6. Delete the default create class “Class1”.
Step-3: Install NuGet packages for Selenium, SpecFlow and SpecRun
- Right click on project under Solution Explorer and select “Manage NuGet Packages..”
2. Search and install following packages in the same order as mentioned below –
Selenium.WebDriver.3.141.0
Selenium.Support.3.141.0
NUnit.3.9.0
SpecFlow.2.3.2
SpecFlow.Tools.MsBuild.Generation.2.3.2
SpecRun.Runner.1.7.1
SpecRun.SpecFlow.1.7.1
Microsoft.TestPlatform.TestHost.15.0.0
Microsoft.NET.Test.Sdk.15.0.0
3. Now the Solution looks like this –
4. Now the “Packages” folder looks like this –
5. Details of “App.config” Note that the “unitTestProvider”.
Step-4: Edit & update the project for Feature File, Step Definition and Page Methods
- Right click on the project and select Add->New Folder. Create below folders -
2. Right click on the folder name “Features” and select Add->New Item…
3. Select “SpecFlow” from left hand side and click on “SpecFlow Feature File”. Enter the name of feature file is “LoginFeature.feature”.
4. Update the feature file content as below –
5. Right click on the folder name “PageMethods” and select Add->Class…Create the class name as “LoginPage.cs”
6. Update the class name “LoginPage.cs” as below –
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeleniumSpecRun.PageMethods
{
class LoginPage
{
private IWebDriver driver;
String toggle_menu = “//a[@id=’menu-toggle’]/i”;
String loginMenu = “//a[contains(text(),’Login’)]”;
String userID = “//input[@id=’txt-username’]”;
String password = “//input[@id=’txt-password’]”;
String loginBtn = “//button[@id=’btn-login’]”;
public LoginPage(IWebDriver driver)
{
this.driver = driver;
}
public void goToPage()
{
driver.Navigate().GoToUrl(“http://demoaut.katalon.com/");
}
public void goToToggleMenu()
{
driver.FindElement(By.XPath(toggle_menu)).Click();
}
public void goToLoginMenu()
{
driver.FindElement(By.XPath(loginMenu)).Click();
}
public void enterUserName(string text)
{
driver.FindElement(By.XPath(userID)).SendKeys(text);
}
public void enterPassword(string text)
{
driver.FindElement(By.XPath(password)).SendKeys(text);
}
public void clickLoginBtn()
{
driver.FindElement(By.XPath(loginBtn)).Click();
}
public void closeBrowser()
{
driver.Quit();
}
}
}
7. Now it’s time to generate step definition. Right click on the folder name “StepDefs” and select Add->Class…Create the class name as “LoginStepDef.cs”
8. Update the class name “LoginStepDef.cs” as below –
using NUnit.Framework;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
using SeleniumSpecRun.PageMethods;
using OpenQA.Selenium.Chrome;
namespace SeleniumSpecRun.StepDefs
{
[Binding]
public class LoginStepDef
{
IWebDriver driver;
LoginPage loginPage;
[Given(@”Launch browser and navigate url”)]
public void GivenLaunchBrowserAndNavigateUrl()
{
ChromeDriverService service = ChromeDriverService.CreateDefaultService(“webdriver.chrome.driver”, @”D:\\Automation\\WebDrivers\\chromedriver.exe”);
driver = new ChromeDriver(service);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
driver.Manage().Window.Maximize();
loginPage = new LoginPage(driver);
loginPage.goToPage();
}
[When(@”Enter username and password details”)]
public void WhenEnterUsernameAndPasswordDetails()
{
loginPage.goToLoginMenu();
loginPage.goToLoginMenu();
loginPage.enterUserName(“John Doe”);
loginPage.enterPassword(“ThisIsNotAPassword”);
}
[Then(@”click on Sign In button”)]
public void ThenClickOnSignInButton()
{
loginPage.clickLoginBtn();
loginPage.closeBrowser();
}
}
}
9. Now Project Explorer looks like this –
10. Now edit the project (SeleniumSpecRun.csproj)and update as below (Open in Notepad++)–
<ItemGroup>
<Compile Include=”Features\LoginFeature.feature.cs”><AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>LoginFeature.feature</DependentUpon>
</Compile>
<Compile Include=”PageMethods\LoginPage.cs” />
<Compile Include=”StepDefs\LoginStepDef.cs” />
<Compile Include=”Properties\AssemblyInfo.cs” />
</ItemGroup>
<ItemGroup>
<None Include=”App.config” />
<None Include=”Default.srprofile”>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include=”Features\LoginFeature.feature”>
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>LoginFeature.feature.cs</LastGenOutput>
</None>
<None Include=”packages.config” />
<None Include=”runtests.cmd” />
<None Include=”SpecRunTestProfile.xsd”>
<SubType>Designer</SubType>
</None>
<None Include=”SpecRunTestProfile_2011_09.xsd”>
<SubType>Designer</SubType>
</None>
</ItemGroup>
11. Now Script creation completed.
Step-5: Build and execute the project as SpecRun
- Click on the Build->Build Solution
2. Build will happen successfully and shows below message –
3. Now open the Test Explorer. From Menu click on Test->Windows->Test Explorer
4. Test Explorer shows below screen –
5. Right click on the scenario and select “Run Selected Test”
6. Script will execute successfully.
Step-6: SpecRun Report
- Report will generate under “TestResults” in the project directory
- Open the html report and its look like this –
You can find the complete code at location — SeleniumSpecRun