Selenium Test Automation: How to build Page Object Model (POM) page classes?

Selenium

BriefTitle: 
POM pages

eclipse-selenium-page-design

Step 1: Create Package

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

Step 2: Create HomePage POM class

2.1: Create class for home page

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

2.2: Write methods for home page

Copy the below code to HomePage.java file.

package com.examplesite.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.examplesite.base.BasePage;

public class HomePage extends BasePage<HomePage> {

public static final By LogInLink = By.name("_PortalLogin_WAR_enablePortal_username");

public HomePage(WebDriver driver) {
super(driver);
}

public LogInPage clickLogIn() {
click(LogInLink);
return new LogInPage(driver);
}

}

Step 3: Create LoginPage POM class

3.1: Create class for login page

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

3.2: Write methods for login page

Copy the below code to LoginPage.java file.

package com.examplesite.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import com.examplesite.base.BasePage;

public class LogInPage extends BasePage<LogInPage> {

// example
public static final By UserNameTextField = By.name("_PortalLogin_WAR_enablePortal_username");
public static final By PasswordTextField = By.name("_PortalLogin_WAR_enablePortal_username");
public static final By LogInButton = By.name("_PortalLogin_WAR_enablePortal_username");

// Enter user name in the User Name field
public void enterUserName(String userName) {
type(userName, UserNameTextField);
System.out.println("Entered user name.");
}

// Enter password in the Password field
public void enterPassword(String userName) {
type(userName, PasswordTextField);
System.out.println("Entered user name.");
}

// Click LogIn button
public ProfilePage clickLogInButton() {
click(LogInButton);
System.out.println("Clicked Log In button");
return new ProfilePage(driver);
}
}

Step 4: Create ProfilePage POM class

4.1: Create class for profile page

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

3.2: Write methods for profile page

Copy the below code to ProfilePage.java file.

package com.examplesite.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import com.examplesite.base.BasePage;

public class ProfilePage extends BasePage<ProfilePage> {

// examplesite
private By ActualProfileName = By.xpath("//h1[@class='profile-contact-name']");
private By LogOutLink = By.xpath("//h1[@class='profile-contact-name']");

// examplesite
public ProfilePage(WebDriver driver) {
super(driver);
}

// examplesite
public boolean verifyIfCorrectProfileLoaded(String expectedprofilename) {
if (ReadText(ActualProfileName).contains(expectedprofilename)) {
System.out.println("Correct profile - " +  ReadText(ActualProfileName) + " - is displayed.");
return true;
} else {
System.out.println("Correct profile is not displayed. Profile name is displayed as - " + ReadText(ActualProfileName) + " -.");

return false;
}

}

// examplesite
public HomePage clickLogOut() {
click(LogOutLink);
System.out.println("Clicked log out.");
return new HomePage(driver);
}

}

src/main/java folder should look like the following:

selenium-eclipse-pom-classes

Sorting: 
12

Comments

Great job

Add new comment

Selenium