Selenium Test Automation: How to build Page Object Model (POM) base class?

Selenium

BriefTitle: 
POM base

eclipse-selenium-page-design

Step 1: Create base package

  1. Right click on src/main/java folder, select New > Package
  2. Enter package name as com.examplesite.base
  3. Click Finish

Step 2: Create base class

  1. Right click on package com.examplesite.base, select New > Class
  2. Enter class name as BasePage
  3. Click Finish

Step 3: Write common methods in base class

All POM classes will extend to this BasePage class.

Open the BasePage class you just create. Add the below code there:

package com.examplesite.base;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

public class BasePage<T> {

protected WebDriver driver;
protected WebDriverWait Wait;

//constructor
protected BasePage(WebDriver driver){

this.driver = driver;
Wait = new WebDriverWait(driver,40);
}

@SuppressWarnings("unchecked")
protected T launchPage(String URL){
driver.get(URL);
return(T) this;
}

protected void type(String text, By element){
find(element).sendKeys(text);
}

protected void click(By element){
find(element).click();
}

private WebElement find(By element) {
return driver.findElement(element);
}

public String ReadTitle(){
return driver.getTitle();
}

protected String ReadText(By element){
return find(element).getText();
}

}

eclipse-selenium-basepage-class

Sorting: 
11

Comments

Thank you. It helped.

Add new comment

Selenium