You are on page 1of 66

Contents

License About Me and the Book Prerequisites About Watir Installation Windows 7, Vista and XP . . . . . . . . . . . . . . . . . . . . . . . . . Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RubyGems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Internet Explorer with watir . . . . . . . . . . . . . . . . . . . . . watir-webdriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . Internet Explorer with watir-webdriver . . . . . . . . . . . . . . . Firefox with watir-webdriver . . . . . . . . . . . . . . . . . . . . 1 2 3 4 6 7 7 9 9 11 12 15 17 19 20 24 27 27 28 28 29 30 31 32 34 36 37

Chrome with watir-webdriver . . . . . . . . . . . . . . . . . . . . Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Opera with watir-webdriver on 7 and Vista . . . . . . . . . . . . Opera with watir-webdriver on XP . . . . . . . . . . . . . . . . . Mac OS X 10.6 and 10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RubyGems on 10.6 . . . . . . . . . . . . . . . . . . . . . . . . . . RubyGems on 10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . Xcode 3.2 for 10.6 . . . . . . . . . . . . . . . . . . . . . . . . . . Xcode 3.1 for 10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . Xcode 4 for 10.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . Safari with safariwatir . . . . . . . . . . . . . . . . . . . . . . . . watir-webdriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . Firefox with watir-webdriver on 10.6 . . . . . . . . . . . . . . . . Firefox with watir-webdriver on 10.5 . . . . . . . . . . . . . . . .

Chrome with watir-webdriver . . . . . . . . . . . . . . . . . . . . Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Opera with watir-webdriver . . . . . . . . . . . . . . . . . . . . . Ubuntu Linux 11.04 . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RubyGems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . watir-webdriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . Firefox with watir-webdriver . . . . . . . . . . . . . . . . . . . .

38 40 41 45 45 46 47 48 50 52 54 57 65

Chrome with watir-webdriver . . . . . . . . . . . . . . . . . . . . Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Opera with watir-webdriver . . . . . . . . . . . . . . . . . . . . . Watir in Five Minutes Buy the Book

License

This work is licensed under a Creative Commons Attribution-NonCommercialShareAlike 3.0 Unported License.

About Me and the Book

Hi, my name is Zeljko Filipin. I am a software tester, the Watir community manager and host of the Watir Podcast. I am also one of the organizers of viaqa conference on software testing. This is a book on Watir. It is not nished. I am working on it. The source (markdown) will always be free at github.com/zeljkolipin/watirbook; pdf, epub and mobi versions are $9. The latest version of the book is currently available for free as pdf, epub (iPad, iPhone, iPod) and mobi (Kindle) le at github.com/zeljkolipin/watirbook/downloads. If you think there needs to be a book on Watir and I am the right person to write it, feel free to buy the book now. You will get all updates to the book for free. I promise I will not take the money, spend it on beer and never write the book. :) If the book is not nished in the near future, I will return the money. Also, if you buy the book and you do not like it, I will return the money. If you like the idea, but do not have $9, feel free to send any amount to zeljko.lipin at gmail.com via Paypal. You can also donate to the book via Flattr. Any amount is appreciated. You can discuss the book at groups.google.com/group/watirbook. Anybody can suggest book format (currently pdf, epub and mobi, but almost anything is possible) and which chapter to write next. Of course, if you buy the book and then suggest a format/chapter, your vote will count more. The next chapter will probably be installation, since a lot of people have problems with it.

Prerequisites
I will not assume that you know Ruby, but I will assume you know the basics of programming. For example, you should know what a variable is, or a loop. It is possible to write Watir tests with almost no Ruby knowledge, but the more you know Ruby, the easier it will be for you. If you grow to like Ruby, there is plenty of literature that will help you learn more. I will not assume that you know a lot about HTML, CSS, JavaScript, AJAX, DOM and stu like that, but you will have to know the basics. For example, you will have to understand what a HTML tag is, what tag attributes are and stu like that. I have not made up the prerequisites. I have just listed everything I knew when I started using Watir.

About Watir

What is this thing Watir, that I have decided to write a book about? The answer used to be pretty simple a few years ago, but it is not any more. Bret Pettichord (Watir co-creator) would say that it is family of Ruby libraries that drive browsers. (Ruby is a programming language, if you did not hear about it so far.) Watir project is a collection of open source tools (RubyGems) that drive browsers and help you automate boring and repetitive parts of functional testing of web applications, so you have more time to test with your brain. It can drive Microsoft Internet Explorer, Mozilla Firefox, Apple Safari, Google Chrome and Opera. Watir works on all major operating systems: Microsoft Windows, Apple Mac OS X and Linux. At the moment most Watir gems run on Ruby MRI (or CRuby), and Celerity runs on JRuby. Celerity is the only gem that does not drive a real browser, it emulates one. That means that it is way faster but does not use your application in a way a real user would. You should make the decision if speed or accuracy is more important to you. Of course, you can always have some tests in a real browser and some in Celerity. Watir-webdriver gem is new and shiny. It uses WebDriver (part of Selenium project) to drive browsers. Since Watir is just a family of Ruby libraries, that means Watir code is in fact Ruby code. Why is that important? Because it means you have full access to all nice Ruby features in your tests.

It is important to say that Watir CAN NOT control browser plugins like Java applets, Adobe Flash or Microsoft Silverlight. There are open source projects that can control some browser plugins. If you like Watir, but for some reason just can not use Ruby, I have good news. Watir is ported to .NET (WatiN) and Java (Watij). Watir is also an open source project. If you do not know what open source is, for now it is enough that you know that you do not have to pay anything to use it. Really, anything.

