Search code examples
rubyseleniumcucumberselenium-chromedriverwatir

Ruby Selenium Automated Test Alert Window


I'm running an automated test on Ruby/Cucumber with Selenium Webdriver on Chrome and trying to delete an object, but every time I do so, an alert window pops up and asks me to confirm. the window has the buttons "ok" and "cancel". How can I click on "ok" automated?

Update from comments

Error stack trace:

unexpected alert open: {Alert text : Möchten Sie den Benutzer [email protected] wirklich löschen?} (Session info: chrome=68.0.3440.106) 
 (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 x86_64) 
 (Selenium::WebDriver::Error::UnhandledAlertError)

Solution

  • This is the code to click okay button in the alert

    driver.switch_to.alert.accept
    

    This code is to dismiss the alert

    driver.switch_to.alert.dismiss
    

    My advice is to use WATIR if you are ready to use Ruby Selenium Binding, WATIR is the nice wrapper which sits on the top of Ruby selenium binding. There are many usual errors like Element not visible, Stale element error wouldn't even arise in WATIR, WATIR has the way to rescue all these errors for the specified time. So Use WATIR if you are ready to use Ruby Selenium Binding.

    In WATIR, you could accept the alert by the following code

    b.alert.ok
    

    To dismiss the alert

    b.alert.close