John is an experienced consultant specialising in Enterprise Java, Web Development, and Open Source technologies, currently based in Sydney, Australia. Well known in the Java community for his many published articles, and as author of Java Power Tools and Jenkins: The Definitive Guide, and founder of the open source Thucydides Automated Acceptance Test Library project, John helps organisations to optimize their Java development processes and infrastructures and provides training and mentoring in agile development, automated testing practices, continuous integration and delivery, and open source technologies in general. John is the CEO of Wakaleo Consulting, and runs several Training Courses on open source Java development tools and best practices. John is a DZone MVB and is not an employee of DZone and has posted 125 posts at DZone. You can read more from them at their website. View Full User Profile

Using Selenium 2.6 ExpectedConditions with Thucydides

09.29.2011
| 5694 views |
  • submit to reddit

Thucydides 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

Published at DZone with permission of John Ferguson Smart, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)