watir-webdriver - clicking Javascript button
Asked Answered
L

3

2

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?

Liston answered 9/2, 2012 at 17:27 Comment(2)
"This doesn't work" does not tell us much. What did you expect to happen? What happens when you try browser.button.present?Shalna
Thanks Željko. Tried to better state the question per your suggestions. But my sole question is the difference between using .fire_event('on_click') to define the action javascript will take on the event vs .when_presentListon
F
3

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.

Folkways answered 9/2, 2012 at 17:58 Comment(5)
Ah, I see, thanks Adam. So is it expected that browser.button(:value, "Search").exists? and browser.button(:value, "Pesquisar").present? return true though the button is still not available?Liston
If exists? returns true, the button is in the DOM. If present? returns true, the button is viewable and available. In this case, click should work BUT, I have run into an issue where the button was still unavailable. I resolved this issue by using the DIV that contained the button in my locator. I can't explain why, but accessing the button in this specific manner resoled the issue for me: browser.div(:name, /testing/).button(:value, "Search").clickFolkways
Curious Joao - did this approach in the comment (using the div) work for you? Or did you continue with one of the previous solutions?Folkways
I've kept using one of the others. I +1 due to your explanation and to remembering trying your .div approach if I run into the same issue in the future. Thanks again.Liston
@adam reed, prefixing with the div works! I don't know why. I encountered this same problem where the button is covered by javascript that changes the button text from "Save" to "Processing". Nothing seemed to work but prefixing the button with the div. Ruby example: @browser.div(class: 'rb_DefaultLayoutDiv').button(id: OrderEntryPOM.save_id).clickKarwan
D
1

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

Doorstep answered 14/5, 2012 at 18:24 Comment(1)
Was using Firefox. Thanks anyway.Liston
K
0

Adding to this story. I encountered the same problem where the button is covered by javascript to change the button text from "Save" to "Processing...". Nothing seemed to work using .click, .fire_event('onclick'), .fire_event('OnClientClick'). It was only prefixing the normal button .click syntax with the div that would work. The following is the final Ruby code:

@browser.div(class: 'rb_DefaultLayoutDiv').button(id: OrderEntryPOM.save_id).click
Karwan answered 4/4, 2022 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.