Installation
Installation is not complicated, but unfortunately, it is not trivial either. I have already said that Watir is just a piece of software written in Ruby, so to install it, you have to install Ruby rst. Since both Ruby and Watir are available on Windows, Mac and Linux and each of them has several releases currently in use (or in case of Linux, both distributions and releases), I will cover what I think is the relevant ones. I will use the most recent stable Ruby release, 1.9.2. I am installing everything in VMware Fusion 3.1.3 virtual machines, except Mac OS, since it does not want to be virtualized. Host OS for VMware Fusion is Mac OS X 10.6.8. You will probably be bored to tears if you read all installation chapters. A lot of stu is repeated. I suggest that you read only the chapters you need. Feel free to read the rest of the book in the whole.

Windows 7, Vista and XP

Windows 7 default desktop Machine is a clean installation of: Microsoft Windows 7 Professional 32-bit, Service Pack 1, Microsoft Windows Vista Ultimate 32-bit, Service Pack 1, Microsoft Windows XP Professional, Version 2002, Service Pack 3. Windows 7 and Vista machines have 1 GB RAM, XP has 512 MB. All machines are fully patched, including Internet Explorer 9 on Windows 7 and Vista. I left Internet Explorer on version 6 at XP machine, just for fun. I did not notice any dierence in installing or using Watir on any version on Windows, so I have decided to put them in one chapter. Ruby You probably do not have Ruby installed. To make sure, open command prompt and type ruby -v. On 7 and Vista open command prompt with Start > Search programs and les > cmd > Enter, and on XP with Start > Run. . . > cmd > Enter. 8

>ruby -v ruby is not recognized as an internal or external command, operable program or batch file. If you get the same thing as I did, you do not have Ruby installed. Download the latest Ruby 1.9. from rubyinstaller.org/downloads. At the moment it is Ruby 1.9.2-p290. Execute the le.

Ruby Installation You can leave all settings at default values, except at the Installation Destination and Optional Tasks screen check both Add Ruby executables to your PATH and Associate .rb and .rbw les with this Ruby installation checkboxes. Installation should take you just a few seconds. Lets check if Ruby is installed. You will have to open another command prompt, because the one you have opened does not see Ruby. >ruby -v ruby 1.9.2p290 (2011-07-09) [i386-mingw32] Congratulations! You now have the latest and greatest Ruby, 1.9.2!

RubyGems Software written in Ruby is usually distributed as RubyGems (colloquial name is gem), Ruby package manager. Sometimes Ruby installations do not have the latest versions of RubyGems, so we will rst update it. RubyGems is also a gem, (a bit recursive, right?) and we get its version with gem -v. >gem -v 1.7.2 You should update it with gem update --system: >gem update --system (...) RubyGems system software updated Ask RubyGems again for its version, just to make sure: >gem -v 1.8.10 Internet Explorer with watir

Internet Explorer 9 on Windows 7 10

Lets install Watir, nally. It is also done from the command line, the command is gem install watir. I prefer to add --no-ri --no-rdoc options, because I do not use either ri (Ruby Index) or RDoc (Ruby Documentation), and it cuts installation time to one third. So, if you want ri or Rdoc, use gem install watir, else use gem install watir --no-ri --no-rdoc. Watir gem (the whole Watir project got named after this gem) can drive Internet Explorer. It has better Internet Explorer support than watir-webdriver gem. Install it with gem install watir --no-ri --no-rdoc. >gem install watir --no-ri --no-rdoc (...) Successfully installed watir-2.0.1 13 gems installed Lets drive Internet Explorer with it: >irb > require "watir" => true > browser = Watir::Browser.new => #<Watir::IE:0x..f8169d746 url="about:blank" title=""> > browser.goto "watir.com" => 16.998912

11

watir gem drives Internet Explorer 9 on Windows 7 watir-webdriver If you are just starting with Watir, start with watir-webdriver gem. It can drive Internet Explorer, Firefox, Chrome, and Opera. Install it with gem install watir-webdriver --no-ri --no-rdoc: >gem install watir-webdriver --no-ri --no-rdoc (...) Successfully installed watir-webdriver-0.3.2 6 gems installed

12

Internet Explorer with watir-webdriver Since Internet Explorer is already installed, we will start with it. Lets see if watir-webdriver can drive Internet Explorer: >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :ie Selenium::WebDriver::Error::NoSuchDriverError: Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones. (...) On 7 and Vista I got Protected Mode must be set to the same value (enabled or disabled) for all zones error message (does not appear for Internet Explorer 6 on Windows XP) and Windows Firewall popup appeared letting me know that it has blocked C:\ruby192\bin\ruby.exe. So, XP/IE6 users can skip enabling protected mode.

Windows Firewall has blocked some features of this program For now just close the popup, lets see how to x the error message. 13

Open Internet Explorer > wrench > Internet Options > Security. There are four zones: Internet, Local intranet, Trusted sites and Restricted sites. Protected Mode is enabled by default in Internet and Restricted sites. Enable it for Local intranet and Trusted sites and close the browser.

Enable Protected Mode for all zones Let try again: >irb 14

> require "watir-webdriver" => true > browser = Watir::Browser.new :ie => #<Watir::Browser:0x..fcf3d4bb8 url="http://localhost:5555/" title="WebDriver"> > browser.goto "watir.com" => "http://watir.com/" It works!

watir-webdriver gem drives Internet Explorer 9 on Windows 7

15

Firefox with watir-webdriver

Firefox 5 on Windows 7 Can it drive Firefox? It can! (If you do not have it installed, download it from mozilla.com/refox.) >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :ff => #<Watir::Browser:0x62d8c4a6 url="about:blank" title=""> > browser.goto "watir.com" => "http://watir.com/"

16

watir-webdriver gem drives Firefox 5 on Windows 7

17

Chrome with watir-webdriver

