Docs
Page Objects: it's simple!
Selenide makes page objects concise and readable. No PageFactory, no initElements, no annotations: just your logic.
Page Objects with Selenide
Page Object is a very popular pattern in the QA world. For every page you create a separate class, a page object, which holds the logic of working with that page’s elements. The idea is to avoid duplicating locators across tests.
Selenide lets you write page objects that are shorter and more readable. This is what a page object looks like with Selenide:
public class GoogleSearchPage {
public GoogleResultsPage search(String query) {
$(By.name("q")).setValue(query).pressEnter();
return page(GoogleResultsPage.class);
}
}
public class GoogleResultsPage {
public ElementsCollection results() {
return $$("#ires li.g");
}
}
As you can see, there’s no PageFactory, no initElements and no other boilerplate like that.
Your page object contains only your logic.
And the test? Here it is:
GoogleSearchPage searchPage = open("/", GoogleSearchPage.class);
GoogleResultsPage resultsPage = searchPage.search("selenide");
resultsPage.results().shouldHave(size(10));
resultsPage.results().get(0).shouldHave(text("Selenide: Concise UI Tests in Java"));
Isn’t it simple?
Since Selenide elements are lazy, you can also keep them in plain fields. They’re looked up only when used:
public class LoginPage {
private final SelenideElement username = $("#username");
private final SelenideElement password = $("#password");
public void loginAs(String user, String pass) {
username.setValue(user);
password.setValue(pass).pressEnter();
}
}
Classic page objects
There is a popular style of page object that declares a separate annotated field for every element on the page. This style has some disadvantages, but if you want it, Selenide supports it:
public class GoogleSearchPage {
@FindBy(how = How.NAME, using = "q")
private SelenideElement searchBox;
public GoogleResultsPage search(String query) {
searchBox.setValue(query).pressEnter();
return page(GoogleResultsPage.class);
}
}
public class GoogleResultsPage {
@FindBy(how = How.CSS, using = "#ires li.g")
public ElementsCollection results;
}
What disadvantages?
One disadvantage of the classic style is that it isn’t dynamic enough.
Imagine a page showing an alphabet of 26 letters.
Do you really want to declare 26 fields in your page object, each annotated with @FindBy?
class AlphabetPage {
@FindBy(xpath = "//*[@letter='A']")
WebElement a;
@FindBy(xpath = "//*[@letter='B']")
WebElement b;
// 24 more elements ...
}
And what if the user can switch languages, and each language has a different number of letters? Or wait, are you going to create 7,164 page objects, one for every language? :) Sure, there’s code generation, Lombok and annotation processing. Go on, let your CV swell. You can even ask an AI to write those hundreds of page objects…
Or would you rather have one short method?
public void pickLetter(char letter) {
$(by("letter", String.valueOf(letter))).click();
}
A final note on OOP
Remember that the original idea of page objects was to encapsulate (read: hide) the logic of working with page elements. Tests should not deal with elements, locators or XPaths. They should call the page object’s methods instead. So even if you declare fields for page elements, please make them private!
Methods public, fields private.
Otherwise, OOP is just nonsense.
Didn't find the answer?
Ask the community, we answer fast: Selenide examples · Telegram · Report an issue