WebdriverIO Tutorial | How To Implement Page Object Model (POM) | Part V | LambdaTest

:rocket: Hello, testers! Exciting news!

Dive into another fantastic tutorial by Marco Cruz on WebdriverIO! :globe_with_meridians: Learn the ropes of implementing the Page Object Model (POM) and level up your testing game. Check out the video now! :man_technologist: #WebdriverIO #Testing #POM

Basic Page Object Model Implementation:

public class LoginPage {
    private WebDriver driver;
    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.id("login-button");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

    public void enterPassword(String password) {
        driver.findElement(passwordField).sendKeys(password);
    }

    public void clickLoginButton() {
        driver.findElement(loginButton).click();
    }
}

I wanted to share a quick tip on how to separate test data from page objects to keep your test code clean and maintainable. Here’s an example in Java:

public class LoginTestData {
    public static final String VALID_USERNAME = "username";
    public static final String VALID_PASSWORD = "password";
}

public class LoginTest {
    private WebDriver driver;
    private LoginPage loginPage;

    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testLoginWithValidCredentials() {
        driver.get("https://example.com/login");
        loginPage.enterUsername(LoginTestData.VALID_USERNAME);
        loginPage.enterPassword(LoginTestData.VALID_PASSWORD);
        loginPage.clickLoginButton();
        // Assertions or verification steps here
    }

    @AfterMethod
    public void teardown() {
        driver.quit();
    }
}

By separating LoginTestData from the LoginTest, you can easily manage your test data independently of your page object code. This approach enhances readability and maintainability.

Feel free to ask if you have any questions or need more details. Happy testing! :star2:

Integrating Assertions for Robust Testing:

java
Copy code
public class LoginTest {
    private WebDriver driver;
    private LoginPage loginPage;

    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testLoginWithValidCredentials() {
        driver.get("https://example.com/login");
        loginPage.enterUsername(LoginTestData.VALID_USERNAME);
        loginPage.enterPassword(LoginTestData.VALID_PASSWORD);
        loginPage.clickLoginButton();

        // Enhanced verification
        Assert.assertTrue(driver.findElement(By.id("welcome-message")).isDisplayed(),
            "Login failed or welcome message not found.");
    }

    @AfterMethod
    public void teardown() {
        driver.quit();
    }
}

This addition not only enhances clarity in demonstrating successful logins but also ensures that the test outcomes are clearly validated, improving reliability and maintenance of test scripts.