Hello Folks,

In interview it is frequently asked question that “How To Establish Relationship Between Pages In Page Object Model In Selenium WebDriver”. It is very simple question but generally interviewee confuses. I will explain in a simple way in this post.

What is establishing relations between pages?

Suppose you have two pages, Page A and Page B. When you click on some web element on Page A, it navigates to Page B. So you should create method in such a way that after clicking on web element, it gives you control on Page B. User should not create an object of Page B to access members of Page B.

For example:

Take example of DemoQA . In home page it has a link “Registration”. When user clicks on it, user navigates to Registration page. So, you should create a method say “clickOnRegistrationLink” and set a return type as object of Registration page. It is called establishing relationship between pages.

Java Program:

DemoQaHomePage.java:

RegistrationPage.java:

RegistrationScript.java:

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

5 thoughts on “How To Establish Relationship Between Pages In Page Object Model In Selenium WebDriver

  1. Hi Amod,
    While creating my framework ,i have created separate page object classes for homepage,Registration. Though i have registration link on homepage screen. Once clicked i have stored registration page objects in separate class. I really looking forward some way to create one class which creates objects for all the page object classes runtime. Is it possible?if yes kindly advice how. Also please advice once the clickonRegistration function navigates to registration screen how other actions of registration page will be written? Can you provide one example of code of passing form details on registration page method?
    Also please advice that do i need to create only one page object class for entire application or separate classes for each screen page?Kindly advice

    1. Memory management is major feature of Java. Page object model follows the same. It brings something in memory when it used. You should keep webelement of page in separate classes and establish the relation if required.

  2. Hi,

    I would like to know your reasoning in detail for the below statement of yours:

    “User should not create an object of Page B to access members of Page B.”

    1. Hello,
      Suppose you have 10 Page classes and you need to create an object of a class if you want to use any member of that class. If the same is done internally by some methods , user need not to do anything. Here in example, clicking on button at Page A will navigate to Page B. If click methods does not return object of Page B, you need to create an object Of Page B to use Page B members. But if you establish relationship between Page A and Page B as I have done in Click method, user just need to store in Page B reference variable.

Leave a Reply