Writing test automation can only be guileless if developers agree they don’t touch the features that are automated. Though this can be dream come true for any test automation engineer, it contradicts objective behind automation, automation is written to catch early bugs in development and to avoid manual testing to some extent.
Automation that began with ease and confidence can end up with high maintenance increasing cost and become a de-motivating factor for the team. A single change in identifying one element can end up updating all automation tests. So no doubt its better to plan and analyze needs for automation and choose an easy to build and maintain the approach that suits best your team. You can go to our blog on designing test automation framework, which can help you in deciding your requirements. In this article, we are going to look in Page Object Model to write maintainable and reusable tests in automation.
Page Object Model
As name highlights, the approach is based on page object repositories. In simple words, we need to create page objects for every page or screen in our application. The object should contain, all the elements on that page and any actions specific to that page. If app contains very huge pages, we can split the actual page into different panels in the page and have classes for panels instead of pages.
For example, with Login page in the app, we can have login page object containing, username input, password input and submit button as elements and login into the app as an action method.
Login page object:
Elements:
|
Actions:
|
From implementation point, here object is the class instance, so we need to write page class including its elements and actions. Writing this is different syntactically in various languages.
Here’s an example from Java:
public class LoginPage {
private static final By usernameInput = By.id("username");
private static final By passwordInput = By.id("password");
private static final By loginButton = By.id("login");
private WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void loginIntoApp(String username, String password) {
driver.findElement(usernameInput).sendKeys(username);
driver.findElement(passwordInput).sendKeys(password);
driver.findElement(loginButton).click();
}
}
Why?
When?
Not For?
If you are already using page object model or tried using it, share your experiences with us in below comments