In the last few Selenium tutorials, we discussed about various commonly used and popular scenarios in Selenium webdriver.
In this section, we will discuss on Test Automation Framework in Selenium webdriver.
What is Framework?
- Automated testing frameworks provide test environment structure that is typically missing from underlying test tools.
- In a very simple language, we can say that a framework is a constructive blend of various unique rules, guidelines, protocols and procedures for test creating, organization and execution.
- These are just guidelines and not rules; they are not mandatory and you can still script without following the framework guidelines. But you will miss out on the advantages of having a Framework.
Advantage of Test Automation framework
- Reusability of code
- Maximum coverage
- Recovery scenario
- Low cost maintenance
- Minimal manual intervention
- Easy Reporting
Types of Test Automation Framework
- Data Driven Testing Framework
- Keyword Driven Testing Framework
- Hybrid Testing Framework
- Behavior Driven Development Framework
The detailed description documents of above frameworks are easily available on the internet, so I am not covering them here. In following section, we will discuss about Page Object Model (POM) and Page Factory which is one of the most widely used design patterns by the Selenium webdriver across the world. We will develop a simple test automation framework using POM and testNG.
Why we need Page object model?
- While writing a linear script, we are finding elements and filling values for those elements. At initial stage, the script maintenance loooks easy. But the time test suite will grow, more and more lines of code will be added to your script and it will become tough.
- The main problem with script maintenance is that if 100 different scripts are using the same page element, with any change in that element, you need to change all 100 scripts. This is very time consuming task.
- In order to overcome this problem, Page Object Model comes into the picture. It helps to make the code more readable, maintainable, and reusable.
What is Page Object Model?
- Page Object Model is a design pattern to create Object Repository for web UI elements.
- For each web page in the application, there should be corresponding page class.
- Page class will find the WebElements of that web page and it also contains page methods which perform operations on those WebElements.
- Name of these methods should be given as per the task they are performing, i.e., enterUserName() or enterPassword.
Advantages of Page Object Model
- Robust and increases code reusability.
- This helps to write more cleaner code and code is easy to understand.
- Easy to maintain as object repository is independent of test cases.
What is Page Factory?
- We use Page Factory pattern to initialize web elements which are defined in Page Objects.
- initElements() method from PageFactory Class is used to initialize web elements
- PageFactory.initElements() static method takes the driver instance of the given class and the class type, and returns a Page Object with its fields fully initialized.
public HomePage(WebDriver driver){
PageFactory.initElements(driver, this);
}
- Page Factory will initialize every web element variable with a reference to a corresponding element on the actual web page based on “locators” defined. This is done by using @FindBy annotations.
@FindBy(id = "username")
public WebElement userID;
- The page object pattern simply abstracts business logic away from the physical structure of the pages. The Page Factory class gives us the ability to use annotations which automatically find the elements on the page without specifying findElement.
How to implement?
We will consider the Facebook login page example to demonstrate on Page Object Model. It identifies the email id field and enters the email id, identifies the password field and enters password and then clicks on login button.
Rules for Creating a POM Class
- Number of Page Object Model class should be same as number of Webpages present in the application. Example: If the web application has 10 pages, we should create 10 Page Object Model Classes.
- The name of the class should be same as the title of the respective webpage and it should end with the word Page. Example: If the title of the my Webpage is Login, then we should create a POM class name as LoginPage.
Pre-requisite:
- Java Project should have been created. Selenium and testNG libraries should have configured with the Project
- Right click on the Java Project, Go to new option and select a Package option.
- Enter Package name as com.yourProjectName.pom. Eg: com.facebook.pom
- Right click on the package created in Step 3, Go to new option and select a class option
- Enter the class name as your webpage name. Say for example if your webpage name is Login, then your POM class name should be LoginPage.
How does the POM class look like?
All the web elements of the Application under Test and the methods that operate on these web elements are maintained POM in a class and function which call these methods are maintained in another class file called as Test Scripts / Test cases.
Following is the sample code from the FacebookLoginPage of Page Object model Class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class FacebookLogInPage { @FindBy(id = "email") public WebElement emailIDField; @FindBy(id = "pass") public WebElement passwordField; @FindBy(id = "u_0_q") public WebElement loginButton; public FacebookLogInPage(WebDriver driver) { PageFactory.initElements(driver, this); } public void enterUserID() { emailIDField.sendKeys("testuser@gmail.com"); } public void enterPassword() { passwordField.sendKeys("Password"); } public void clickOnLoginButton() { loginButton.click(); } } |
Please see the following image for description of above class.
How does Page Object Model works?
To make the POM class work we have to write a separate Test Class which will perform the actions on the methods inside POM class.
Consider the following code of Test Class which should be written under scripts package.
Consider the following code of Test Class which should be written under scripts package.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.seleniumTutorial.pom.Test11; public class TestFacebookLogin { public static FacebookLogInPage ObjFacebookLogInPage; public static WebDriver driver; @BeforeClass public void beforeClass() { // Creating the driver System.setProperty("webdriver.gecko.driver", "./gecodriver/geckodriver.exe"); WebDriver driver = new FirefoxDriver(); // Creating Page object ObjFacebookLogInPage = new FacebookLogInPage(driver); } @Test public void fLoginToFacebook() { // Calling page object methods ObjFacebookLogInPage.enterUserID(); ObjFacebookLogInPage.enterPassword(); ObjFacebookLogInPage.clickOnLoginButton(); } @AfterClass public void afterClass() { driver.close(); } } |
- When the constructor of Page Object model class is called, it initializes the web elements on the webpage using init elements method of Page Factory class and converts the code to selenium code. i.e private WebElement emailIDField= driver.findElement (By.id(“email");
- Similarly in our case, when the instance of FacebookLogInPage is created, it calls the constructor on the Page Object model class, and converts the code to Selenium code as shown above. But, converted code will not be executed. During runtime when the respected method is called, the converted selenium code gets executed and performs the action on the webpage.
- E.g: when the method enterEmail.enterEmailID() is called, control passes to enterEmailID() and enters the specified email id in the EmailID field on the webpage.
I have created a demo test automation framework using Selenium Page object model, Page factory with testNG. Please click on following download image to get the code.
<-- Previous
superb Vinod!! Great job.
ReplyDeleteVery good article!! Thanks a lot
ReplyDeleteThanks you so much sir,for your tutorial,and it is very nice and easy to understand!!!!
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete