Web Automation with Watir
Thu, Oct 4, 2018How I use Watir to make my life easier.
What’s Watir?
“An open source Ruby library for automating tests. Watir interacts with a browser the same way people do: clicking links, filling out forms and validating text.” - Watir
A Quick Example
Goal
Log into a page and confirm success.
Setup
$ gem install pry
$ gem install watirPut chromedriver in a $PATH directory. Mine’s in /usr/local/bin
Workflow
I’ve found using a REPL like pry helps speed up development. Instead of incrementally adding steps to and rerunning my script, I can use Pry to get real-time feedback.
Starting the Browser & Navigating to a page
Initiate Pry
$ pryRequire watir, initiate a browser, and navigate to a page.
require 'watir'
$b = Watir::Browser.new
=> #<Watir::Browser:0x..f8f7739e910ec7622 url="data:," title="">
$b.goto('http://testing-ground.scraping.pro/login')Locating & Interacting with Elements
Using the DOM, I realized the user/pass fields had an ID
username_field = $b.text_field id: 'usr'
password_field = $b.text_field id: 'pwd'
sign_in = $b.button value:'Login'
username_field.set 'admin'
password_field.set '12345'
sign_in.clickReading the Page
There are various ways to do this. I’ll be getting the raw HTML of the page, which is returned as a string, and see if it includes the success element.
$b.html.include? '<h3 class="success">WELCOME :)</h3>'
=> trueYou did it!