Design Page Classes in Page Object Model

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

Let’s see, Hiow can you design Page Classes in Page Object Model. In this tutorial, We will learn How can we generate the Page Classes in POM.

We have learned, we keep the Web Elements and action methods on these Web Elements in Page Classes for the specific page.

 

Now just think, What if we have to repeat the code in each page class for some repetitive actions and methods. Is it not make the code repetitive and make the unnecessary line of code.

To avoid this problem we generate the page classes.

 

First, We will declare the common methods in the Page Class like in below example we have declared the ‘Click’ method in the Page.java , which is common to all Page Classes, So any page Class can override this method and use it in an own way.

 

In this method we are are passing the cssSelector as the parameter, So we will pass the CSSSelector locator of web Element on which we want to click. and using exception handling. We are capturing the Exception if occurs. printStackTrace() method will print all exception in console or report.

 

Let’s see the complete structure of Page.Java Class

 

Let’s understand the above class.

In the very first line, we have declared the WebDriver class variable Static So that only one instance of the webdriver can invoke at a time. In class Constructor, we have invoked the WebDriver instance.

 

Later on, we have declared some common method for input text, click on the element and verify page title. Actually, these are the common operation in all pages so that we have mentioned these in the Super class (Page.java). in subclasses, we will use these.

 

Page Classes

Now, We will see how can we design the Page classes. Suppose we need to design the Page class for ForgottenPassword page. Now we will design the forgotten password page class structure.

 

Let’s understand the above code. In ForgottenPasswordPage class, first we will declare the constructor of the class and call the super class constructor inside this using super() .

 

Now we have stored the Locators in class variables and in Page methods we are overriding the super class (page.java) methods.

Like, in enterEmail method we are passing the CSS location of email field and text what we need to pass to it.

 

Later on, we will use these classes in Test Classes to make the Test class.

 

Test Classes

Let’s see the sample Test classes based on the above implementation.

 

Let’s understand the above code. In the above Test Class, we have written the Test for ‘User is able to navigate on forgot password Page

 

At first, we have invoked the object of both classes what we are going to use in this Test. Because Forgot Password link is on the Login page and later on to verify the page availability we need to verify the Forgot Password page title, So need to switch on another page.

 

Using login page object we clicked on Forgot Password link and in next step, we verified the page tittle using Forgot password page object. This is called page switching in Selenium because we have declared the Webdriver object static so both instance will use the same instance of the web driver.

 

Like this, we design the Page classes and use these classes in POM. For better understanding practice it on your own local machine and design your own framework.

0 Comment

Leave a Reply