Here’s a cheeky bit of code that’ll help you to dismiss an alert box. If you don’t know what an alert box is then head to http://lazyautomation.co.uk/lazy4.html and click the ‘Click Me’ button to see one in action.
Now let’s automate it! The following code will open a browser and navigate to the same test page, click the button, and then dismiss the alert box. Then as a bonus, it’ll also grab the text from the alert and output it to the console just in case we need to know what it said.
require "watir-webdriver"
b = Watir::Browser.new :chrome
b.goto 'http://www.lazyautomation.co.uk/lazy4.html'
b.button(:id, 'button1').click
sleep(3) #I've put this sleep here just so you can actually see the alert before it gets dismissed
puts "Alert displayed saying: " + b.alert.text
b.alert.ok
Leave a Reply