Automation In Selenium Using Page Object Model And Page Factory
Posted By : Aarushi Sharma | 13-Mar-2018
POM framework has become more popular automation framework and many people are using it because of it's code maintainability and reusability
POM is used to create Object Repository for the web elements.There is a class created for each web page.This class will find the elements on the page and also define what actions(click, send keys etc) has to be performed on the elements.
In Page Object Model, web pages are represented as classes and page elements are declared as variables. All user interactions are implemented as methods.Name of the method should be given as per task.For example, if you want to click on the sign-in button then the name should be.
for eg->click SignInButton();
enterCredentials(username,password);
Page Factory is used to support Page Object Model. Page Factory is used to initialize the web elements that are created in classes and Page Objects. To initialize the web elements,"initelements" function is used.
eg-> Signin page=new Signinpage(driver);
PageFactory.initelements(driver,page);
In web page class
eg-> public SigninPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
WebElement can be defined as:
@FindBy(name="username")
private WebElement user_name;
Example of Page Object Model:
In this class all locators are defined:
public abstract class TestBase {
public static WebDriver driver = LoadDriver.driver;
@FindBy(xpath="//input[@id='username']")
public WebElement enterUsername;
@FindBy(xpath="//input[@type='password']")
public WebElement enterPassword;
@FindBy(xpath="//button[@class='login-btn']")
public WebElement clickLogin;
public TestBase(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
public abstract void login(String Username,String Password);
}
In this class all actions on the elements are described:
public class TestImplement extends TestBase {
public static WebDriver driver = LoadDriver.driver;
public TestImplement(WebDriver driver) {
super(driver);
}
@Override
public void login(String Username, String Password) {
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
enterUsername.sendKeys(Username);
enterPassword.sendKeys(Password);
clickLogin.click();
}
Now the final login class:
public class Login extends EnvProperties{
public static WebDriver driver = LoadDriver.driver;
TestBase testbase;
@BeforeTest
public void launchbrowser() throws IOException {
driver = LoadDriver.call("URL");
}
@Test(priority=0)
public void login() throws InterruptedException, IOException {
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
testbase=new TestbaseImplement(driver);
testbase.login(getProperties().getProperty("username"), getProperties().getProperty("Password"));
}
Advantages of POM:
1.Optimized code and also code can be reused.
2.Object repository can be used with different tools for a different purpose.Eg->It can be used with TestNg or Junit for functional testing and with Cucumber for acceptance testing.
3.Test code and page specific code is separate because of this code become more maintainable.The code should be changed in object class if any u.i change is occurring.
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aarushi Sharma
Aarushi is a bright QA engineer with experience in manual testing. Apart from that she loves to dance.