Separation of concerns with page objects

Our second issue is that, while we abstracted away our WebElement creation, we didn’t abstract away any of the heavy lifting performed by our test script. We are now using a page object, but we are still going to have lots of duplication in our code. Let’s illustrate this by looking at a script to perform a login and how we would do that using our new page objects.

Let's start with a basic login page object:

package com.masteringselenium.page_objects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {

public static By usernameLocator = By.id("username");
public static By passwordLocator = By.id("password");
public static By loginButtonLocator = By.id("login");
}

Next, we need a test that uses this LoginPage object:

@Test
public void logInToTheWebsite() {
driver.get("http://web.masteringselenium.com/index.html");

WebElement username =
driver.findElement(LoginPage.usernameLocator);
WebElement password =
driver.findElement(LoginPage.passwordLocator);
WebElement submitButton =
driver.findElement(LoginPage.loginButtonLocator);

username.sendKeys("foo");
password.sendKeys("bar");
submitButton.click();

assertThat(driver.getTitle()).isEqualTo("Logged in");
}

Our script is quite clean and we are using our page object to find the elements that we need to use. However, what if we write another script that needs to use the login process? We will end up reusing the first six lines of code that are in this script.  The same will apply for any other script that performs a login; that's a lot of code duplication!

We can make this better! We know that the duplicate lines of code are filling in the form fields to perform a login. Let’s abstract this action away into the page object:

package com.masteringselenium.page_objects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {

public static By usernameLocator = By.id("username");
public static By passwordLocator = By.id("password");
public static By loginButtonLocator = By.id("login");

public static void logInWithUsernameAndPassword
(String username, String password, WebDriver driver) {

driver.findElement(usernameLocator).sendKeys(username);
driver.findElement(passwordLocator).sendKeys(password);
driver.findElement(loginButtonLocator).click();
}
}

We can now use the action that is in the page object in our tests:

@Test
public void logInToTheWebsiteStep2() {
driver.get("http://web.masteringselenium.com/index.html");
LoginPage.logInWithUsernameAndPassword("foo", "bar", driver);

assertThat(driver.getTitle()).isEqualTo("Logged in");
}

Our test is now much cleaner, it has fewer lines of code, and it is nice and descriptive without being overly verbose. We can also reuse this login function in all of the tests that we write, that perform a login.

Keep your assertions in your tests, not in your page objects. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.201.209