Moez Hirani
Moez Hirani

Reputation: 141

Implementing the Page Object Model

I am building a test automation project using Perl and Selenium. I am using the Page Object Model. I am somewhat unsure about where the Selenium driver would fit into the implementation of the page object model.

Should each page object 'have' a driver ? The way I am thinking is that each page object represents a set of services that the page offers to a user. With this concept in mind , a page object does not have a 'has-a' relationship with a driver. A page object interacts with a driver. However, I am still looking for suggestions. Should I have the driver as part of each page object in the web-application?

Thanks!

Upvotes: 2

Views: 724

Answers (4)

Uchiha Suryajit
Uchiha Suryajit

Reputation: 147

As I understand, there are No set rules/standards for implementing POM.

However, the general thumb rule is to create a BaseTest and BasePage class in your Framework where each respective webpage (like Login) will represent by its PageClass(LoginPage). Similarly, all your Page classes will extend your BasePage and all tests will extend your BaseTest class.

Below is a rough idea of its implementation ->


public class BaseTest{

@BeforeSuite()
setupMethod(){
initialize your WebDriver here
}

}

------------------------------------------
public class BasePage {

  //create constructor
  public BasePage(WebDriver driver) {
    this.driver = driver;
    this.wait = new WebDriverWait(this.driver, Duration.ofSeconds(TIMEOUT));
    PageFactory.initElements(new AjaxElementLocatorFactory(this.driver, TIMEOUT), this);
  }
  
  //other common methods which can be utilized in your respective child Page classes

}

----------------------------------------------------
public class LoginPage extends BasePage {

    //Your Locators and Weblelements 
    private static final LOGIN_ID = "login";
    
    //Constructor to supply webdriver
    public LoginPage(WebDriver driver) {
        super(driver);
    }
    
    //your action methods
    public void loginToApp(){
    driver.findbyelement(By.ID(LOGIN_ID)).click
    }

}
----------------------------------------------------
public class LoginTest extends BaseTest{

public LoginPage login;
BeforeAll()
{
login = new LoginPage(driver);
}

@Test
public void verifyLogin(){
login.loginToApp();
}

}

Upvotes: 0

Muditha Perera
Muditha Perera

Reputation: 1252

In the way, I implemented the framework. I used the driver in the commonFactory.class that contains commonly used elements. Each page was implemented as a child class of the commonFactory.class. so you don't have to implement the driver in each and every class. Since the driver is independent of the test scenarios it's better to have it in a separate way.

Upvotes: 0

robx
robx

Reputation: 3123

This answer won't be any much different from @zodvik, and your thought process, but is another optional approach. Instead of passing the driver around, you can create an abstract class that each page object can then inherit from. From the abstract class, can also contain some common functional methods that you will find yourself often using.

This is at least how I do it in Java language.

Upvotes: 2

zodvik
zodvik

Reputation: 921

I always include driver as part of every Page Object. The way I thought about driver was that it represents the state of the current page. It gives access to the URL, Page Source, etc.

Now, each page has a a current URL, a page source code, a page title which are all accessible through the driver.

Upvotes: 1

Related Questions