Chrome 13 on Windows 7 Could it be that it can drive Chrome too? Lets nd out. (You can get Chrome at google.com/chrome.) >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :chrome Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. Please download the server from http://code.google.com/p/chromium/downloads/list and place it somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver. (...) Looks like there is a problem. You have to download chromedriver win32 14.0.836.0.zip (or newer version) from http://code.google.com/p/chromium/downloads/list. Unzip the le (you will get chromedriver.exe) and put it in any folder that is in your PATH. To check which folders are in PATH, open command prompt and type path: 18

>path PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Ruby192\bin Folders are separated with ;. C:\Ruby192\bin looks like a good place, so I will put chromedriver.exe there. Lets try again: >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :chrome Started ChromeDriver port=49522 => #<Watir::Browser:0x..fdbf27548 url="about:blank" title="about:blank"> > browser.goto "watir.com" => "http://watir.com/"

19

watir-webdriver gem drives Chrome 12 on Windows 7 I got Windows Firewall popup again letting me know that it has blocked C:\ruby192\bin\chromedriver.exe. Just close it for now, I have no idea what to do with it. Java To drive Opera, you will have to install Java rst. Lets check if Java is already installed with java -version: >java -version java is not recognized as an internal or external command, operable program or batch file.

20

Looks like we will have to install Java. There is big Free Java Download button at http://www.java.com/en/download. Execute download le and install Java. Lets check if Java is really installed with java -version: >java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03) Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing) Looks good to me! Opera with watir-webdriver on 7 and Vista

Opera on Windows 7 And nally, lets drive Opera. If you do not have it installed, you can get it at opera.com. >irb > require "watir-webdriver" => true

21

> browser = Watir::Browser.new :opera Selenium::WebDriver::Error::WebDriverError: Unable to find the Selenium server jar. Please download the standalone server from http://code.google.com/p/selenium/downloads/list and set the SELENIUM_SERVER_JAR environmental variable to its location. More info at http://code.google.com/p/selenium/wiki/OperaDriver. Download selenium-server-standalone-2.5.0.jar (or newer version) from http://code.google.com/p/selenium/downloads/list and put it in C:\Ruby192\bin. Then make SELENIUM SERVER JAR environmental variable and set it to C:\Ruby192\bin\selenium-server-standalone-2.5.0.jar. To create environmental variable, right click computer and click Properties > Advanced system settings > Environmental Variables > User variables > New... > Variable name: SELENIUM SERVER JAR > Variable value: C:\Ruby192\bin\selenium-server-standalone-2.5.0.jar > OK > OK > OK. Open new command prompt, the old one will not see SELENIUM SERVER JAR variable. >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :opera Selenium::WebDriver::Error::UnhandledError: No response in a timely fashion. Build info: version: 2.3.0, revision: 13158, time: 2011-08-01 18:13:39 System info: os.name: Windows 7, os.arch: x86, os.version: 6.1, java.version: 1.6.0_26 Driver info: driver.version: OperaDriver (com.opera.core.systems.scope.exceptions.ResponseNotReceivedException) IRB said No response in a timely fashion. Then I got Windows Firewall popup. Since I do not know what to do with it, just close the popup.

22

Windows Firewall blocking Java And nally I got Opera startup error popup saying: Opera has failed to access or upgrade your profile. This may have occurred because your computer has insufficient resources available or because some files are locked by other applications. You may have to restart your computer before Opera will start again.

Opera startup error Well, rebooting did not help. Not even reinstalling Opera (and then rebooting). What did help is running command prompt as administrator. So, instead of Start > Search programs and les > cmd > Enter, right click cmd and select Run as administrator.

23

Run command prompt as administrator Finally, lets drive Opera: >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :opera => #<Watir::Browser:0x..fef436832 url="http://www.google.hr/" title="Google">

24

> browser.goto "watir.com" => "http://watir.com/"

watir-webdriver gem drives Opera 11.50 on Windows 7 Opera with watir-webdriver on XP And nally, lets drive Opera. If you do not have it installed, you can get it at opera.com. >irb > require "watir-webdriver" => true

25

> browser = Watir::Browser.new :opera Selenium::WebDriver::Error::WebDriverError: Unable to find the Selenium server jar. Please download the standalone server from http://code.google.com/p/selenium/downloads/list and set the SELENIUM_SERVER_JAR environmental variable to its location. More info at http://code.google.com/p/selenium/wiki/OperaDriver. Download selenium-server-standalone-2.5.0.jar (or newer version) from http://code.google.com/p/selenium/downloads/list and put it in C:\Ruby192\bin. Then make SELENIUM SERVER JAR environmental variable and set it to C:\Ruby192\bin\selenium-server-standalone-2.5.0.jar. To create environmental variable right click My Computer and then Properties > Advanced > Environment Variables > User variables > New > Variable name: SELENIUM SERVER JAR > Variable value: C:\Ruby192\bin\selenium-server-standalone-2.5.0.jar > OK > OK > OK Open new command prompt, the old one will not see SELENIUM SERVER JAR variable. >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :opera Selenium::WebDriver::Error::UnhandledError: Could not start the process: Cannot run program "C:\Documents and Settings\zeljko\.launcher\launcher-win32-i86pc.exe": CreateProcess error=14001, This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem Build info: version: 2.5.0, revision: 13516, time: 2011-08-23 18:29:57 System info: os.name: Windows XP, os.arch: x86, os.version: 5.1, java.version: 1.6.0_26 Driver info: driver.version: OperaDriver (org.openqa.selenium.WebDriverException) I got Windows Firewall popup. Since I do not know what to do with it, just close the popup.

26

