DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Using Selenium 2.6 ExpectedConditions with Thucydides

John Ferguson Smart user avatar by
John Ferguson Smart
·
Sep. 30, 11 · Interview
Like (0)
Save
Tweet
Share
7.07K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • Thucydides Github project
  • Official Thucydides Blog

 

 

From http://weblogs.java.net/blog/johnsmart/archive/2011/09/27/using-selenium-26-expectedconditions-thucydides

Testing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 11 Observability Tools You Should Know
  • Container Security: Don't Let Your Guard Down
  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Building a Real-Time App With Spring Boot, Cassandra, Pulsar, React, and Hilla

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: