watirwatir-webdriver

watir-webdriver - clicking Javascript button


First week with watir-webdriver and Web app testing in general, so still trying to wrap some concepts around.

Having this javascript element:

<input type="submit" class="button" value="Search" name="_target0">

browser.button(:value, "Search").exists?
=> "true"

browser.button(:value, "Pesquisar").present?
=> true

browser.button(:name, "_target0").value
=> "Search"

This doesn't actually drive the button to get clicked,

browser.button(:name, "_target0").click

So I got the driven Firefox clicking the button using either

browser.button(:name, "_target0").fire_event('on_click')
browser.button(:name, "_target0").when_present.click

but what are the differences between them?


Solution

  • As far as the differences between them:

    when_present is useful for when your site uses AJAX, and interacting with one object causes another object to eventually appear. Using a .click may attempt to click the second object before it is available, and the script will fail.

    It is likely that your page contains an AJAX form, and the button you are attempting to interact with does not load immediately, but after a short delay when:

    Since fire_event does not look for the physical representation of the button, but rather the JS event in the source, it can be used before the button is present/visible/actionable.