DEV Community

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

Posted on • Edited on

2 1

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay