Using Selenium 2.6 ExpectedConditions with Thucydides
Join the DZone community and get the full member experience.
Join For FreeThucydides is an open source library designed to help you write better ATDD-style automated acceptance tests with Selenium 2.
Selenium 2.6 provides a new API that allows more fluent wait conditions, such as waiting for several fields to be visible or enabled. This has been integrated into the Thucydides PageObject class with a helper method to make it a bit easier to use.
Suppose we have a page object class with some fields, as shown here:
public class MyPage extends PageObject { protected WebElement firstName; protected WebElement lastName; protected WebElement city; protected WebElement country; public MyPage(WebDriver driver, int timeout) { super(driver, timeout); } }
The first useful thing to do is to implement access methods for the fields, as shown here:
public class MyPage extends PageObject { protected WebElement firstName; protected WebElement lastName; protected WebElement city; protected WebElement country; public MyPage(WebDriver driver, int timeout) { super(driver, timeout); } public WebElementFacade firstName() { return element(firstName); } public WebElementFacade lastName() { return element(lastName); } public WebElementFacade city() { return element(city); } public WebElementFacade country() { return element(country); } }
These methods make it easier to write tests and test steps about the fields. For example, in the following test we check that the firstName field is both visible and read-only:
@RunWith(ThucydidesRunner.class) public class MyPageTest { @Managed public WebDriver webdriver; @ManagedPages(defaultUrl = "http://localhost:8080/index.html") public Pages pages; @Test public void first_name_field_should_be_visible_and_enabled() { MyPage page = pages.get(MyPage.class); page.open(); page.firstName().shouldBeVisible(); page.firstName().shouldNotBeEnabled(); } }
If this field is not expected to be present immediately on the screen (such as might happen in an Ajax application), we can use the waitFor methods:
@Test public void first_name_field_should_be_visible_and_disabled() { MyPage page = pages.get(MyPage.class); page.open(); page.firstName().waitUntilVisible(); page.firstName().waitUntilDisabled(); }
If this test succeeds, it means that the field was eventually both visible and read-only. If this does not happen, the test will timeout and fail with an appropriate exception.
Using the new Selenium 2.6 ExpectedCondition API, we could also do this in a more elegant fashion. First, we write a method in the Page Object class that embodies the condition we are waiting for, that is, that the first name field is both visible and disabled, along with a simple method that uses the waitForCondition() method to wait for this to happen:
public class MyPage extends PageObject { ... public ExpectedCondition firstNameIsVisibleAndDisabled() { return new ExpectedCondition() { public Boolean apply(WebDriver driver) { return (firstName.isDisplayed() && !firstName.isEnabled()); } }; } public void waitForFirstNameField() { waitForCondition().until(firstNameIsVisibleAndDisabled()); } }
Next, you can wait for this condition to occur in your test or test step:
@Test public void first_name_field_should_be_visible_and_disabled() { MyPage page = pages.get(MyPage.class); page.open(); page.waitForFirstNameField(); }
This becomes even more compelling when you need to wait for several fields. For example, suppose you wanted to wait for both the firstname and the lastname field to be enabled. You could do this as shown here:
public class MyPage extends PageObject { ... public ExpectedCondition nameFieldsAreEnabled() { return new ExpectedCondition() { public Boolean apply(WebDriver driver) { return (firstName.isEnabled() && lastName.isEnabled()); } }; } public void waitForNameFields() { waitForCondition().until(nameFieldsAreEnabled()); } }
Learn more about Thucydides at the following sites:
From http://weblogs.java.net/blog/johnsmart/archive/2011/09/27/using-selenium-26-expectedconditions-thucydides
Opinions expressed by DZone contributors are their own.
Comments