Watir-webdriver - clicking a Javascript button

In the first week, using watir-webdriver and testing web applications in general, so I'm still trying to wrap some concepts.

The presence of 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 does not result in a button click,

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

So, I got managed Firefox by clicking a button using

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

but what are the differences between them?

+2
watir-webdriver watir
source share
2 answers

Regarding the differences between the two:

  • .click = simulates a left-click on an object.
  • .fire_event = executes javascript even if it is available or may not be accessible usually with the mouse.
  • when_present.click = waits for the object to be accessible and displayed in the viewing area (full browser) of the window) before it tries to click.

when_present is useful when your site uses AJAX, and interacting with one object results in another object appearing. Using .click may try to click the second object until it is available, and the script will fail.

Your page probably contains an AJAX form, and the button you are trying to interact with does not load immediately, but after a short delay, when:

  • text field entered
  • another button pressed
  • page completes content creation
  • and etc.

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

+2
source share

Can you check the zoom level of the browser where you run your test?

I ran into the same problem with the click method, but I was able to “fix” it by setting the zoom level of my browser (IE9) to 100%.

Please note that for Selenium Webdriver requirements for Internet Explorer, this is a requirement for Native Events to work. see here

+1
source share

All Articles