Ruby Watir: Clicking OK on JavaScript Alerts?

99封情书 提交于 2019-12-04 03:01:55

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.

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

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

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!