Page Object Model in Selenium java

- Advertisement -

pageobjectmodel

Building the right page factory is key which leads to less maintenance, well organized code and easy to understand the flow of the automated script and application.

now a days most of applications are web based and more complex in nature.  it usually has lot of static and dynamic pages. while automating such applications, we have to make some systematic approach about how we should organize the code flow which will ultimately help in reducing maintenance efforts and easy to understand.

Page factory is one of concept to organized your code. selenium provide certain functionality to achieve it. many people have myth that page factory is test automation framework. but it is not a Test Automation Framework. you can use Page Factory in any Test Automation framework. it is just the way how you organized the code with functionality provided by Selenium API.

in this post , we will see how we can implement the Page factory in java and what is the best to organize the code.

Page object model (POM) based automation framework is widely used automation framework for web based applications. it is effectively being used with selenium. Page object model (POM) can be used in any kind of framework such as data driven, modular, keyword driven etc. Page object model gives more focus on how the code is being structured to get the best benefit out of it. it has its own positive and negative sides.

What is Page Object Model?

In Page Object Model code is arranged or write considering the functionality present on the webpage. All Objects and Actions which can be performed on that object on the webpage get stored in one class file.

page object model

Lets take an example:

Suppose for login page we want to apply Page object model.

login

 

 

Step 1 : Create New class file:

First step will be creation of new class file. its always preferred to give the class name same as web page name, so in this case class name will be Loginpage.java.

Step 2: Identify the Objects/Controls:

First step will be identifying the controls/objects on the web page. in this case followings are the objects

- Advertisement -

  1. UserName textbox
  2. Password textbox
  3. Sign in Button
  4. Not a Member Link
  5. Sign up Now Link

each web element should be uniquely gets identified and should be defined at class level.

we will use FindBy annotation and will define web element. so that we can able to perform actions on them.

for above controls my code will look like this

(Note all below code is pseudo code just to explain POM concept)

@FindBy(id = "username")
private WebElement txtuserName;
@FindBy(id = "password")
private WebElement txtpassword;
@FindBy(id = "button")
private WebElement btnsignIn;
@FindBy(textlink = "forgotpassword")
private WebElement linkforgotpassword;
@FindBy(textlink = "signin")
private WebElement linksignin;

 

Step 3 : Write the possible actions on the objects ( web elements)

in this  case following actions can be performed on web elements

  1. For Textbox :  set text, clear text
  2. For Button : Click, get text on button, get visibility status ( enable/disable)
  3. For Link : Click, get Link text

 

now we have write the method will perform this actions

For TextBox :

Public static void txtSetUserName(string userName){
try
 {
 txtuserName.sendkeys(userName);
 }
 catch (NoSuchElementException e) {
 System.out.println("Exception message"+ e.getMessage());
 Assert.fail("Element is not found");
 }
}

Public static void txtClearUserName(){
try
 {
 txtuserName.clear();
 }
 catch (NoSuchElementException e) {
 System.out.println("Exception message"+ e.getMessage());
 Assert.fail("Element is not found");
 }
}

in similar we need to write code for identified controls.

Step 4: Create Business Reusable

Now we are ready to create business logic and create reusable components. on Business reusable class file, create following method.

 

<span class="kwd">
public</span> <span class="kwd">class</span> TestCase<span class="pun">{</span> <span class="lit">@Page</span>
<span class="typ">PageObject</span><span class="pln"> Loginpage</span><span class="pun">;</span> Public static void Login(String userName, String password){ Loginpage.txtsetusername (userName); Loginpage.txtsetpassword (password); Loginpage.btnclicksignin(); }

 

we hope this article give you information about how the POM is and how to implement it in your project. if you have any queries, do let us know.

 

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More