Prerequisites
Please complete Part 2 of this tutorial before continuing.
Creating Project Structure
Pages
Since we are using Page Object Model (POM) design pattern in our automation framework, we need to maintain elements and actions of each web page in separate class files. (i.e. If your web application contains 5 different pages, you need to create 5 class files which corresponds to each web page and include all elements and actions for each page)
1) In your solution, create a folder and rename it as Pages. Then create a GoogleHomePage.cs class inside Pages folder. GoogleHomePage.cs class will contain all elements and actions of the GoogleHome page.
2) Import the following libraries to the class.
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
The syntax to be used to define elements
[FindsBy(How = How.XPath, Using = "")]
private IWebElement searchTextBox;
3) Include elements for Search text box and Google Search button in GoogleHomePage.cs class
(For this example I will be using xPath to identify elements)
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
The syntax to be used to define elements
[FindsBy(How = How.XPath, Using = "")]
private IWebElement searchTextBox;
3) Include elements for Search text box and Google Search button in GoogleHomePage.cs class
(For this example I will be using xPath to identify elements)
4) Include actions required in GoogleHomePage.cs class.
(For this example I will be using adding text to search text box and clicking Google Search button)
5) Create a constructor to initialize elements to be identified by the driver.
(For this example I will be using adding text to search text box and clicking Google Search button)
5) Create a constructor to initialize elements to be identified by the driver.
Features
Feature file is a file which contains one or more scenarios for a particular feature of the system. Feature can be a direct map to a story in a project.
To make maintenance easy, we will create a Features folder in the project and keep all the feature files inside that folder.
1) In your solution, create a folder and rename it as Features. Then right click on the Features folder > Add New Item > SpecFlow > SpecFlow Feature File to add a new feature file. (i.e. I will add a feature file called 'GoogleSearch.feature')
2) The feature file contains generic text. You can edit the file according to your requirement. However the format must not be changed.
In the GoogleSearch.feature file I want to have the following scenario.
1. Launch Google Chrome browser
2. Navigate to google home page
3. Enter 'Google' in search textbox and click on Google Search button
4. View search results
I have modified GoogleSearch.feature file as given below.
Please note that one feature file can connect to step implementations on multiple step definition classes.
1) To implement step implementations for GoogleSearch.feature, I will add GoogleSearchSteps.cs class inside Steps folder.
2) SpecFlow provides the capability to generate step definitions for each step written in feature file. Navigate to GoogleSearch.feature file > Right Click on any step > Generate Step Definitions.
In the pop-up window, select Copy methods to clipboard option to copy step definitions.
3) Navigate back to GoogleSearchSteps.cs class and paste copied step definitions.
Use TechTalk.SpecFlow reference to resolve errors.
To bind the step definitions to feature file, include [Binding] on top of the class.
4) Navigate back to GoogleSearch.feature file to see if binding worked successfully. To navigate to step definition, click on step and press F12. You should be navigated to relevant step definition.
2) Add a IWebDriver static variable called driver. Then create a method as given in screenshot. In this project I will be covering executing a sample test on a chrome browser.
In order to launch chrome browser, chrome driver is needed. Download the chrome driver in following chrome driver link based on the chrome web browser version installed on the computer. Then add the folder path until chromedriver.exe in the launchBrowser method.
3) Add the following methods to SeleniumHelper.cs class.
//maximize browser window
public static void maximizeBrowser()
{
driver.Manage().Window.Maximize();
}
//navigate to given url
public static void navigateToUrl(string url)
{
driver.Navigate().GoToUrl(url);
}
//return current driver instance
public static IWebDriver getDriver()
{
return driver;
}
4) Next add a SpecFlow hooks class named Hooks.cs into Utilities folder. SpecFlow hooks class will contain specific tasks that need to be run on certain points when tests are running. (i.e. Execute a set of commands prior to test run)
5) Add an AfterTestRun event in Hooks.cs class and add the command to close the browser.
2) The feature file contains generic text. You can edit the file according to your requirement. However the format must not be changed.
In the GoogleSearch.feature file I want to have the following scenario.
1. Launch Google Chrome browser
2. Navigate to google home page
3. Enter 'Google' in search textbox and click on Google Search button
4. View search results
I have modified GoogleSearch.feature file as given below.
Steps
For every action written in feature file should have a corresponding implementation step to execute the required actions. These steps are included in a steps class. This class is called step definition class. Step definition class can have one or more step implementations.Please note that one feature file can connect to step implementations on multiple step definition classes.
1) To implement step implementations for GoogleSearch.feature, I will add GoogleSearchSteps.cs class inside Steps folder.
2) SpecFlow provides the capability to generate step definitions for each step written in feature file. Navigate to GoogleSearch.feature file > Right Click on any step > Generate Step Definitions.
In the pop-up window, select Copy methods to clipboard option to copy step definitions.
3) Navigate back to GoogleSearchSteps.cs class and paste copied step definitions.
Use TechTalk.SpecFlow reference to resolve errors.
To bind the step definitions to feature file, include [Binding] on top of the class.
4) Navigate back to GoogleSearch.feature file to see if binding worked successfully. To navigate to step definition, click on step and press F12. You should be navigated to relevant step definition.
Utilities
Add a folder called Utilities in order to keep classes which keeps supporting methods for the framework.
1) For starters in utilities folder, first add a class called SeleniumHelper.cs.
2) Add a IWebDriver static variable called driver. Then create a method as given in screenshot. In this project I will be covering executing a sample test on a chrome browser.
In order to launch chrome browser, chrome driver is needed. Download the chrome driver in following chrome driver link based on the chrome web browser version installed on the computer. Then add the folder path until chromedriver.exe in the launchBrowser method.
3) Add the following methods to SeleniumHelper.cs class.
//maximize browser window
public static void maximizeBrowser()
{
driver.Manage().Window.Maximize();
}
//navigate to given url
public static void navigateToUrl(string url)
{
driver.Navigate().GoToUrl(url);
}
//return current driver instance
public static IWebDriver getDriver()
{
return driver;
}
4) Next add a SpecFlow hooks class named Hooks.cs into Utilities folder. SpecFlow hooks class will contain specific tasks that need to be run on certain points when tests are running. (i.e. Execute a set of commands prior to test run)
5) Add an AfterTestRun event in Hooks.cs class and add the command to close the browser.
No comments:
Post a Comment