-1

In the Page Object model, we separate page UI elements and Test methods. However, there is a concept of Page chaining, in which we return an object on the next Page Object in the previous page object method. Now, I don't use this in my Cucumber BDD Selenium+Java framework as not every page object calls the next page. I use the PageFactory pattern.

  1. Some page objects perform some actions like Edit user profiles, search user, etc... Is it a bad practice not to return the instance of the next Page class in the chain?
  2. Also, I don't create a separate Page Object java class for each page in my application. I make PageObject per functionality, not per page. Like only AddContacts.java, not for the Edit Contact page, View Contact page, etc. Is it again acceptable?
  3. What is the difference between the page factory and the POM model? In the page factory, we don't return page objects in methods.

Also, are there some resources on the theory of Page object chaining/pattern?

public HomePage loginToSite(String username, String password)
{
    uName.sendKeys(username);
    pWd.sendKeys(password);
    loginBtn.click();
    return new HomePage();
}

2 Answers 2

1

Some page objects perform some actions like Edit user profiles, search user, etc... Is it a bad practice not to return the instance of the next Page class in the chain?

It will depend on what your client needs. If the clients of the object need an object that represents another page or if it needs another object for the same page, but with a different state, you should return it right away, so the flow will be the responsibility of the page object, not the clients. If the clients define the flow, you may end up with code duplication (responsibility scattered through the codebase).

Also, I don't create a separate Page Object java class for each page in my application. I make PageObject per functionality, not per page. Like only AddContacts.java, not for the Edit Contact page, View Contact page, etc. Is it acceptable again?

I don't see any problem in principle, but in order to respect the Single Responsibility Principle, you will need to create objects to represent the pages themselves. Otherwise, you may end up with multiple reasons to change one class: E.g. in "Edit User Profile", you may have to deal with Edit Profile form itself (one responsibility) and with the Profile page (another responsibility). Thus, your Action objects will coordinate the multiple pages, but the interaction details will be on the objects representing the page.

What is the difference between the page factory and the POM model? In the page factory, we don't return page objects in methods.

Page Factory is just an annotation that Selenium provides to create WebElements objects.

@FindBy(id="elementId") WebElement element;

Page Objects is a design pattern about how to map a UI into objects.

IMO, the name Page Factory is unfortunate, because it indeed may confuse with Page Objects, but one thing has nothing to do with the other.

1
  • This is a great answer!
    – Lee Jensen
    Jan 30 at 22:07
1

It is not mandatory to return a PageObject instance in every Page Object method, but it is a common practice in the Page Object Model to return a new page object instance to allow for page chaining, i.e., calling multiple page actions in a single line of code.

Having a Page Object per functionality rather than per page is also acceptable and can make your code more organized. It depends on the size and complexity of your application, as well as personal preferences.

The difference between the Page Factory and the Page Object Model is that the Page Factory is a way to initialize elements in a Page Object and it does not necessarily dictate a specific pattern for returning page objects. The Page Object Model, on the other hand, is a design pattern for organizing UI elements and test methods in a single class for each page.

There are many resources available online for learning about the Page Object Model and Page Factory, including blogs, online courses, and books on Selenium testing and automation.

Here are some online resources that might be helpful in learning more about the Page Object Model (POM), Page Factory, and related topics:

  1. Martin Fowler's article on the Page Object Model: https://martinfowler.com/bliki/PageObject.html

  2. SeleniumHQ's official documentation on the Page Object Model: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/PageFactory.html

  3. AutomatedTester's blog post on the Page Object Model: https://www.automatedtester.co.uk/blog/page-object-model-selenium-webdriver

  4. Guru99's tutorial on the Page Object Model: https://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html

  5. Testing Excellence's article on the difference between Page Factory and Page Object Model: https://testing-excellence.com/page-factory-vs-page-object-model/

  6. Software Testing Help's article on the Page Object Model: https://www.softwaretestinghelp.com/page-object-model-pom-in-selenium-with-pagefactory/

These resources should give you a good idea of the concepts, benefits, and implementation details of the Page Object Model and Page Factory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.