DEV Community

Cover image for Selenium - Design a Framework: Page Object Pattern
Anne Quinkenstein
Anne Quinkenstein

Posted on • Edited on

Selenium - Design a Framework: Page Object Pattern

Which Framework to use?

How to write scripts, store objects in a effective way?:

Frameworks:
data-driven, keyword-driven & Page Object Pattern
Why POP?

  • easy to maintain
  • easy readability of scripts
  • reduce of eliminate duplicacy
  • re-usability of code
  • reliability

Basics of Page Object Pattern

PageObjects Class

Create Class for each Page:
Homepage - all Objects belonging to homepage
Login - JavaClass(LoginPage)
to identify the objects on one page

public class MaerkischeScholle {

WebDriver driver; 
    //local driver 

public MaerkischeScholle(WebDriver driver) {
         this.driver = driver;
         //driver from where parameter is passed into constructor, is passed to local driver 
    }    

By textFlatAvailability = By.xpath("//*[@id='article-27']"); 

public WebElement TextFlat() {
   return driver.findElement(textFlatAvailability); 
       }
}
Enter fullscreen mode Exit fullscreen mode

Testcase Class

Create a Class for Testcases

eg. Login
call Login-Field from Loginpage-Class
than you can act on the Login-Field
or get a Text from a Page and print it

package testcases;

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

import objectrepository.MaerkischeScholle;

public class CheckAvailability {

    @Test
    public void CheckText() {

        System.setProperty("webdriver.gecko.driver", "//home//helloworld//Documents//Code//Drivers" + 
                "//geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.maerkische-scholle.de");

        MaerkischeScholle ms = new MaerkischeScholle(driver); 

        String text = ms.TextFlat().getText();

        System.out.println(text); 

        }

}
Enter fullscreen mode Exit fullscreen mode

Driver

The Webdriver is initiated in the Testcase Class

 System.setProperty("webdriver.gecko.driver", "//home//helloworld//Documents//Code//Drivers" + 
                "//geckodriver");
 WebDriver driver = new FirefoxDriver();
Enter fullscreen mode Exit fullscreen mode

Testcases Class (parameter for new object of PageObjectClass)

MaerkischeScholle ms = new MaerkischeScholle(driver); 
Enter fullscreen mode Exit fullscreen mode

and then passed to the PageObjects-Class as a parameter through the constructor.
-> PageObjectsclass

WebDriver driver; 
    //local driver 

     public MaerkischeScholle(WebDriver driver) {
         this.driver = driver;
         //driver from where parameter is passed into constructor, is passed to local driver 
                PageFactory.initElements(driver, this); 
                 // to use the @FindBy Annotation
    }   
Enter fullscreen mode Exit fullscreen mode

if you switch to another page, just create a new PageObjekt in the Testcases Class and pass the driver

 HomePage hp = new HomePage(driver); 
Enter fullscreen mode Exit fullscreen mode

PageObjectFactory @FindBy

@FindBy(xpath= "//*[@id='article-27']")
WebElement textFlatAvailability; 

public WebElement TextFlat() {
   return textFlatAvailability; 
  }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)