TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Selenium

Selenium Automation: Page Objects

In this post, we will discuss about Page Objects .The most important aspect while authoring real time web application tests using Selenium Webdriver.

What is a Page object?

A page object is an object that we use as a representation of an HTML element to abstract them in our Selenium tests. So, our Selenium tests will not interact with HTML code directly, but with page objects that use selectors to refer to the HTML code.We may create independent classes for declaring our page objects. The class should group together all the page objects that belong to the same page. The tests and page objects should be independent one of the other.

Let us build a sample PageObject for a Login page below:

 

package com.testingdocs.testng.sample;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {

 private final WebDriver driver;
 private By username = By.id("userID");
 private By password = By.id("pwd");
 private By loginSubmit = By.id("submit");

 public LoginPage(WebDriver driver)
 {
 this.driver = driver;

 }

 public LoginPage typeUserID(String userID) {
 driver.findElement(username).sendKeys(userID);
 return this; 
 }

 public LoginPage typePassword(String pass) {
 driver.findElement(password).sendKeys(pass);
 return this; 
 }

 public void submitLogin()
 {
 driver.findElement(loginSubmit).submit();
 }

 public void login(String userID, String password) {
 typeUserID(userID);
 typePassword(password);
 
 }
}

 

Now a sample test which instantiates the page object with out the knowledge of html elements and the locating mechanism of the login elements.

package com.testingdocs.testng.sample;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class SampleLoginTest
{
 WebDriver driver = new FirefoxDriver();
 LoginPage lpage = new LoginPage(driver) ;
 
 @Test
 public void logintest()
 {
 driver.get("Sample_Login_Page_URL"); 
 lpage.login("1001", "mypassword1234$");
 .....
 .....
 }
}

 

Advantages of Page object

Firstly, we can take advantage of object oriented approach like abstraction , encapsulation etc by using page objects.

Shield tests against application HTML code changes. Changes will only affect the page object, and  not our selenium tests
Using page object we will avoiding redundant code.
We can have page object in only one place and refer them in our tests.

Page Object FindBy Annotation

Learning PageFactory

PageObject may need not represent an entire page. It may represent a section that appears many times within a site or page. The motto is that there is only one place in your entire test suite with knowledge of the structure of the HTML of a particular page or part of it.In conclusion, using page object we can write robust web tests without worrying about the HTML web page changes.

 

Selenium WebDriver Tutorials on this website can be found at:

https://www.testingdocs.com/selenium-webdriver-tutorial

For more details on the Selenium, visit the official website at:

https://www.selenium.dev/

Related Posts

Windows 10 Settings

Selenium /

Add Microsoft Webdriver on Windows OS

Download Selenium Components

Selenium /

Getting Started with Selenium Webdriver

LambdaTest Testing Cloud SaaS Platform

Selenium /

LambdaTest – Testing Cloud SaaS Platform

Selenium /

Selenium 3.0 and Mozilla GeckoDriver

SauceLabs Website

Selenium /

Run an Example Test on SauceLabs

‹ Selenium FluentWait Example› WebDriver Actions in Selenium

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version