Ruby Watir: Clicking OK on JavaScript Alerts?
Asked Answered
K

5

10

Seems none of the code I've tried has any affect. My intention is to close any and all JavaScript prompts that may come up by hitting the "OK" button. Problem is, my script has no affect on the prompts that come up. In other words, it does nothing.

Here's what I have:

fx = FireWatir::Firefox.start(somepage)
fx.startClicker("OK")
fx.button(:id, "OK").click
fx.button(:id, "CONFIRM").click

The HTML:

<script type="text/javascript">
    alert("Alert!");
    window.confirm("Confirm?");
</script>

The text in the prompts can change, my intention is to hit OK regardless of what is inside the alert/confirm prompt.

PS: I'm running Ubuntu.

Kinchinjunga answered 12/2, 2010 at 2:23 Comment(0)
R
6

The best way is to stop pop-ups from triggering at all.

require 'watir'
b = Watir::Browser.start "http://somepagewithdialogs"
# don't return anything for alert
b.execute_script("window.alert = function() {}")

# return some string for prompt to simulate user entering it
b.execute_script("window.prompt = function() {return 'my name'}")

# return null for prompt to simulate clicking Cancel
b.execute_script("window.prompt = function() {return null}")

# return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")

# return false for confirm to simulate clicking Cancel
b.execute_script("window.confirm = function() {return false}")

# interact with some element which would trigger the pop up
b.button(:id => "dialogTrigger").click

See: http://watirmelon.com/2010/10/31/dismissing-pesky-javascript-dialogs-with-watir/ for more detail.

Ridglee answered 2/12, 2010 at 10:12 Comment(0)
C
1

this was asked an eternity ago so I'm just adding something a little more updated that did it for me

@browser.alert.exists? @browser.alert.ok @browser.alert.close

first one will return a boolean second one will ok whatever action you are prompted to do and third one will close the alert with no

Countenance answered 16/1, 2018 at 17:59 Comment(1)
In 2022, Rails 6, Ruby 2.7 and Watir 7.1, this works.Loaning
H
0

Pop ups are black magic to me. Did you try the solutions from here?

I would also suggest posting your question at watir-general.

Haddington answered 16/2, 2010 at 11:49 Comment(1)
Using solution #3 worked for me in Željko second link above. I modified the startClicker method slightly to take a browser variable parameter, but this was the one that finally worked.Arborization
N
0

I think your fx.button(:id, "OK").click was waiting state changed.
But javascript dialog does not change state.
So your watir will be waiting forever.
If not like that,I do not know.

The action will not change state, never return it.
So it needs click no wait.
When I use watir(not firewatir), @ie.button(:id, 'OK').click_no_wait.
Then better wait 1~3 second for popup.
Then as you like.
And moreover if you want control msg-box(popup), need to AutoIT. --This is sample for wait msg-box and click ok for IE popup--

autoit=WIN32OLE.new('AutoItX3.Control')
autoit.WinWait('Windows Internet Explorer')
autoit.WinActive('Windows Internet Explorer')
autoit.ControlClick('Windows Internet Explorer','','OK')

It is possible that completely I don't understand what you mean. If so ignore this.

Noncooperation answered 24/3, 2010 at 18:22 Comment(0)
U
0

Check out /var/lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html (assuming that's where your firewatir gem is installed). I ran the test and it worked for me. Maybe reading the test will give you some insight into how startClicker is supposed to work.

Undressed answered 31/3, 2010 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.