Question

For example, if I were to test Google search, what is the benefit of the Page Object model returning new Google Search Page Object?

E.g.

public class SearchPage {
    private final WebDriver driver;

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

    public SearchPage search(String query) {
        WebElement e = driver.findElement(By.name("q")).sendKeys(query);
        e.submit();

        return new SearchPage(driver);
    }
}

vs

public class SearchPage {
    private final WebDriver driver;

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

    public void search(String query) {
        WebElement e = driver.findElement(By.name("q")).sendKeys(query);
        e.submit();
    }
}

Thanks for the help!

Was it helpful?

Solution

One thing that comes to my mind is chaining the methods from SearchPage class. When you would have lets say some higher level class that is responsible for running the tests you could use sth like this:

String actualText = searchPage.search("q").openFirstResult().selectItemFromCombo().checkName().getNameText() 

etc. etc.

This makes reading your code very easy, looks almost like a sentence and it is understandable for othe people.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top