Windows Firewall blocking Java I also got This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem error message. Well, rebooting did not help. Not even reinstalling Opera (and then rebooting). What did help is installing Microsoft Visual C++ 2008 Redistributable Package (x86). Finally, lets drive Opera: >irb > require "watir-webdriver" => true > browser = Watir::Browser.new :opera => #<Watir::Browser:0x..fef436832 url="http://www.google.hr/" title="Google"> > browser.goto "watir.com" => "http://watir.com/"

27

Mac OS X 10.6 and 10.5

Mac OS X 10.6 default desktop Machine is a clean installation of Mac OS X 10.6.8 or 10.5.8, fully patched, with 4 GB RAM. 10.6 has Safari 5.1 and 10.5 has Safari 5.0.6. Firefox is 6.0.1, Chrome 13, Opera 11.51. All browsers are English (US) version. Ruby Regarding installing Ruby on Mac, I have good news and bad news. Good news is that Ruby is already installed by default, so you can skip this step if you just want to try Watir. Bad news is that you have an old version of Ruby installed. It will be good enough for trying Watir, but if you decide you want to use it, you will probably want to install a newer version. So, for now, I will skip Ruby installation. I will cover it later. To check if Ruby is installed on your Mac, open Terminal application (located in /Applications/Utilities) any type ruby -v. On 10.6 you should get this: $ ruby -v ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

28

Ruby 1.8.7 patchlevel 174 (released June 12, 2009). That is actually not so bad. On 10.5 you should get this: $ ruby -v ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0] Ruby 1.8.6 is pretty old, as the date says (2009). Does not matter at the moment, you can install newer version of Ruby later. RubyGems on 10.6 With RubyGems, the same story as with Ruby. It is already installed, but an old version. Lets see which version is here with gem -v: $ gem -v 1.3.5 1.3.5 is pretty old. Watir needs never version. Fortunately, it is easy to upgrade RubyGems with sudo gem update --system: $ sudo gem update --system (...) RubyGems 1.8.10 installed (...) RubyGems on 10.5 With RubyGems, the same story as with Ruby. It is already installed, but an old version. Lets see which version is here with gem -v: $ gem -v 1.0.1 RubyGems 1.0.1 is pretty old. Watir needs never version. Fortunately, it should be easy to upgrade RubyGems with sudo gem update --system: $ sudo gem update --system (...) Updating RubyGems... ERROR: While executing gem ... (Gem::RemoteSourceException) HTTP Response 302 fetching http://gems.rubyforge.org/yaml

29

Now you see why I said should be easy. RubyGems 1.0.1 is so old, it could not even be updated the easy way. See how lucky you are to have this book? Of course there is a way to upgrade to the newer version of RubyGems. The latest version I could install or Ruby 1.8.6 is RubyGems 1.4.2. Download rubygemsupdate1.4.2.gem (click Download link), open Terminal window in folder where you have downloaded it (/Users/zeljko/Downloads in my case) and install it: $ sudo gem install -l rubygems-update-1.4.2.gem Successfully installed rubygems-update-1.4.2 1 gem installed Installing ri documentation for rubygems-update-1.4.2... Installing RDoc documentation for rubygems-update-1.4.2... $ sudo update_rubygems RubyGems 1.4.2 installed File not found: README Ignore File not found: dated: $ gem -v 1.4.2 Everything is as it should be. Xcode 3.2 for 10.6 Xcode 3.2 does not work on Mac OS 10.5. It works on 10.6. You will need Xcode from Apple if you want to install watir-webdriver or safariwatir gems. The easiest way to install Xcode is from Mac OS DVD. Insert the DVD, go to Optional Installs folder and double click Xcode.mpkg le. README error message. Check if RubyGems is up-

30

Install Xcode 3.2 from Mac OS DVD You can get a newer version from Apple web site. You will need Apple ID. It is username and password you already use for iTunes Store or Mac App Store. If you do not have Apple ID, you can create one for free. Sign in with your Apple ID at Xcode site and on the bottom right you will see Looking for Xcode 3? Download Now. Download Xcode 3.2.6 and iOS SDK 4.3 (Disk Image). Please notice it is 4.1 GB. You can get the newest Xcode 3.2 without Apple ID and without downloading 4 GB le. Install Xcode 3.2 from Mac OS DVD and run Software Update. It will update Xcode to 3.2.6, and downloaded le will be just about 600 MB. Xcode 3.1 for 10.5 Xcode 3.1 is for Mac OS 10.5. On 10.6 install Xcode 3.2 or Xcode 4. You will need Xcode from Apple for watir-webdriver and safariwatir gems. The easiest way to install Xcode is from Mac OS DVD. Insert the DVD, go to Optional Installs/Xcode Tools folder and double click XcodeTools le. Leave all settings at default values.

31

Install Xcode 3.1.2 from Mac OS DVD Optionally, after Xcode is installed, run Software Update. At the moment, there is no update. If you do not have Mac OS 10.5 DVD, you can download Xcode 3.1.4 from Apple. Log in to http://connect.apple.com/ with your Apple ID. Click Developer Tools link (at the right hand side, under Downloads) and download Xcode 3.1.4 Developer DVD (Disk Image). Please note it is 993 MB. Xcode 4 for 10.6 Xcode 4 does not work on Mac OS 10.5. It works on 10.6. According to Xcode site, Xcode 4 is a free download for all members of the iOS and Mac Developer Programs. Both programs are $99/year. If you are not a member of mentioned programs (I am not), you can buy it from Mac App Store for $4.99. But, since you can install Xcode 3 for free, there is no need to buy anything.

32

Safari with safariwatir

