Page Object Manager

In last chapter of Page Object Pattern of Selenium Cucumber Framework, we learned how to design Page Object pattern using Selenium Page Factory in Cucumber. This chapter is again based on Page Objects. But in this chapter we will design a Page Object Manager

In last chapter we notices that we were creating objects of the different pages using below statement in Cucumber Step Definition files:

HomePage homePage = new HomePage(driver);
ProductListingPage productListingPage = new ProductListingPage(driver);

But what is the problem here. So far we have just one single Cucumber Step Definition file. But in the case of multiple step definition files, we will creating object of Pages again and again. Which is against the coding principle.

To avoid this situation, we can create a Page Object Manager. Duty of the Page Object Manger is to create the page’s object and also to make sure that the same object should not be created again and again. But to use single object for all the step definition files.

Design Page Object Manager Class

1. Create a New Package file and name it as managers, by right click on the src/main/java and select New >> Package. As this Page Object Manager is a part of the framework, we keep this in src/main/java folder.

2. Create a New Class file and name it as PageObjectManager by right click on the above created Package and select New >> Class

PageObjectManager.java

Explanation

Constructor

public PageObjectManager(WebDriver driver) {
     this.driver = driver;
}

This constructor is asking for parameter of type WebDriver. As to create an object of the Pages, this class requires a driver. Now who so ever will create the object of this class needs to provide the driver like :

PageObjectManager pageObjectManager = new PageObjectManager(driver);

Page Object Creation Method
public HomePage getHomePage() {
     return (homePage == null) ? new HomePage(driver) : homePage;
}
This method has two responsibilities:

  1. To create an Object of Page Class only if the object is null.
  2. To supply the already created object if it is not null 

Modification in Cucumber Step Definition File

The change in above code requires change in our step definition file as well.

Steps.java

Notice, now the duty of the creation of all the pages assigned to only one class which is Page Object Manager.

Run the Cucumber Test

Run as JUnit

Now we are all set to run the Cucumber test. Right Click on TestRunner class and Click Run As  >> JUnit TestCucumber will run the script the same way it runs in Selenium WebDriver and the result will be shown in the left hand side project explorer window in JUnit tab.

Project Explorer

PageObjectManager

Page Object Classes

Although there are no changes required in our Page Objects classes, but I am pasting all of these for your reference.

HomePage,java

ProductListingPage.java

CartPage.java

CheckoutPage.java

Cucumber Test Runner File

No change in Test Runner file.

TestRunner.java

Cucumber Feature File

No change in Test Runner file.

End2End_Tests.feature

For more updates on Cucumber Framework, please Subscribe to our Newsletter.

Please ask questions on ForumsQA, in case of any issues or doubts.

原文地址:https://www.cnblogs.com/wldan/p/10546565.html