Let's have a look at a couple of simple methods that we can use to validate that an automated test has either passed of failed. We're going to do this using an 'if statement'.
Checking An Element Exists
Let's write a little test that opens a browser, navigates to the test web page http://www.lazyautomation.co.uk/lazy1.html and then confirms that the 'Hello World' button exists. We can do this like so.
require "watir-webdriver" b = Watir::Browser.new :chrome b.goto 'http://www.lazyautomation.co.uk/lazy1.html' if b.button(:id, 'hello1').exists? puts 'PASS - Hello World Button Exists' else puts 'FAIL - Hello World Button is Missing' end
As you can see it's pretty easy to write, we have an 'if statement' that checks to see if the button exists using exists? and if it does then we output some text to say that the test has passed, otherwise we output some text to say that the test has failed. If you try running this it should pass. Try changing the button ID on line 6, this should stop it from finding the button and the test should fail.
Checking For Some Text
Now let's write a test that goes to the test web page, clicks the 'Hello World' button and validates that after the button has been clicked that the words 'HELLO WORLD' appear on the web page.
require "watir-webdriver" b = Watir::Browser.new :chrome b.goto 'http://www.lazyautomation.co.uk/lazy1.html' b.button(:id, 'hello1').click if b.text.include? 'HELLO WORLD' puts 'PASS - Text is displayed' else puts 'FAIL - Text was NOT displayed' end
As you can see we're navigating to the site on line 4, clicking the button on line 6 and then we have our 'if statement'. Using include? We can validate that the text is included on the web page. Try changing the text to something else e.g. 'GOODBYE WORLD' and you should see the test fail.