Safari 5.1 on Mac 10.6 Safari is installed by default on Mac OS X, so you do not have to install it. You do have to install Xcode 3 or 4 (see previous chapters). If you do not have Xcode installed, you will get this error message if you try to install safariwatir: $ sudo gem install safariwatir --no-ri --no-rdoc (...) Fetching: rb-appscript-0.6.1.gem (100%) Building native extensions. This could take a while... ERROR: Error installing safariwatir: ERROR: Failed to build gem native extension. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ ruby extconf.rb mkmf.rb cant find header files for ruby at /System/Library/ Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/ rb-appscript-0.6.1 for inspection. Results logged to /Library/Ruby/Gems/1.8/gems/rb-appscript-0.6.1/ 33

./gem_make.out Install Xcode 3 or 4 and try again: $ sudo gem install safariwatir --no-ri --no-rdoc (...) Successfully installed safariwatir-0.4.0 (...) Lets try safariwatir. $ irb > require "safariwatir" => true > browser = Watir::Safari.new => #<Watir::Safari:0x10116fc68 @scripter=#<Watir::AppleScripter:0x10116fbf0 @js=#<Watir::JavaScripter:0x10116fc18>, @typing_lag=0.08, @app=app("/Applications/Safari.app"), @document=app("/Applications/Safari.app").documents[1], @appname="Safari">> > browser.goto "http://watir.com" => nil It works! :)

34

safariwatir gem driving Safari 5 on Mac OS 10.6 watir-webdriver Lets try watir-webdriver gem. For now it can drive Firefox, Chrome and Opera. It should also be able to drive Safari in the future. Install it with sudo gem install watir-webdriver --no-ri --no-rdoc. If you did not install Xcode, you will get this: $ sudo gem install watir-webdriver --no-ri --no-rdoc (...) Fetching: ffi-1.0.7.gem (100%) (...)

35

Building native extensions. This could take a while... ERROR: Error installing watir-webdriver: ERROR: Failed to build gem native extension. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb mkmf.rb cant find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/ffi-1.0.7 for inspection. Results logged to /Library/Ruby/Gems/1.8/gems/ffi-1.0.7/ext/ffi_c/gem_make.out Fortunately, it is easy to x. Install Xcode 3 or 4 (see previous chapters). Try again: $ sudo gem install watir-webdriver --no-ri --no-rdoc (...) Successfully installed watir-webdriver-0.3.2 (...)

36

Firefox with watir-webdriver on 10.6

Firefox 6 on Mac OS 10.6 Lets try to drive Firefox! To drive Firefox, make sure you have it installed. Open our old friend IRB and type this: $ irb > require "watir-webdriver" => true > browser = Watir::Browser.new :ff => #<Watir::Browser:0x10101ee40 url="about:blank" title=""> > browser.goto "watir.com" => "http://watir.com/" Great! We can drive Firefox.

37

watir-webdriver driving Firefox 4 on Mac OS 10.6 Firefox with watir-webdriver on 10.5 There is a problem with driving Firefox 4+ on 10.5: $ irb > require "watir-webdriver" => true

> browser = Watir::Browser.new :ff Selenium::WebDriver::Error::WebDriverError: unable to start Firefox cleanly, args: ["-silent (...)

38

Take a look at https://github.com/jnicklas/capybara/issues/313 for more detail. The easiest way to x it is to uninstall the current version of Firefox, and install Firefox 3.6.22. I could not nd how to download Firefox 4 or 5, so I did not test with them. Chrome with watir-webdriver

Chrome 13 on Mac OS 10.6 Lets see if it can really drive Chrome too. To drive Chrome, make sure you have it installed. $ irb > require "watir-webdriver" => true > browser = Watir::Browser.new :chrome Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. Please download the server from http://code.google.com/p/chromium/downloads/list and place it 39

somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver. Looks like we have to install something called chromedriver executable. Fortunately, the error message is pretty clear. Download chromedriver mac 14.0.836.0.zip (or newer version, the description should be ChromeDriver server for Mac OS X ) from http://code.google.com/p/chromium/downloads/list and unzip it (with mouse double-click, for example). You will get a le named chromedriver. Put it somewhere on your PATH, as the error message said. If you have no idea what that means, read on. To nd out where to put chromedriver le, type this in Terminal: $ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin So, in my case, these folders are on my PATH: /usr/bin, /bin, /usr/sbin, /sbin, /usr/local/bin and /usr/X11/bin. usr/bin looks like a good place to put chromedriver le. To open the folder, go to Finder > Go > Go to Folder > /usr/bin > Go. Copy the le there. You will have to provide admin password. Lets try again: $ irb > require "watir-webdriver" => true > browser = Watir::Browser.new :chrome => #<Watir::Browser:0x12d610c url="about:blank" title="about:blank"> > browser.goto "watir.com" => "http://watir.com/" Finally! It works!

40

watir-webdriver driving Chrome 12 on Mac OS 10.5 Java To drive Opera, you need Java. Fortunatelly, it is already installed. All you have to do is check if it is installed with java -version: On 10.6 you should get this: $ java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03-384-10M3425) Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-384, mixed mode) On 10.5 you should get this:

41

java version "1.5.0_30" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_30-b03-389-9M3425) Java HotSpot(TM) Client VM (build 1.5.0_30-161, mixed mode) Opera with watir-webdriver

Opera 11.51 on Mac 10.6 Lets see how it drives Opera. To drive Opera, make sure you have it installed. $ irb > require "watir-webdriver" => true > browser = Watir::Browser.new :opera Selenium::WebDriver::Error::WebDriverError: Unable to find the Selenium server jar. Please download the standalone server from http://code.google.com/p/selenium/downloads/list and set the SELENIUM_SERVER_JAR environmental variable to its location. More info at http://code.google.com/p/selenium/wiki/OperaDriver.

42

