Monday, October 1, 2012

JavaScript Dialogs with Web Drivers


JavaScript dialogs are fairly common in web applications.Watir and Watir-Webdriver have different methods for dealing with JS dialogs.

Webdriver has an Alerts API, and Watir-Webdriver has an alerts helper which makes use of that, with a few different methods in it to deal with various styles of javascript popups.Below are the commands to tackle different dialogs using web drivers


JavaScript Alerts
# Check if alert is shown
browser.alert.exists?

# Get text of alert
browser.alert.text

# Close alert
browser.alert.ok
browser.alert.close

JavaScript Confirms
# Accept confirm
browser.alert.ok

# Cancel confirm
browser.alert.close

JavaScript Prompt
# Enter text to prompt
browser.alert.set "Prompt answer"

# Accept prompt
browser.alert.ok

# Cancel prompt
browser.alert.close

If you are not on web drivers then you can use traditional .click and no_wait variants to get rid of them.