6. Object-Oriented Design for Automation & Page Objects

OOP in Test Frameworks

// Base Page with encapsulated driver/page reference:
public abstract class BasePage {
    protected String pageUrl;

public BasePage(String pageUrl) {
this.pageUrl = pageUrl;
}

public void open() {
System.out.println("Navigating to: " + this.pageUrl);
}
}

// Child Page inheriting common capabilities:
public class LoginPage extends BasePage {
private String usernameSelector = "#username";

public LoginPage() {
super("https://targetqa.com/login");
}

public void login(String user, String pass) {
open();
System.out.println("Typing credentials into " + usernameSelector);
}
}

Task

  • Create a TestUser class:
  • 1. Private fields: username (String) and role (String).
  • 2. Constructor accepting username and role.
  • 3. Getters getUsername() and getRole().
  • 4. Method isAdmin() returning true if role equals "ADMIN".
πŸ’‘Common doubts
Did you like the problem?
Lesson 6 of 38
Java
Loading code editor…
Next