Error message similar to the one when we rst tried to open Chrome. The solution is similar too. We have to download a le, put it somewhere and point a variable to it. Do not worry, it sounds more complicated than it really is. Fortunately again, the error message says it all. Go to http://code.google.com/p/selenium/downloads/list and download selenium-server-standalone-2.5.0.jar (or newer version). Since I have put chromedriver le in /usr/bin, I will put selenium-server-standalone-2.5.0.jar le there too. You will have to provide admin password when copying the le. The last step is setting SELENIUM SERVER JAR environmental variable. Create (if the le does not exist) or edit .bash profile le in your home folder (/Users/zeljko in my case) with your favorite text editor. Add this line to the le: export SELENIUM_SERVER_JAR=/usr/bin/selenium-server-standalone-2.5.0.jar If you just got lost, I have step by step guide how to do it. Open new Terminal tab or window (it should open in your home folder by default, /Users/zeljko in my case) and type nano .bash profile: $ nano .bash_profile GNU nano text editor will open. Paste (cmd-v, for example) export SELENIUM SERVER JAR... line. Exit GNU nano and save the le with control+x. Press y when it asks Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES)? and press Enter when it displays File Name to Write: .bash profile or Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES)? (text is dierent if the le already exists or not).

43

GNU nano asking should it save changes to .bash profile le If you have done everything right, GNU nano will close and you will see normal Terminal window. We can check if the line is written to .bash profile le: $ cat .bash_profile export SELENIUM_SERVER_JAR=/usr/bin/selenium-server-standalone-2.5.0.jar Lets drive Opera, nally! Open new Terminal window or tab (this is important, already opened windows or tabs would not see SELENIUM SERVER JAR variable). $ irb > require "watir-webdriver" => true > browser = Watir::Browser.new :opera #<Watir::Browser:0x1017f6a28 url="opera:debug" title="Connect to Debugger">

44

watir-webdriver gem drives Opera 11.51 on Mac 10.6

45

Ubuntu Linux 11.04

Ubuntu Linux 11.04 default desktop Machine is a clean installation of Ubuntu Linux 11.04, fully patched, 512 MB RAM. Firefox 5.0, Chrome 12, Opera 11.50. Ruby Lets see if Ubuntu comes with Ruby installed. Open Terminal (Applications > Accessories > Terminal ) and type ruby -v: $ ruby -v The program ruby is currently not installed. You can install it by typing: sudo apt-get install ruby Install it with sudo apt-get install ruby: $ sudo apt-get install ruby (...) Setting up libruby1.8 (1.8.7.302-2) ... 46

Setting up ruby1.8 (1.8.7.302-2) ... Setting up ruby (4.5) ... (...) Check the version with ruby -v: $ ruby -v ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux] Ruby 1.8.7 is pretty good, but 1.9.2 would be better. Since we just want to get Watir installed the easiest possible way, we will user Ruby 1.8.7. RubyGems Lets see the version of RubyGems we got with Ruby with gem -v: $ gem -v The program gem is currently not installed. You can install it by typing: sudo apt-get install rubygems1.8 Looks like we did not get any version installed. We will install RubyGems with apt-get, since that is the easiest way: $ sudo apt-get install rubygems1.8 (...) Setting up ruby1.8-dev (1.8.7.302-2) ... Setting up rubygems1.8 (1.3.7-3) ... Ask RubyGems for its version with gem -v: $ gem -v 1.3.7 RubyGems 1.3.7 is really old (May 13, 2010). Lets try to update it the usual way, with gem update --system: $ gem update --system ERROR: While executing gem ... (RuntimeError) gem update --system is disabled on Debian, because it will overwrite the content of the rubygems Debian package, and might break your Debian system in subtle ways. The 47

Debian-supported way to update rubygems is through apt-get, using Debian official repositories. If you really know what you are doing, you can still update rubygems by setting the REALLY_GEM_UPDATE_SYSTEM environment variable, but please remember that this is completely unsupported by Debian. Since I do not really know what I am doing, I will leave RubyGems at version 1.3.7 and hope everything will work. watir-webdriver I think that Watir-webdriver gem is the future of Watir. In short, it can drive Firefox, Chrome and Opera. Install watir-webdriver --no-ri --no-rdoc. gem with sudo gem install watir-webdriver

$ sudo gem install watir-webdriver --no-ri --no-rdoc (...) Successfully installed watir-webdriver-0.2.8 4 gems installed

48

Firefox with watir-webdriver

Firefox on Ubuntu 11.04 Since Firefox is installed by default on Ubuntu, you do not have to install it. Lets check if it can drive Firefox: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.new :ff => #<Watir::Browser:0x..fb734a4d8 url="about:blank" title=""> > browser.goto "watir.com" => "http://watir.com/"

49

Watir-webdriver drives Firefox on Ubuntu 11.04 No problem here, works just ne.

50

Chrome with watir-webdriver

Chrome on Ubuntu 11.04 Now, lets see if it can really drive Chrome too. Ubuntu does not have Chrome installed by default, so you have to install it yourself. Download it from google.com/chrome. After installation Chrome will appear at Applications > Internet > Chrome. $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.new :chrome Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. Please download the server from http://code.google.com/p/chromium/downloads/list and place it somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver. 51

(...) Looks like we have to install something called chromedriver executable. Fortunately, the error message is pretty clear. Download chromedriver linux32 14.0.836.0.zip (or newer version, the description should be ChromeDriver server for linux32 ) from http://code.google.com/p/chromium/downloads/list and unzip it (mouse right click and then Extract Here, for example). You will get a le named chromedriver. Put it somewhere on your PATH, as the error message said. The easiest way to do it on Ubuntu is to create a folder called bin in your home folder (/home/zeljko/bin in my case). You have to reboot (or at least log out and then log in, but I have not checked that) and by some magic (provided by /home/zeljko/.profile le in my case) /home/zeljko/bin folder will appear in your PATH: $ echo $PATH /home/zeljko/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin: /sbin:/bin:/usr/games Lets drive Chrome, nally: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.new :chrome => #<Watir::Browser:0x..fb743b7d4 url="about:blank" title="about:blank"> > browser.goto "watir.com" => "http://watir.com/" And it really works!

