Friday, 24 February 2017

Creating the Base Page & Top Menu classes

Let’s add a base page class which will contain all the reusability methods & each page class will extend the base class.
To create a BasePage.java, Right Click on com.demo.pom.pages > Click on New > Class > Insert the name as BasePage

Once the BasePage.java file created, you can add some common methods or else download BasePage.java file & replace in your com.demo.pom.pages package. The advantages of having BasePage is that we can have re-usable methods in our project.
E.g. till now in each page classes we are creating WebDriver driver object which is common. So, we can add the WebDriver driver statement in our BasePage class so that every page which are extending the BasePage will have the same drive object.
From the above steps, we have completed creating a BasePage class file which will have all the common functionalities. Let’s create another class known as Menu.java, where we will add all the common menu elements (which is common in each page).
Menu.java
In this class file, we will declare all the common features of pages. E.g. after login there is a logout link, there is search field or T-Shirts menu which will be displayed in every page. So, we will write methods for logout, search, gotoTshirts methods inside this class. We will implement the actual code latter, here I am just trying to explain the architecture.
So, we got an idea about Menu class. We have a BasePage class which is the super class for all page classes. So inside BasePage class create the object for Menu class so that every child class can access the Menu class object. Create a getter method as below inside BasePage class:
public Menu getMenus(){
              return menu;
       }
The above method will return the object of Menu class. In any child class, we can call the methods inside menu class by using the below command:
myAccountPage.getMenus().logout(); //where myAccountPage is the object MyAccountPage class.

Download the files from here & follow the steps below:
  •  Extract the .zip file
  •  Copy & replace SignTest.java inside com.demo.pom.testcases package.
  • Copy & replace “BasePage.java, LunchPage.java, Menu.java, MyAccountPage.java & SignInPage.java” files inside com.demo.pom.pages package.
  • Run the SignTest.java file as TestNG Test & verify the below result:

In console if you are seeing Insile Search & Insile Logout that means the Menu class is accessible by any other page classes.



Note: Till now we have not implemented the test cases & I am explaining about the Page Classes first. Once we are ready with Page Classes it will be easy to implement the test cases.


<<<Prev       Next>>>

3 comments:

  1. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Java Training in Bangalore

    ReplyDelete
  2. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
    "Devops Training in Chennai"

    ReplyDelete
  3. Amazing post thanks for sharing the post.Your blogs are admirable and full of knowledge DevOps Training in Bangalore | DevOps Training in India

    ReplyDelete