How To Establish Relationship Between Pages In Page Object Model In Selenium WebDriver

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

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

  1. Hello Amod,
    Firstly Thank you for sharing this. Its very helpful.
    I have been building a page object model framework using python but the way my UI is structured is –
    Main page A (has a link to page B) -> Page B (has a link to page C ) -> Page C
    Now what I understand in terms of navigation is if I have to navigate to Page B i should be doing so by returining object of page B in page A and likewise for page C
    but when I define my suite file(main testcase file) which has the tests for validation of Page C; should I be just initialising the Page A and likewise access the methods through the objects.
    Thanks in advance for your help.

  2. 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.

  3. 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 to Amod Mahajan Cancel reply

Your email address will not be published. Required fields are marked *