Main Content
Automed web site testing scripted in Ruby using watir-webdriver Archive - Originally posted on "The Horse's Mouth" - 2011-09-09 07:10:23 - Graham Ellis
How do you test your web site? With a visit to a few pages, or by waiting for customer feedback if something's not working? That's a very risky approach, and what you really need is a planned and systematic testing regime, with unit tests that must be passed (but will flag fails) run on a regular basis.
There are a number of tools that use Ruby scripting to describe and run tests. Both Selinium and Watir are well know names, and you can also use Cucumber on top of Watir's Webdriver to describe your tests in an English-like way that will be good for your customer to understand. At the end of yesterday's Learning to program in Ruby course, we used the watir-webdriver to automate browsing and test some of the results - choosing the Well House Consultants quotation page to check out.
I wrote my tests into a hash:
quote_checks = {'Armagh' => "Northern Ireland",
'E18 4RT' => "London - East",
"Cumbria" => "Carlisle",
'SN12 7NY' => "Well House Manor",
'66554' => "Kansas"}
That's five tests of the onsite quote system, with a check to be made that the location has been identified correctly.
I created a browser object, and this starts Firefox in the background, which is then driven by the watir-webdriver for the rest of the tests.
browser = Watir::Browser.new
Finally, I drive my browser to pages, fill in forms, submit them, and look at the results
browser.goto 'www.wellho.net/index.html'
browser.text_field(:name => 'where').set location
browser.button(:value => 'Go!').click
if browser.text =~ /Venue requested:(.*)/
The rest of code is regular ruby - you can find the complete source used to write this article [here] .
Screen output, when run:
munchkin:rs grahamellis$ irb
>> require 'onsite'
Testing On site quotation pages from home page
Passed - 66554
FAILED - 002 [Venue text not matched] Cumbria
Passed - E18 4RT
Passed - SN12 7NY
Passed - Armagh
Passed 4
FAILED 1
(Total 5)
>>
With futher output avaialble in a log file. ([sample] )
I installed the watir-webdriver on my Mac using
sudo gem install watir-webdriver
You may link to our home page (the one on which this script fills in the form to ask for a quotation) [here] , and the quotation generated for E18 - the one that's pictured above - is [here]
Testing is important! and with facilities like Cucumber, Watir, and Selenium, you can automate much of this work, with the programatic elements giving you just a whispered background commentary as tests tun correctly and then shouting at you when something doesn't work as specified / required / predicted.
Some other articles
R118 - Ruby Sources and Resources Load path, load and require in Ruby, and a change from 1.8 to 1.9 Loading Ruby classes - where does Ruby look? Sorta sorting a hash, and what if an exception is NOT thrown - Ruby This article Syncronise - software, trains, and buses. Please! Ruby collections and strings - some new examples Cheat Sheet - what do you need for Ruby on Rails? 2006 - Making business a pleasure H311 - Testing your PHP Testing new algorithms in PHP Testing code coverage (have I tested everything?) in PHP Testing classes for your PHP website with PHPUnit Some TestWise examples - helping use Ruby code to check your web site operation This article G902 - Web site techniques, utility and visibility Almost so wrong, but perhaps it's right for some? Effect on external factors on traffic to our web sites - an update Selecting RECENT and POPULAR news and trends for your web site users Well House Consultants, Well House Manor, First Great Western Coffee shop, TransWilts / 2014 web site reports Facebook marketing - early experiences How do I post automatically from a PHP script to my Twitter account? More or less back - what happened to our server the other day Web site - fully back! Helping search engines with appropriate 400 error codes TV show appearance - how does it effect your web site? An email marathon Some traps it's so easy to fall into in designing your web site Legal change - You need to obtain user consent if you use cookies on your website Short Web Addresses for Melksham QR codes with marketing logos embedded Some TestWise examples - helping use Ruby code to check your web site operation Promoting a single one of your domains on the search engines How big is a web page these days? Does the size of your pages matter? Learning more about our web site - and learning how to learn about yours Sharing the user experience - designing a form with the customer in mind Who is knocking at your web site door? Are you well set up to deal with allcomers? This article Google +1 - what is it? Finding and diverting image requests from rogue domains Looking back at www.wellho.net Making the most of critical emails - reading behind the scene Retaining web site visitors - reducing the one page wonders How to set up short and meaningfull alternative URLs Is it worth it? How to run a successful online poll / petition / survey / consultation Web site traffic - real users, or just noise? Analysing Google arrivals by country of origin Status Page / breaks of service in early December Removal of technical resources from this site Writing with our customers words Koulutus, Open Source tietokone kielillä ldning, Open Source dator sprÃ¥k ldning, Open Source dator sprÃ¥k Opplæring, Open Source datamaskinen sprÃ¥k Uddannelse, Open Source computer sprog Opleiding, Open Source computertalen Formação, Open Source computador lÃnguas Ausbildung, die Open-Source-Sprachen Formazione, Open Source computer lingue Formación, de los lenguajes de código abierto Formation, des langages Open Source How important is a front page ranking on a search engine? Static mirroring through HTTrack, wget and others Web Site Loading - experiences and some solutions shared Cooking bodies and URLs Plagarism - who is copying my pages? Making our things easier to find How to avoid duplicating web page maintainance Find the link A few of my favourite things Web Bloopers - good form design - avoiding pitfalls I have been working hard but I do not expect you noticed Which country does a search engine think you are located in? Ever had One of THOSE mornings? Who is watching you? Rapid growth leads to server move How do Google Ads work? Kiss and Book To provide external links, or not? PHP course dot co, dot uk Online hotel reservations - Melksham, Wiltshire (near Bath) Colour, Composition or Content Where in the world / country is my visitor from? Perl, PHP or Python? No - Perl AND PHP AND Python! Ongoing Image Copyright Issues, PHP and MySQL solutions Script to present commonly used images - PHP A time to update pictures Above the fold with First Great Western Stuffing content into a web page - easy maintainance What proportion of our web traffic is robots? What brought YOU to our web site? Simple but effective use of mod_rewrite (Apache httpd) From Web to Web 2 Two new pages / sites Finding resources - some pointers Sorting out for a site map Drawing dynamic graphs in PHP Above the fold Our search engine placement is dropping. Search engine placement - long term strategy and success Training on Cascading Style Sheets Santa at the station Driving customers away Visibility Effective web campaign? Finding the language preference of a web site visitor Horse and Python training Where is a web site visitor browsing from Protecting images from theft Mirroring a dynamic site Keeping the visitors happy and browsing Denial of Service ''attack'' Bigger Box Campaign Getting favicon to work - avoiding common pitfalls Dynamic Web presence - next generation web site New Navigation Aid - Launch of My Wellho Form Madness What brings people to my web site? CMS - the minefield of Choices Graveyard pages Frightening and from-friend viruses and spams More maps Ordnance Survey - using a 'Get a map' What language is this written in? Growth pains Colour blindness for web developers The Iconish language Cover all the options An apology to Mr Boneparte Our most popular resources Information request forms, cleaning up spam Putting a form online Responding to spam Who are all these visitors? Searching for numbers Allow for peak traffic on your web site Your personal Google ranking The hunt for unique words Data Mining Implementing an effective site search engine Colour for access A case of case URLs - a service and not a hurdle No more 'Error 404' pages. Something better. Web design platoon Skills and responsibilities A602 - Apache httpd - Sourcing, Installation, Testing Adding a PHP build option, rotating an image based on camera data, and a new look at thumbnails in PHP This article Global and Enable - two misused words! Choosing the right version of Java and Tomcat Where is my new Apache httpd installed Using ApacheBench and jconsole to test and monitor Tomcat Summary - Apache httpd build on Linux What is built in to this httpd and PHP? Apache httpd, MySQL, PHP - installation procedure Configuring Apache httpd Connecting to MySQL 5 from PHP on Mac OSX Leopard Upgrade Mac OSX to Leopard, Web Server Apache httpd config lost DHCP automatic IP address v Static IP Apache httpd , browser, MySQL and MySQL client downloads Notes from the white board Browser -> httpd -> Tomcat -> MySQL. Restarting. Stopping and restarting Apache httpd cleanly 2006 - Making business a pleasure Apache httpd - serving web documents from different directories Apache httpd release 2.2 R222 - Cucumber / web site testing with watir webdrivers and capybara Defining the behaviour of your web site and testing that it works Cucumber example - test::unit, scenario outlines, datafile driven test Regression Testing my website - Cucumber and Watir This article