52

Watir-webdriver drives Chrome on Ubuntu 11.04 Java To drive Opera, you will have to install Java rst. Lets check if Java is already installed with java -version: $ java The program java can be found in the following packages: * gcj-4.4-jre-headless * gcj-4.5-jre-headless * openjdk-6-jre-headless 53

Try: sudo apt-get install <selected package> Looks like we will have to install Java. Install it with sudo apt-get install openjdk-6-jre-headless: $ sudo apt-get install openjdk-6-jre-headless (...) Setting up openjdk-6-jre-lib (6b22-1.10.2-0ubuntu1~11.04.1) ... Setting up icedtea-6-jre-cacao (6b22-1.10.2-0ubuntu1~11.04.1) ... Setting up icedtea-6-jre-jamvm (6b22-1.10.2-0ubuntu1~11.04.1) ... Setting up ca-certificates-java (20100412) ... creating /etc/ssl/certs/java/cacerts... done. Lets check if Java is really installed with java -version: $ java -version java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.2) (6b22-1.10.2-0ubuntu1~11.04.1) OpenJDK Client VM (build 20.0-b11, mixed mode, sharing) Looks good to me!

54

Opera with watir-webdriver

Opera on Ubuntu 11.04 To drive Opera make sure you have it installed. Lets see how it drives Opera. Open our old friend, IRB: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.new :opera Selenium::WebDriver::Error::WebDriverError: Unable to find the Selenium server jar. Please download the standalone server from http://code.google.com/p/selenium/downloads/list and set the SELENIUM_SERVER_JAR environmental variable to its location. More info at http://code.google.com/p/selenium/wiki/OperaDriver. (...) 55

Error message similar to the one when we rst tried to open Chrome. The solution is similar too. We have to download a le, put it somewhere and point a variable to it. Do not worry, it sounds more complicated than it really is. Fortunately again, the error message says it all. Go to http://code.google.com/p/selenium/downloads/list and download selenium-server-standalone-2.5.0.jar (or newer version, the description should be Use this if you want to use the Selenium RC or Remote WebDriver or use Grid 2 without needing any additional dependencies). Since I have put chromedriver le in /home/zeljko/bin, I will put this le there too. The last step is setting SELENIUM SERVER JAR environmental variable. If you just got lost, I have step by step guide how to do it. Open Nautilus and go to your home folder: Places > Home Folder. You want to edit .bashrc le, but by default les that have names starting with dot are not displayed. To see the le go to View > Show Hidden Files. Doubleclick .bashrc le (it will open the le in gedit editor). Add this line to the le: export SELENIUM_SERVER_JAR=~/bin/selenium-server-standalone-2.5.0.jar Save the le and close all Terminal windows. Open Terminal again. To check if the variable is set, try printenv | grep SELENIUM: $ printenv | grep SELENIUM SELENIUM_SERVER_JAR=/home/zeljko/bin/selenium-server-standalone-2.5.0.jar Looks good to me! After all this work, enjoy driving Opera: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.new :opera => #<Watir::Browser:0x..fb72fd854 url="http://watir.com/" title="Watir"> > browser.goto "watir.com" => "http://watir.com/"

56

Watir-webdriver drives Opera on Ubuntu 11.04

57

Watir in Five Minutes


In this chapter I assume you have Ruby, RubyGems, watir-webdriver gem and Firefox browser installed, since that is the combination available on all operating systems. If you do not have them installed, please see Installation chapter. An example would be handy right about now Brian Marick would say. Five minutes from now, you will be crazy about Watir. If not, maybe it is not the right tool for you. Even then, if you continue reading the book, I hope you will grow to like it the more you know about it. In the rest of the book, I will explain all Watir functionality in great (maybe even painful) detail, but for now, I just want to show o a few cool features to get you excited about it. I still remember how pleasantly surprised I was the rst time I saw Watir at work. I installed it, and in a few hours I was able to create a script that would log me into the web application I was testing. Since you have this book you will be able to log into your web site in minutes, not hours. If you are familiar with Ruby, I am sure you already think IRB is one of the greatest tools for learning a new Ruby library. If you are new to Ruby, you are probably thinking: What is this IRB thing? IRB (in this case) does not stand for International Rugby Board or Immigration or Refugee Board (of Canada). It stands for Interactive Ruby Shell. Think of it as a shell that knows Ruby (as the name says). To start IRB, just type irb in command line. You will see something like this: $ irb > Now you can enter any Ruby command and you will immediately get a result. We will start with telling Ruby that we want to use watir-webdriver gem with require "watir-webdriver": You should see something like this: > require "watir-webdriver" => true If you get something like this, do not panic: > require "watir-webdriver" LoadError: no such file to load -- watir-webdriver from (irb):1:in require from (irb):1 58

It just means you have to require RubyGems rst. In that case, do this: > require "rubygems" => true > require "watir-webdriver" => true Every Ruby command returns something. After require "rubygems" you will get either true or false, depending how your Ruby installation is set up. You do not have to care about that right now. You should get => true after require "watir-webdriver". There are two parts in the returned line. The rst one is =>. It looks like an arrow. It means Ruby returned this. The second part is true, the thing that is actually returned. When true is returned, it usually means that everything is ne. Unless I say dierently, just ignore what is returned, for now. And now the magic starts. With just one command you will open Firefox. browser = Watir::Browser.new :ff

59

