In this chapter we will understand how to handle alerts using Watir.
browser.alert.exists? browser.alert.ok browser.alert.close
<html> <head> <title>Testing Alerts Using Watir</title> </head> <body> <script type = "text/javascript"> function wsformsubmitted() { alert("Button is Clicked !"); } </script> <button id = "btnsubmit" onclick = "wsformsubmitted();">Submit</button> </body> </html>
require 'watir' b = Watir::Browser.new :chrome b.goto('http://localhost/uitesting/testalert.html') b.button(id: 'btnsubmit').click b.alert.ok b.screenshot.save 'alerttest.png'
The output alerttest.png is shown here −