Friday, 26 January 2018

8. The Login Page Object dissected

Creating the Login Page Object


So I believe you are all excited and geared up to start building a robust framework along with me. The website that we are going to automate is a HR management website. The URL for which is http://opensource.demo.orangehrmlive.com. We will cover the following scenarios: Login, Add Employee, Edit Employee, and Logout. Login and Logout are integral parts of the Add Employee and Edit Employee scenarios. Next we will identify the screens involved in these scenarios. There will be one page object per screen.
The screens identified are:
Login
Home Page
Add User Page
Edit User Page

Creating a page object involves specific steps
1) Create the locators for the page
2) Create the constructor
3) Initialize the Page Factory in the constructor
4) Create specific methods for each elements - These methods will in turn call the common methods in the base class that we have mentioned earlier.
5) The scenario design will be done in the test classes

This way we are abstracting out the functionality.

We begin with the entry point of every application ie. the Login Page. For logging in, the id will be Admin and the password will be admin. The home page is displayed after clicking the sign-in button.

The thing to be kept in mind while designing page objects
Page Objects cannot contain assertions

Page Objects cannot contain Test annotations

The picture below displays the Login Page where there are three main WebElement [Two textboxes and one button]

Let's have a look at the Login Page Object code

package hrdemoapp;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class Login extends BaseClass{
@FindBy(how=How.XPATH,using="//*[@id='txtUsername']")
private static WebElement txtUserName;
@FindBy(how=How.XPATH,using="//*[@id='txtPassword']")
private static WebElement txtPassword;
@FindBy(how=How.XPATH,using="//*[@id='btnLogin']")
private static WebElement btnLogin;
public Login(){
PageFactory.initElements(driver, this);
}
public void enterUserName(String userName){
enterText(txtUserName,userName);
}
public void enterPassword(String password){
enterText(txtPassword,password);
}
public HomePage clickLogin(){
clickButton(btnLogin);
return new HomePage();
}

}

The code highlighted in yellow is our object repository. Let's break this code and analyze.

@FindBy - Annotation that specifies the object location strategy for a webelement or a list of webelements
how - indicates how you want to search for an element
using - put the dynamic x-path here

Underneath the FindBy annotion, we define the webElement that will hold the derived object

Next, the code in turquoise color is the constructor. Here we initialize the Page Factory.

The final methods are one where we perform actions on webelements. The first two have a return type of void. This is because we stay on the Login Page after entering the username and password. The clickLogin method, although has a return type HomePage. This is because, after clicking the Login Button, we get navigated to the HomePage. One important point to note is our Login page extends the Base Class. Thus we have access to all protected and public variables and methods. That is the reason, we were able to call the enterText() and clickButton() methods even though they were not defined in the Login class.

No comments:

Post a Comment