Search code examples
watir-webdriver

How do I delay selecting from a dropdown until it's populated?


I'm using Watir WebDriver. I'm new to ruby.

The following dropdown list is always present. It fails unless I precede it by sleep(1). The developer said that the dropdown is not populated until the previous controls are set.

Which of the Wait commands do I need for this? I think in Selenium I waited until the hidden contents of the list contained the value that I wanted, then I selected that value.

def enterCompany(company)
    @browser.select_list(:id, "ddlCompanyName").select(company)
end

Solution

  • You can use the when_present to wait until the option is present before selecting it. Basically, Watir will wait up to 30 seconds for the option to appear. If it appears sooner than 30 seconds it will proceed with the action (ie select). Otherwise, a timeout exception is thrown.

    @browser.select_list(:id, "ddlCompanyName").option(:text => company).when_present.select
    

    Note that the above assumes that company is the text of the option.

    Another option is to wait for anything to appear in the dropdown.

    @browser.wait_until{  @browser.select_list(:id, "ddlCompanyName").options.length > 0 }