0

So I want to wait until an alert is displayed, and then the usual switch to alert and get text

The solution in Java has been provided Selenium 2 (WebDriver): check that text exists before calling Alert.getText()

Currently I use sleep but everyone knows how bad that is.

This is what i have -

sleep 3
a = $driver.switch_to.alert
begin
  a.text == 'Your alert here.'
rescue
  verify { assert_match "true","false","The alert failed to be displayed"}
end
a.accept

I am looking for something like

$wait.until { a = $driver.switch_to.alert }
begin
  a.text == 'Your alert here.'
rescue
  verify { assert_match "true","false","The alert failed to be displayed"}
end
a.accept

But that does not work, it basically ignores the wait (I have a 30 second wait btw). As I mentioned, I need to know how to do it in RUBY - Test Unit

Community
  • 1
  • 1
Amey
  • 8,470
  • 9
  • 44
  • 63

2 Answers2

1

There is no method to wait for alert to appear. But you can use the below code to wait till the alert appears. It will come out of the loop once alert appears.

status=true
while (status)
alert = driver.switch_to.alert rescue "exception happened"
if (alert == "exception happened")
status = true
alert = "nothing happened"
else
status = false
end
end
Satheesh
  • 21
  • 1
  • thanks for your reply. This I guess would work, but would lead to another issue, of timing. I do not want an infinite loop if the alert never shows up. I want to wait for say 30 seconds and if the alert does not show up. I want to fail the test. – Amey Jul 25 '12 at 14:36
0

If you use watir and watir-webdriver you can do something like $browser.wait_unitil_present

cjapes
  • 99
  • 9