Frage

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!

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top