问题
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?
回答1:
As far as the differences between them:
.click
= simulates a left mouse click on the object..fire_event
= executes a javascript even that may or may not be accessible normally via mouse click.when_present.click
= waits for the object to be both available and appear in the viewable area (full browser window) before it attempts to click.
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:
- a text field is entered
- another button is clicked
- the page finishes generating content
- etc.
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.
回答2:
Can you check the zoom level of the browser you run your test in?
I encountered the same problem with the click method, but I was able to "fix" it by setting the zoom level of my browser (IE9) back to 100%.
Note that the Selenium Webdriver requirements for Internet Explorer state this is a requirement for Native Events to work. see here
来源:https://stackoverflow.com/questions/9215713/watir-webdriver-clicking-javascript-button