watir-webdriver driving Firefox 6 on Mac OS 10.6 When I saw the browser magically appearing for the rst time, I started singing (with apologies to Foreigner): Ive been waiting for a tool like you to come into my life. . . Please open just one browser. It will be enough. You can play with other browsers later. Output should be similar to this: > browser = Watir::Browser.new :ff => #<Watir::Browser:0x2ed1f1cd5b186306 url="about:blank" title=""> As I said earlier, you can ignore #<Watir::Browser...>. Opening Firefox returned the browser as an object, and this is textual representation of the object.

60

Just opening a browser is cool, but not so useful. Watir can do much more. For example, it can navigate the browser to any site. I will use google.com in this example. I suggest that you literally follow the example, and then try a few sites yourself. So, go to google.com: > browser.goto "http://www.google.com/" => "http://www.google.hr/" And google.com opens. Magic, isnt it? Controlling the browser is really useful but, as I am sure you already know, there is more to testing than just performing the actions. You have to check what happened after the action. What happens when I enter a URL in browser address bar, when I click a link or a button, when I enter some text in a text eld or select something from select box. . . ? This is the rst time we will perform a check. It is also the rst time we will take a look what Ruby returns after the command. Lets check if the browser really opened google.com. > browser.url => "http://www.google.hr/" It really works! Ruby returned a string (the thing in double quotes) that contains the text from the browser address bar. Since I am in Croatia, google.hr opened. If you are not in the US, some other Google site could open. Time to click on a link. It is easy to explicitly say which link to click on. Right now I want to click on a link with the text Google.com in English. It gets just a bit more complicated if there are two links with the same text on the page, but we will deal with that later. If your browser already opened google.com, ignore this step. > browser.link(:text => "Google.com in English").click => [] And google.com opens. Before we click another link, I want to show o one of Watirs killer features. It is called ash. Real world web applications are complex, and sometimes when you are developing a new test or debugging an existing one, you want to make sure you are interacting with the correct element. Try this (and look at Images link in the top-left corner of google.com):

61

> browser.link(:text => "Images").flash => 10

62

Link Images changes background from white to red a few times. Did you see the link ashing? Its background color changes to red a few times. Isnt that cool? I use it all the time when I talk about Watir (at conferences, for example). I think Watir is just great for presentations. It is very visual. If you did not see the ash (it ashes just for a short time), execute the command a few more times. To execute the same command in command prompt again, just click up arrow key on your keyboard and the last command that you have typed will appear. It is time to click the link: > browser.link(:text => "Images").click => [] This time, lets check the page title. > browser.title => "Google Images" We got back the string with the page title. Lets search for something. This will enter book in search text eld: 63

> browser.text_field(:name => "q").set "book" => ["book"] Maybe you are wondering how I knew the text eld had the value of name attribute set to q (I am talking about :name => "q"). If you do not know how to inspect pages, read on. I will explain it later. Now, click Search Images button: > browser.button(:value => "Search Images").click => [] Page with search results will open. Lets check how many images are on the page. (You might get a dierent number, it does not have to be 250.) > browser.images.size => 250 And nally, lets close the browser. > browser.close => true Well, that was a lot of fun. But you do not want to type into IRB all the time. You want to run the tests and do something else while it runs. As for almost everything else in Ruby and Watir, there is a simple solution. Paste all code you have entered in IRB in a text le, and save it with rb extension. IRB is used only for development or debugging, so do not paste irb as the rst line of the le. The le should look like this: require "watir-webdriver" browser = Watir::Browser.new :ff browser.goto "http://www.google.com/" browser.url browser.link(:text => "Google.com in English").click browser.link(:text => "Images").click browser.title browser.text_field(:name => "q").set "book" browser.button(:value => "Search Images").click browser.images.size browser.close Add require "rubygems" at the top of the le if you had to type it in IRB.

64

You can use any text editor to edit the le. I use RubyMine or NetBeans. Save the le as watir5.rb. If IRB is still running in your command prompt, enter exit to return to normal command prompt, or open a new command prompt. You can remove clicking Google.com in English link if Firefox opens google.com automatically for you. To run the le, navigate in command prompt to the folder where you have saved it and run it with ruby watir5.rb: $ ruby watir5.rb Smile while the browser clicks around. What is the output in the command prompt? Nothing? Yes, nothing. IRB displays values that Ruby returns, but when you execute Ruby le from the command line, it does not display the values Ruby returns. You have to explicitly say to Ruby that you want them displayed. It is as easy as adding puts in front of the command. Modify the script to look like this (you can add puts in front of every command, but you really do not care about what some commands return): require "watir-webdriver" browser = Watir::Browser.new :ff browser.goto "http://www.google.com/" puts browser.url browser.link(:text => "Google.com in English").click browser.link(:text => "Images").click puts browser.title browser.text_field(:name => "q").set "book" browser.button(:value => "Search Images").click puts browser.images.size browser.close Run the script. This time the output should look like this: $ ruby watir5.rb http://www.google.hr/ Google Images 246 Later I will show you how to make cool looking reports. If you are not impressed by now, you probably never will. If you liked what you saw so far, it is time to bring on heavy artillery. Lets go deeper.

65

Buy the Book


You are reading free version of the Watir book. If you liked the book so far, you can continue reading at github.com/zeljkolipin/watirbook. You can also buy the book for $9 via PayPal by clicking this link: buy the book. If you are reading buy the book in EPUB or MOBI format, the link will probably not work. (It should work in PDF and HTML format.) Please go to watir.com/book and click buy the book link there. When you buy the book, I will send it to you in PDF, EPUB (iPad, iPhone, iPod) and MOBI (Kindle) format via e-mail. Since the book is not nished yet, you will get all updates to the book for free.

66

You might also like