Watir

March 30, 2018 | Author: manishkhanna3002 | Category: Selenium (Software), Xcode, Internet Explorer, Mac Os X Snow Leopard, Safari (Web Browser)


Comments



Description

ContentsLicense 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 . . . . . . . . . . . . . . . . 1 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. 2 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 finished. I am working on it. The source (markdown) will always be free at github.com/zeljkofilipin/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) file at github.com/zeljkofilipin/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 finished 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.filipin 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. 3 the easier it will be for you. you should know what a variable is.Prerequisites I will not assume that you know Ruby. or a loop. I have not made up the prerequisites. but the more you know Ruby. JavaScript. If you grow to like Ruby. 4 . I will not assume that you know a lot about HTML. there is plenty of literature that will help you learn more. DOM and stuff like that. For example. what tag attributes are and stuff like that. you will have to understand what a HTML tag is. AJAX. but you will have to know the basics. For example. I have just listed everything I knew when I started using Watir. It is possible to write Watir tests with almost no Ruby knowledge. CSS. but I will assume you know the basics of programming. At the moment most Watir gems run on Ruby MRI (or CRuby). Since Watir is “just” a family of Ruby libraries. Mozilla Firefox. 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. Google Chrome and Opera. It can drive Microsoft Internet Explorer. You should make the decision if speed or accuracy is more important to you. Watir-webdriver gem is new and shiny.About Watir What is this thing Watir. Celerity is the only gem that does not drive a real browser. Of course. Apple Mac OS X and Linux. 5 . but it is not any more. Why is that important? Because it means you have full access to all nice Ruby features in your tests. Watir works on all major operating systems: Microsoft Windows. you can always have some tests in a real browser and some in Celerity. and Celerity runs on JRuby. That means that it is way faster but does not use your application in a way a real user would. (Ruby is a programming language. Apple Safari. that means Watir code is in fact Ruby code. so you have more time to test with your brain. It uses WebDriver (part of Selenium project) to drive browsers. it emulates one. Bret Pettichord (Watir co-creator) would say that it is family of Ruby libraries that drive browsers. that I have decided to write a book about? The answer used to be pretty simple a few years ago. It is important to say that Watir CAN NOT control browser plugins like Java applets. If you do not know what open source is. anything. Adobe Flash or Microsoft Silverlight. but for some reason just can not use Ruby. for now it is enough that you know that you do not have to pay anything to use it. I have good news.NET (WatiN) and Java (Watij). Watir is also an open source project. 6 . Watir is ported to . There are open source projects that can control some browser plugins. Really. If you like Watir. I have already said that Watir is “just” a piece of software written in Ruby. so to install it.1. both distributions and releases). I suggest that you read only the chapters you need. You will probably be bored to tears if you read all installation chapters. Mac and Linux and each of them has several releases currently in use (or in case of Linux. A lot of stuff is repeated. Host OS for VMware Fusion is Mac OS X 10. Feel free to read the rest of the book in the whole.Installation Installation is not complicated.3 virtual machines. except Mac OS. 1. Since both Ruby and Watir are available on Windows. I will cover what I think is the relevant ones. I will use the most recent stable Ruby release. I am installing everything in VMware Fusion 3.8. 7 . but unfortunately. it is not trivial either. since it does not want to be virtualized.6.9. you have to install Ruby first.2. open command prompt and type ruby -v. including Internet Explorer 9 on Windows 7 and Vista. All machines are fully patched. . Windows 7 and Vista machines have 1 GB RAM. 8 . Service Pack 1. Service Pack 1. • Microsoft Windows Vista Ultimate 32-bit. XP has 512 MB. and on XP with Start > Run. just for fun. Vista and XP Windows 7 default desktop Machine is a clean installation of: • Microsoft Windows 7 Professional 32-bit. Ruby You probably do not have Ruby installed. I did not notice any difference in installing or using Watir on any version on Windows. On 7 and Vista open command prompt with Start > Search programs and files > cmd > Enter. . To make sure.Windows 7. I left Internet Explorer on version 6 at XP machine. • Microsoft Windows XP Professional. > cmd > Enter. so I have decided to put them in one chapter. Service Pack 3. Version 2002. rbw files with this Ruby installation checkboxes.2! 9 .org/downloads. because the one you have opened does not see Ruby.>ruby -v ’ruby’ is not recognized as an internal or external command. you do not have Ruby installed. At the moment it is Ruby 1. except at the Installation Destination and Optional Tasks screen check both Add Ruby executables to your PATH and Associate .9. >ruby -v ruby 1. Installation should take you just a few seconds. Ruby Installation You can leave all settings at default values. If you get the same thing as I did. Let’s check if Ruby is installed.rb and . You will have to open another command prompt.2-p290.2p290 (2011-07-09) [i386-mingw32] Congratulations! You now have the latest and greatest Ruby.9. Execute the file.9. from rubyinstaller. Download the latest Ruby 1.9. 1. operable program or batch file. ) RubyGems system software updated Ask RubyGems again for it’s version.2 You should update it with gem update --system: >gem update --system (.RubyGems Software written in Ruby is usually distributed as RubyGems (colloquial name is gem ).7.8... so we will first update it. >gem -v 1. (a bit recursive. right?) and we get it’s version with gem -v. Ruby package manager. Sometimes Ruby installations do not have the latest versions of RubyGems. RubyGems is also a gem. just to make sure: >gem -v 1.10 Internet Explorer with watir Internet Explorer 9 on Windows 7 10 . 1 13 gems installed Let’s drive Internet Explorer with it: >irb > require "watir" => true > browser = Watir::Browser.0. else use gem install watir --no-ri --no-rdoc. It has better Internet Explorer support than watir-webdriver gem. So. Watir gem (the whole Watir project got named after this gem) can drive Internet Explorer.. Install it with gem install watir --no-ri --no-rdoc.Let’s install Watir. use gem install watir. the command is gem install watir. It is also done from the command line.goto "watir.998912 11 .) Successfully installed watir-2.com" => 16. I prefer to add --no-ri --no-rdoc options. finally. if you want ri or Rdoc. >gem install watir --no-ri --no-rdoc (. because I do not use either ri (Ruby Index) or RDoc (Ruby Documentation).f8169d746 url="about:blank" title=""> > browser.. and it cuts installation time to one third..new => #<Watir::IE:0x. 2 6 gems installed 12 . start with watir-webdriver gem. It can drive Internet Explorer. and Opera. Chrome. Firefox.. Install it with gem install watir-webdriver --no-ri --no-rdoc: >gem install watir-webdriver --no-ri --no-rdoc (.3.watir gem drives Internet Explorer 9 on Windows 7 watir-webdriver If you are just starting with Watir.) Successfully installed watir-webdriver-0.. XP/IE6 users can skip enabling protected mode. Windows Firewall has blocked some features of this program For now just close the popup..exe. Let’s 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.. let’s see how to fix the error message. we will start with it. 13 .) 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.Internet Explorer with watir-webdriver Since Internet Explorer is already installed. (. So. Protected Mode must be set to the same value (enabled or disabled) for all zones. Local intranet. Enable Protected Mode for all zones Let’ try again: >irb 14 . Enable it for Local intranet and Trusted sites and close the browser. Protected Mode is enabled by default in Internet and Restricted sites. There are four zones: Internet. Trusted sites and Restricted sites.Open Internet Explorer > wrench > Internet Options > Security. .com" => "http://watir.new :ie => #<Watir::Browser:0x.goto "watir.fcf3d4bb8 url="http://localhost:5555/" title="WebDriver"> > browser.com/" It works! watir-webdriver gem drives Internet Explorer 9 on Windows 7 15 .> require "watir-webdriver" => true > browser = Watir::Browser. com/firefox.com/" 16 .com" => "http://watir.) >irb > require "watir-webdriver" => true > browser = Watir::Browser.Firefox with watir-webdriver Firefox 5 on Windows 7 Can it drive Firefox? It can! (If you do not have it installed.new :ff => #<Watir::Browser:0x62d8c4a6 url="about:blank" title=""> > browser. download it from mozilla.goto "watir. watir-webdriver gem drives Firefox 5 on Windows 7 17 . Please download the server from http://code.) Looks like there is a problem. (You can get Chrome at google.google. To check which folders are in PATH.) >irb > require "watir-webdriver" => true > browser = Watir::Browser.. open command prompt and type path: 18 .0.Chrome with watir-webdriver Chrome 13 on Windows 7 Could it be that it can drive Chrome too? Let’s find out. Unzip the file (you will get chromedriver.zip (or newer version) from http://code. (.new :chrome Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable.com/chrome. More info at http://code.0.com/p/selenium/wiki/ChromeDriver.com/p/chromium/downloads/list.google.com/p/chromium/downloads/list and place it somewhere on your PATH.exe) and put it in any folder that is in your PATH.. You have to download chromedriver win32 14.google.836. 0\.C:\Ruby192\bin Folders are separated with .com" => "http://watir..fdbf27548 url="about:blank" title="about:blank"> > browser.goto "watir.new :chrome Started ChromeDriver port=49522 => #<Watir::Browser:0x.com/" 19 .C:\Windows..>path PATH=C:\Windows\system32. Let’s try again: >irb > require "watir-webdriver" => true > browser = Watir::Browser. so I will put chromedriver.C:\Windows\System32\Wbem. C:\Windows\System32\WindowsPowerShell\v1. C:\Ruby192\bin looks like a good place.exe there. Java To drive Opera. operable program or batch file.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. I have no idea what to do with it.exe. Just close it for now. Let’s check if Java is already installed with java -version: >java -version ’java’ is not recognized as an internal or external command. you will have to install Java first. 20 . sharing) Looks good to me! Opera with watir-webdriver on 7 and Vista Opera on Windows 7 And finally. >irb > require "watir-webdriver" => true 21 .Looks like we will have to install Java. you can get it at opera.0_26" Java(TM) SE Runtime Environment (build 1. If you do not have it installed.6. Execute download file and install Java.6.com.1-b02.com/en/download.0_26-b03) Java HotSpot(TM) Client VM (build 20.java. mixed mode. There is big Free Java Download button at http://www. Let’s check if Java is really installed with java -version: >java -version java version "1. let’s drive Opera. 0. Then I got Windows Firewall popup.jar.google.jar > OK > OK > OK.new :opera Selenium::WebDriver::Error::WebDriverError: Unable to find the Selenium server jar.com/p/selenium/downloads/list and put it in C:\Ruby192\bin. More info at http://code.0.5. right click computer and click Properties > Advanced system settings > Environmental Variables > User variables > New. Open new command prompt. >irb > require "watir-webdriver" => true > browser = Watir::Browser.name: ’Windows 7’. revision: ’13158’.exceptions. java.com/p/selenium/downloads/list and set the SELENIUM_SERVER_JAR environmental variable to its location. Then make SELENIUM SERVER JAR environmental variable and set it to C:\Ruby192\bin\selenium-server-standalone-2.systems.opera. time: ’2011-08-01 18:13:39’ System info: os..version: OperaDriver (com.scope.. To create environmental variable.arch: ’x86’.core.jar (or newer version) from http://code. > Variable name: SELENIUM SERVER JAR > Variable value: C:\Ruby192\bin\selenium-server-standalone-2.com/p/selenium/wiki/OperaDriver. Download selenium-server-standalone-2.3.google. os. the old one will not see SELENIUM SERVER JAR variable.version: ’1.1’.5. 22 . Build info: version: ’2.new :opera Selenium::WebDriver::Error::UnhandledError: No response in a timely fashion.ResponseNotReceivedException) IRB said No response in a timely fashion.0’. os.google.0_26’ Driver info: driver.6.0.version: ’6. Please download the standalone server from http://code. just close the popup. Since I do not know what to do with it.> browser = Watir::Browser.5. Opera startup error Well.Windows Firewall blocking Java And finally I got Opera startup error popup saying: Opera has failed to access or upgrade your profile. rebooting did not help. 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. instead of Start > Search programs and files > cmd > Enter. Not even reinstalling Opera (and then rebooting). What did help is running command prompt as administrator. right click cmd and select Run as administrator. So. 23 . Run command prompt as administrator Finally, let’s 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 finally, let’s 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 google. rebooting did not help. Finally.fef436832 url="http://www. Well. let’s drive Opera: >irb > require "watir-webdriver" => true > browser = Watir::Browser.hr/" title="Google"> > browser. Not even reinstalling Opera (and then rebooting).com" => "http://watir.. What did help is installing Microsoft Visual C++ 2008 Redistributable Package (x86).goto "watir.Windows Firewall blocking Java I also got This application has failed to start because the application configuration is incorrect.com/" 27 .new :opera => #<Watir::Browser:0x. Reinstalling the application may fix this problem error message. Bad news is that you have an old version of Ruby installed. It will be good enough for trying Watir.6 and 10. but if you decide you want to use it. So.0.8 or 10.1 and 10.5 has Safari 5.6 has Safari 5. so you can skip this step if you just want to try Watir.0] 28 . Opera 11.5 Mac OS X 10.1.51.8. open Terminal application (located in /Applications/Utilities) any type ruby -v. Firefox is 6.7 (2009-06-12 patchlevel 174) [universal-darwin10. for now.6 you should get this: $ ruby -v ruby 1.5. Chrome 13.0. All browsers are English (US) version. Ruby Regarding installing Ruby on Mac. On 10. To check if Ruby is installed on your Mac.Mac OS X 10. 10. I will skip Ruby installation. Good news is that Ruby is already installed by default.6. with 4 GB RAM.8. fully patched.6 default desktop Machine is a clean installation of Mac OS X 10. I will cover it later. you will probably want to install a newer version.6. I have good news and bad news. .1 RubyGems 1.8.5 you should get this: $ ruby -v ruby 1..10 installed (. Let’s see which version is here with gem -v: $ gem -v 1.5 With RubyGems. the same story as with Ruby.. On 10. Watir needs never version..0] Ruby 1.) Updating RubyGems.Ruby 1.8.) RubyGems on 10. RubyGems on 10. you can install newer version of Ruby later. the same story as with Ruby.0. Let’s see which version is here with gem -v: $ gem -v 1..5 is pretty old. Fortunately. it is easy to upgrade RubyGems with sudo gem update --system: $ sudo gem update --system (. ERROR: While executing gem .3.7 patchlevel 174 (released June 12.6 With RubyGems. (Gem::RemoteSourceException) HTTP Response 302 fetching http://gems.. It is already installed. That is actually not so bad.6 (2009-06-08 patchlevel 369) [universal-darwin9...rubyforge.1 is pretty old.8.8.0.3. It is already installed. 2009).6 is pretty old. Does not matter at the moment.org/yaml 29 .5 1. but an old version. Fortunately. Watir needs never version.. it should be easy to upgrade RubyGems with sudo gem update --system: $ sudo gem update --system (.. as the date says (2009). but an old version.) RubyGems 1. gem Successfully installed rubygems-update-1.2.2.4. 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. It works on 10.6 Xcode 3..2 installed File not found: README Ignore File not found: dated: $ gem -v 1.2 for 10.mpkg file.Now you see why I said “should be easy”.4.2. Xcode 3. it could not even be updated the easy way. You will need Xcode from Apple if you want to install watir-webdriver or safariwatir gems.2 does not work on Mac OS 10. See how lucky you are to have this book? Of course there is a way to upgrade to the newer version of RubyGems. README error message.gem (click Download link). Download rubygemsupdate–1.5..4. Installing RDoc documentation for rubygems-update-1.4.6 is RubyGems 1.4. The easiest way to install Xcode is from Mac OS DVD..2. The latest version I could install or Ruby 1.4. $ sudo update_rubygems RubyGems 1.6.2 1 gem installed Installing ri documentation for rubygems-update-1. Check if RubyGems is up- 30 .1 is so old.8.. RubyGems 1. go to Optional Installs folder and double click Xcode.4.0.4.2 Everything is as it should be. Insert the DVD.2. Install Xcode 3. You can get the newest Xcode 3.2.1 GB. The easiest way to install Xcode is from Mac OS DVD. On 10.6 install Xcode 3. Leave all settings at default values. you can create one for free.3 (Disk Image). go to Optional Installs/Xcode Tools folder and double click XcodeTools file. Please notice it is 4.1 for 10.2 without Apple ID and without downloading 4 GB file. 31 . You will need Apple ID.2 from Mac OS DVD You can get a newer version from Apple web site.5.2 or Xcode 4. Xcode 3.6. Sign in with your Apple ID at Xcode site and on the bottom right you will see Looking for Xcode 3? Download Now. You will need Xcode from Apple for watir-webdriver and safariwatir gems.1 is for Mac OS 10. It will update Xcode to 3. Install Xcode 3. and downloaded file will be “just” about 600 MB.6 and iOS SDK 4. It is username and password you already use for iTunes Store or Mac App Store.2. Download Xcode 3.2 from Mac OS DVD and run Software Update. Insert the DVD. If you do not have Apple ID.5 Xcode 3. 5 DVD. since you can install Xcode 3 for free.4 Developer DVD (Disk Image).apple. 32 .99. At the moment.6. you can buy it from Mac App Store for $4. Log in to http://connect. Both programs are $99/year. you can download Xcode 3.1. Xcode 4 for 10. there is no update.5.4 from Apple.1. It works on 10.6 Xcode 4 does not work on Mac OS 10.2 from Mac OS DVD Optionally. run Software Update. there is no need to buy anything. But. Please note it is 993 MB. If you are not a member of mentioned programs (I am not). under Downloads ) and download Xcode 3. after Xcode is installed. According to Xcode site. Click Developer Tools link (at the right hand side.Install Xcode 3.com/ with your Apple ID. Xcode 4 “is a free download for all members of the iOS and Mac Developer Programs”. If you do not have Mac OS 10.1. rb mkmf. /System/Library/Frameworks/Ruby. If you do not have Xcode installed..Safari with safariwatir Safari 5.6.1 on Mac 10.8/usr/bin/ ruby extconf.6. you will get this error message if you try to install safariwatir: $ sudo gem install safariwatir --no-ri --no-rdoc (.rb can’t find header files for ruby at /System/Library/ Frameworks/Ruby. You do have to install Xcode 3 or 4 (see previous chapters).8/usr/lib/ruby/ruby.6 Safari is installed by default on Mac OS X.framework/Versions/1.gem (100%) Building native extensions.. This could take a while.8/gems/rb-appscript-0..8/gems/ rb-appscript-0.. Results logged to /Library/Ruby/Gems/1.) Fetching: rb-appscript-0.6.h Gem files will remain installed in /Library/Ruby/Gems/1.1/ 33 .framework/Versions/1. so you do not have to install it.1. ERROR: Error installing safariwatir: ERROR: Failed to build gem native extension.1 for inspection. new => #<Watir::Safari:0x10116fc68 @scripter=#<Watir::AppleScripter:0x10116fbf0 @js=#<Watir::JavaScripter:0x10116fc18>.documents[1]. @document=app("/Applications/Safari...08.) Successfully installed safariwatir-0. $ irb > require "safariwatir" => true > browser = Watir::Safari.out Install Xcode 3 or 4 and try again: $ sudo gem install safariwatir --no-ri --no-rdoc (.4.app"). @app=app("/Applications/Safari.com" => nil It works! :) 34 .0 (.app")..) Let’s try safariwatir. @appname="Safari">> > browser.../gem_make. @typing_lag=0.goto "http://watir. you will get this: $ sudo gem install watir-webdriver --no-ri --no-rdoc (.) Fetching: ffi-1.0.safariwatir gem driving Safari 5 on Mac OS 10. It should also be able to drive Safari in the future...6 watir-webdriver Let’s try watir-webdriver gem. For now it can drive Firefox. If you did not install Xcode..) 35 . Install it with sudo gem install watir-webdriver --no-ri --no-rdoc. Chrome and Opera..7.gem (100%) (. h Gem files will remain installed in /Library/Ruby/Gems/1.7/ext/ffi_c/gem_make.0.7 for inspection.. Results logged to /Library/Ruby/Gems/1.) 36 .3.out Fortunately.0..) Successfully installed watir-webdriver-0.. Install Xcode 3 or 4 (see previous chapters). Try again: $ sudo gem install watir-webdriver --no-ri --no-rdoc (.Building native extensions.8/gems/ffi-1..rb can’t find header files for ruby at /System/Library/Frameworks/Ruby. This could take a while.8/usr/bin/ruby extconf.8/usr/lib/ruby/ruby. ERROR: Error installing watir-webdriver: ERROR: Failed to build gem native extension..framework/Versions/1. /System/Library/Frameworks/Ruby..8/gems/ffi-1.rb mkmf. it is easy to fix.framework/Versions/1.2 (. 6 Firefox 6 on Mac OS 10.6 Let’s try to drive Firefox! To drive Firefox.goto "watir.com" => "http://watir.com/" Great! We can drive Firefox.new :ff => #<Watir::Browser:0x10101ee40 url="about:blank" title=""> > browser. Open our old friend IRB and type this: $ irb > require "watir-webdriver" => true > browser = Watir::Browser. 37 . make sure you have it installed.Firefox with watir-webdriver on 10. .watir-webdriver driving Firefox 4 on Mac OS 10.) 38 ..new :ff Selenium::WebDriver::Error::WebDriverError: unable to start Firefox cleanly.5: $ irb > require "watir-webdriver" => true > browser = Watir::Browser.5 There is a problem with driving Firefox 4+ on 10.6 Firefox with watir-webdriver on 10. args: ["-silent (. com/jnicklas/capybara/issues/313 for more detail.new :chrome Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. I could not find how to download Firefox 4 or 5. Please download the server from http://code. make sure you have it installed. The easiest way to fix it is to uninstall the current version of Firefox.6.google. To drive Chrome. Chrome with watir-webdriver Chrome 13 on Mac OS 10.Take a look at https://github.6 Let’s see if it can really drive Chrome too.22. $ irb > require "watir-webdriver" => true > browser = Watir::Browser.com/p/chromium/downloads/list and place it 39 . and install Firefox 3. so I did not test with them. Fortunately. read on. Looks like we have to install something called chromedriver executable.new :chrome => #<Watir::Browser:0x12d610c url="about:blank" title="about:blank"> > browser. these folders are on my PATH: /usr/bin. the error message is pretty clear. Let’s try again: $ irb > require "watir-webdriver" => true > browser = Watir::Browser. /usr/sbin. To find out where to put chromedriver file.0. Copy the file there. You will have to provide admin password.zip (or newer version.somewhere on your PATH. for example). Put it somewhere on your PATH.google.com/p/chromium/downloads/list and unzip it (with mouse double-click. /bin. More info at http://code.com/p/selenium/wiki/ChromeDriver. type this in Terminal: $ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin So.0.com/" Finally! It works! 40 . Download chromedriver mac 14.com" => "http://watir. /sbin.836. You will get a file named chromedriver. go to Finder > Go > Go to Folder > /usr/bin > Go. the description should be ChromeDriver server for Mac OS X ) from http://code. as the error message said. If you have no idea what that means. To open the folder. usr/bin looks like a good place to put chromedriver file.goto "watir.google. /usr/local/bin and /usr/X11/bin. in my case. it is already installed.6 you should get this: $ java -version java version "1.0_26" Java(TM) SE Runtime Environment (build 1. All you have to do is check if it is installed with java -version: On 10. mixed mode) On 10.5 you should get this: 41 . you need Java.6.watir-webdriver driving Chrome 12 on Mac OS 10.1-b02-384.0_26-b03-384-10M3425) Java HotSpot(TM) 64-Bit Server VM (build 20.5 Java To drive Opera.6. Fortunatelly. To drive Opera.0_30-b03-389-9M3425) Java HotSpot(TM) Client VM (build 1.5.0_30" Java(TM) 2 Runtime Environment.6 Let’s see how it drives Opera.51 on Mac 10.5.0_30-161.5.google. 42 . $ irb > require "watir-webdriver" => true > browser = Watir::Browser.google. mixed mode) Opera with watir-webdriver Opera 11. Standard Edition (build 1.com/p/selenium/wiki/OperaDriver. More info at http://code. Please download the standalone server from http://code.com/p/selenium/downloads/list and set the SELENIUM_SERVER_JAR environmental variable to its location.new :opera Selenium::WebDriver::Error::WebDriverError: Unable to find the Selenium server jar.java version "1. make sure you have it installed. 43 . /Users/zeljko in my case) and type nano . the error message says it all.0. Create (if the file does not exist) or edit . Fortunately again. it sounds more complicated than it really is.bash profile file in your home folder (/Users/zeljko in my case) with your favorite text editor.5.jar (or newer version).Error message similar to the one when we first tried to open Chrome. Press y when it asks Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES)? and press Enter when it displays File Name to Write: .jar file there too. Go to http://code.5... I have step by step guide how to do it.bash_profile GNU nano text editor will open. The last step is setting SELENIUM SERVER JAR environmental variable. We have to download a file.5.jar If you just got lost.com/p/selenium/downloads/list and download selenium-server-standalone-2. Open new Terminal tab or window (it should open in your home folder by default. I will put selenium-server-standalone-2.google. You will have to provide admin password when copying the file. Exit GNU nano and save the file with control+x. Do not worry. Add this line to the file: export SELENIUM_SERVER_JAR=/usr/bin/selenium-server-standalone-2.0. Paste (cmd-v. line. The solution is similar too.bash profile or Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES)? (text is different if the file already exists or not).bash profile: $ nano . put it somewhere and point a variable to it. for example) export SELENIUM SERVER JAR. Since I have put chromedriver file in /usr/bin.0. We can check if the line is written to .bash profile file: $ cat . $ irb > require "watir-webdriver" => true > browser = Watir::Browser.bash profile file If you have done everything right. finally! Open new Terminal window or tab (this is important.new :opera #<Watir::Browser:0x1017f6a28 url="opera:debug" title="Connect to Debugger"> 44 . GNU nano will close and you will see normal Terminal window.jar Let’s drive Opera.0.5.bash_profile export SELENIUM_SERVER_JAR=/usr/bin/selenium-server-standalone-2.GNU nano asking should it save changes to . already opened windows or tabs would not see SELENIUM SERVER JAR variable). 6 45 .watir-webdriver gem drives Opera 11.51 on Mac 10. ) Setting up libruby1.7. fully patched.0. Ruby Let’s see if Ubuntu comes with Ruby installed.. 512 MB RAM..04 default desktop Machine is a clean installation of Ubuntu Linux 11. You can install it by typing: sudo apt-get install ruby Install it with sudo apt-get install ruby: $ sudo apt-get install ruby (.8 (1.50.04 Ubuntu Linux 11. Opera 11..302-2) . Chrome 12.04. 46 .. Firefox 5. Open Terminal (Applications > Accessories > Terminal ) and type ruby -v: $ ruby -v The program ’ruby’ is currently not installed.8.Ubuntu Linux 11. . (..302-2) .8.7.. Ask RubyGems for it’s version with gem -v: $ gem -v 1.8.8.8 (1.8.8 (1.7 is pretty good.7 (2010-08-16 patchlevel 302) [i686-linux] Ruby 1.. We will install RubyGems with apt-get...7 is really old (May 13..9. RubyGems Let’s see the version of RubyGems we got with Ruby with gem -v: $ gem -v The program ’gem’ is currently not installed.8 Looks like we did not get any version installed.8 (.) Check the version with ruby -v: $ ruby -v ruby 1. Setting up rubygems1.302-2) .7.7 RubyGems 1..3. You can install it by typing: sudo apt-get install rubygems1. Setting up ruby (4. Let’s try to update it the usual way. (RuntimeError) gem update --system is disabled on Debian. The 47 . but 1.3.7. we will user Ruby 1.7-3) .8-dev (1. 2010).8.Setting up ruby1. with gem update --system: $ gem update --system ERROR: While executing gem ..3..2 would be better..) Setting up ruby1. and might break your Debian system in subtle ways.. Since we just want to get Watir installed the easiest possible way.. since that is the easiest way: $ sudo apt-get install rubygems1. because it will overwrite the content of the rubygems Debian package..5) . 8 4 gems installed 48 .3. but please remember that this is completely unsupported by Debian.Debian-supported way to update rubygems is through apt-get. using Debian official repositories.2. In short. Chrome and Opera.. it can drive Firefox.) Successfully installed watir-webdriver-0. Since I do not really know what I am doing. If you really know what you are doing.7 and hope everything will work. Install watir-webdriver --no-ri --no-rdoc. gem with sudo gem install watir-webdriver $ sudo gem install watir-webdriver --no-ri --no-rdoc (. you can still update rubygems by setting the REALLY_GEM_UPDATE_SYSTEM environment variable. I will leave RubyGems at version 1.. watir-webdriver I think that Watir-webdriver gem is the future of Watir. goto "watir. Let’s check if it can drive Firefox: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.fb734a4d8 url="about:blank" title=""> > browser..Firefox with watir-webdriver Firefox on Ubuntu 11.new :ff => #<Watir::Browser:0x.com" => "http://watir.com/" 49 . you do not have to install it.04 Since Firefox is installed by default on Ubuntu. Watir-webdriver drives Firefox on Ubuntu 11. works just fine. 50 .04 No problem here. new :chrome Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. Download it from google. Ubuntu does not have Chrome installed by default.04 Now.Chrome with watir-webdriver Chrome on Ubuntu 11. Please download the server from http://code. More info at http://code.com/p/selenium/wiki/ChromeDriver. 51 .google.com/p/chromium/downloads/list and place it somewhere on your PATH. After installation Chrome will appear at Applications > Internet > Chrome. $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.com/chrome. lets see if it can really drive Chrome too.google. so you have to install it yourself. 0.0. finally: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser. You have to reboot (or at least log out and then log in..new :chrome => #<Watir::Browser:0x.goto "watir.com/" And it really works! 52 . 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)..profile file 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 Let’s drive Chrome. as the error message said.) Looks like we have to install something called chromedriver executable..com/p/chromium/downloads/list and unzip it (mouse right click and then Extract Here.(.fb743b7d4 url="about:blank" title="about:blank"> > browser. the error message is pretty clear.google. the description should be ChromeDriver server for linux32 ) from http://code. You will get a file named chromedriver.836. Fortunately. Download chromedriver linux32 14.com" => "http://watir. but I have not checked that) and by some magic (provided by /home/zeljko/.zip (or newer version. for example). Put it somewhere on your PATH. Watir-webdriver drives Chrome on Ubuntu 11.04 Java To drive Opera. you will have to install Java first.5-jre-headless * openjdk-6-jre-headless 53 .4-jre-headless * gcj-4. Let’s check if Java is already installed with java -version: $ java The program ’java’ can be found in the following packages: * gcj-4. 10.1) OpenJDK Client VM (build 20.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.2-0ubuntu1~11. Let’s check if Java is really installed with java -version: $ java -version java version "1... Setting up icedtea-6-jre-cacao (6b22-1. done.04.0-b11.04.1) ..1) ..6.10.2) (6b22-1.04.2-0ubuntu1~11.2-0ubuntu1~11. Install it with sudo apt-get install openjdk-6-jre-headless: $ sudo apt-get install openjdk-6-jre-headless (. Setting up icedtea-6-jre-jamvm (6b22-1.. creating /etc/ssl/certs/java/cacerts. mixed mode...1) .Try: sudo apt-get install <selected package> Looks like we will have to install Java..) Setting up openjdk-6-jre-lib (6b22-1.... Setting up ca-certificates-java (20100412) ..2-0ubuntu1~11.10. sharing) Looks good to me! 54 .10.04. . More info at http://code.) 55 . IRB: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser..google.04 To drive Opera make sure you have it installed. Let’s see how it drives Opera. (. Open our old friend.google.new :opera Selenium::WebDriver::Error::WebDriverError: Unable to find the Selenium server jar.com/p/selenium/downloads/list and set the SELENIUM_SERVER_JAR environmental variable to its location.com/p/selenium/wiki/OperaDriver.Opera with watir-webdriver Opera on Ubuntu 11. Please download the standalone server from http://code. The last step is setting SELENIUM SERVER JAR environmental variable.0. We have to download a file. I will put this file there too. I have step by step guide how to do it. Doubleclick .5. The solution is similar too.Error message similar to the one when we first tried to open Chrome. To see the file go to View > Show Hidden Files.bashrc file. Open Terminal again.com" => "http://watir.com/p/selenium/downloads/list and download selenium-server-standalone-2.new :opera => #<Watir::Browser:0x.fb72fd854 url="http://watir.jar Save the file and close all Terminal windows.0. it sounds more complicated than it really is. the error message says it all. put it somewhere and point a variable to it..com/" 56 . Since I have put chromedriver file in /home/zeljko/bin.com/" title="Watir"> > browser. Go to http://code.5. Do not worry.jar (or newer version. You want to edit . Open Nautilus and go to your home folder: Places > Home Folder. enjoy driving Opera: $ irb > require "rubygems" => true > require "watir-webdriver" => true > browser = Watir::Browser.bashrc file (it will open the file in gedit editor).google. 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 ). If you just got lost.jar Looks good to me! After all this work. To check if the variable is set. Fortunately again. but by default files that have names starting with dot are not displayed.5.goto "watir.0. try printenv | grep SELENIUM: $ printenv | grep SELENIUM SELENIUM_SERVER_JAR=/home/zeljko/bin/selenium-server-standalone-2. Add this line to the file: export SELENIUM_SERVER_JAR=~/bin/selenium-server-standalone-2. Watir-webdriver drives Opera on Ubuntu 11.04 57 . please see Installation chapter. just type irb in command line. An example would be handy right about now Brian Marick would say. since that is the combination available on all operating systems. I hope you will grow to like it the more you know about it. I still remember how pleasantly surprised I was the first time I saw Watir at work. do not panic: > require "watir-webdriver" LoadError: no such file to load -. You will see something like this: $ irb > Now you can enter any Ruby command and you will immediately get a result. Five minutes from now. you will be crazy about Watir. If not.Watir in Five Minutes In this chapter I assume you have Ruby. watir-webdriver gem and Firefox browser installed. if you continue reading the book. maybe it is not the right tool for you. Even then. I am sure you already think IRB is one of the greatest tools for learning a new Ruby library. RubyGems. If you are familiar with Ruby. I will explain all Watir functionality in great (maybe even painful) detail. but for now. If you do not have them installed. If you are new to Ruby. and in a few hours I was able to create a script that would log me into the web application I was testing. 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. To start IRB. not hours. 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). I installed it.watir-webdriver from (irb):1:in ‘require’ from (irb):1 58 . In the rest of the book. I just want to show off a few cool features to get you excited about it. Since you have this book you will be able to log into your web site in minutes. Think of it as a shell that knows Ruby (as the name says). It stands for Interactive Ruby Shell. do this: > require "rubygems" => true > require "watir-webdriver" => true Every Ruby command returns something. You should get => true after require "watir-webdriver". It looks like an arrow. There are two parts in the returned line. it usually means that everything is fine. just ignore what is returned. The second part is true.new :ff 59 . When true is returned. The first one is =>. browser = Watir::Browser. In that case. It means Ruby returned this. the thing that is actually returned. After require "rubygems" you will get either true or false. for now. With just one command you will open Firefox.It just means you have to require RubyGems first. And now the magic starts. Unless I say differently. You do not have to care about that right now. depending how your Ruby installation is set up. . It will be enough. Please open just one browser.. . 60 . I started singing (with apologies to Foreigner): I’ve been waiting for a tool like you to come into my life. you can ignore #<Watir::Browser.new :ff => #<Watir::Browser:0x2ed1f1cd5b186306 url="about:blank" title=""> As I said earlier. and this is textual representation of the object. You can play with other browsers later.watir-webdriver driving Firefox 6 on Mac OS 10. Output should be similar to this: > browser = Watir::Browser. Opening Firefox returned the browser as an object.6 When I saw the browser magically appearing for the first time..>. hr opened. What happens when I enter a URL in browser address bar. as I am sure you already know. > browser. > browser. I will use google.google. there is more to testing than just performing the actions.google. Before we click another link. If your browser already opened google. Watir can do much more. So. when I click a link or a button. but we will deal with that later. It is also the first time we will take a look what Ruby returns after the command.com opens. It gets just a bit more complicated if there are two links with the same text on the page.hr/" And google. isn’t it? Controlling the browser is really useful but. If you are not in the US.com in English").com : > browser. google.com.com/" => "http://www. some other Google site could open. Time to click on a link.com opens. Real world web applications are complex.com in this example. Since I am in Croatia. and sometimes when you are developing a new test or debugging an existing one. ignore this step.hr/" It really works! Ruby returned a string (the thing in double quotes) that contains the text from the browser address bar. when I enter some text in a text field or select something from select box. For example. go to google. Right now I want to click on a link with the text Google. Let’s check if the browser really opened google. I want to show off one of Watir’s killer features. .Just opening a browser is cool.click => [] And google. you want to make sure you are interacting with the correct element. and then try a few sites yourself.com ): 61 . It is called flash. I suggest that you literally follow the example.url => "http://www. Magic. You have to check what happened after the action.com in English. . It is easy to explicitly say which link to click on.goto "http://www. ? This is the first time we will perform a check. Try this (and look at Images link in the top-left corner of google. it can navigate the browser to any site.google. but not so useful.link(:text => "Google.com. > browser.link(:text => "Images").flash => 10 62 . I think Watir is just great for presentations. Isn’t that cool? I use it all the time when I talk about Watir (at conferences.title => "Google Images" We got back the string with the page title. execute the command a few more times.click => [] This time. for example). To execute the same command in command prompt again. This will enter book in search text field: 63 . It is time to click the link: > browser. let’s check the page title.link(:text => "Images"). If you did not see the flash (it flashes just for a short time). > browser. just click up arrow key on your keyboard and the last command that you have typed will appear. Did you see the link flashing? It’s background color changes to red a few times. It is very visual.Link Images changes background from white to red a few times. Let’s search for something. click Search Images button: > browser. As for almost everything else in Ruby and Watir. Paste all code you have entered in IRB in a text file.link(:text => "Google. You want to run the tests and do something else while it runs.click browser. Now.close => true Well. > browser.set "book" browser.button(:value => "Search Images"). The file should look like this: require "watir-webdriver" browser = Watir::Browser. But you do not want to type into IRB all the time.url browser. If you do not know how to inspect pages. I will explain it later.click => [] Page with search results will open.> browser.click browser.close Add require "rubygems" at the top of the file if you had to type it in IRB.goto "http://www. IRB is used only for development or debugging.set "book" => ["book"] Maybe you are wondering how I knew the text field had the value of name attribute set to q (I am talking about :name => "q").google. (You might get a different number.link(:text => "Images"). there is a simple solution.click browser.images.title browser. let’s close the browser.new :ff browser.images.com/" browser.) > browser.com in English"). it does not have to be 250. Let’s check how many images are on the page. and save it with rb extension. read on.size => 250 And finally.text_field(:name => "q").button(:value => "Search Images").text_field(:name => "q"). that was a lot of fun. 64 .size browser. so do not paste irb as the first line of the file. I use RubyMine or NetBeans. If you are not impressed by now. Let’s go deeper.close Run the script. This time the output should look like this: $ ruby watir5.click puts browser. nothing. If IRB is still running in your command prompt. or open a new command prompt.com/" puts browser. 65 . but when you execute Ruby file from the command line. If you liked what you saw so far.rb. you probably never will.goto "http://www.link(:text => "Images").rb Smile while the browser clicks around.url browser.google.size browser.com automatically for you.set "book" browser.google.hr/ Google Images 246 Later I will show you how to make cool looking reports. It is as easy as adding puts in front of the command. it is time to bring on heavy artillery. navigate in command prompt to the folder where you have saved it and run it with ruby watir5. IRB displays values that Ruby returns.images. What is the output in the command prompt? Nothing? Yes. You can remove clicking Google. To run the file. but you really do not care about what some commands return): require "watir-webdriver" browser = Watir::Browser.link(:text => "Google. it does not display the values Ruby returns.button(:value => "Search Images").rb http://www. You have to explicitly say to Ruby that you want them displayed.click puts browser.com in English").You can use any text editor to edit the file. Modify the script to look like this (you can add puts in front of every command.text_field(:name => "q").new :ff browser.title browser. enter exit to return to normal command prompt.click browser.rb: $ ruby watir5.com in English link if Firefox opens google. Save the file as watir5. iPod) and MOBI (Kindle) format via e-mail. iPhone.) Please go to watir.com/book and click buy the book link there. When you buy the book.com/zeljkofilipin/watirbook. the link will probably not work. You can also buy the book for $9 via PayPal by clicking this link: buy the book. I will send it to you in PDF. 66 . you will get all updates to the book for free. If you liked the book so far. (It should work in PDF and HTML format.Buy the Book You are reading free version of the Watir book. EPUB (iPad. Since the book is not finished yet. If you are reading buy the book in EPUB or MOBI format. you can continue reading at github.
Copyright © 2024 DOKUMEN.SITE Inc.