Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 195 lines (118 sloc) 10.286 kb
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
1 # Watir in Five Minutes
2
a09df8f @zeljkofilipin added "You will need internet access if you want to follow examples in t...
zeljkofilipin authored
3 *You will need internet access if you want to follow examples in this chapter.*
4
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
5 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.
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
6
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
7 *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.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
8
b962b96 @zeljkofilipin Felipe is fixing my grammar :)
zeljkofilipin authored
9 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 off a few cool features to get you excited about it.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
10
b962b96 @zeljkofilipin Felipe is fixing my grammar :)
zeljkofilipin authored
11 I still remember how pleasantly surprised I was the first 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.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
12
13 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.
14
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
15 If you are new to Ruby, you are probably thinking: *What is this IRB thing?*
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
16
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
17 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).
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
18
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
19 To start IRB, just type `irb` in command line. You will see something like this:
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
20
21 $ irb
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
22 >
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
23
24
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
25 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"`:
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
26
27 You should see something like this:
28
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
29 > require "watir-webdriver"
30 => true
31
32 If you get something like this, do not panic:
33
34 > require "watir-webdriver"
35 LoadError: no such file to load -- watir-webdriver
36 from (irb):1:in `require'
37 from (irb):1
38
39 It just means you have to require RubyGems first. In that case, do this:
40
41 > require "rubygems"
42 => true
43
44 > require "watir-webdriver"
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
45 => true
46
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
47 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 first 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 fine.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
48
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
49 Unless I say differently, just ignore what is returned, for now.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
50
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
51 And now the magic starts. With just one command you will open Firefox.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
52
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
53 browser = Watir::Browser.new :ff
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
54
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
55 ![watir-webdriver driving Firefox 6 on Mac OS 10.6](https://github.com/zeljkofilipin/watirbook/raw/master/images/watir-in-five-minutes/webdriver-firefox.png)\
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
56
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
57 *watir-webdriver driving Firefox 6 on Mac OS 10.6*
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
58
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
59 When I saw the browser magically appearing for the first time, I started singing (with apologies to [Foreigner](http://www.youtube.com/watch?v=BrzzR-3PPqw)): *I've been waiting for a tool like you to come into my life...*
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
60
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
61 Please open just one browser. It will be enough. You can play with other browsers later.
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
62 Output should be similar to this:
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
63
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
64 > browser = Watir::Browser.new :ff
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
65 => #<Watir::Browser:0x2ed1f1cd5b186306 url="about:blank" title="">
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
66
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
67 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.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
68
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
69 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.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
70
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
71 So, go to *google.com*:
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
72
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
73 > browser.goto "http://www.google.com/"
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
74 => "http://www.google.hr/"
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
75
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
76 And *google.com opens*. Magic, isn't it?
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
77
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
78 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 field or select something from select box...?
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
79
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
80 This is the first time we will perform a check. It is also the first time we will take a look what Ruby returns after the command. Let's check if the browser really opened *google.com*.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
81
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
82 > browser.url
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
83 => "http://www.google.hr/"
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
84
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
85 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.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
86
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
87 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.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
88
ab64e0d @zeljkofilipin refactoring: using browser.a instead of browser.link
zeljkofilipin authored
89 > browser.a(:text => "Google.com in English").click
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
90 => []
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
91
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
92 And *google.com* opens.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
93
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
94 Before we click another link, I want to show off one of Watir's killer features. It is called *flash*. 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*):
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
95
ab64e0d @zeljkofilipin refactoring: using browser.a instead of browser.link
zeljkofilipin authored
96 > browser.a(:text => "Images").flash
f2d3a6e @zeljkofilipin updated Watir in five minutes up to flashing a link
zeljkofilipin authored
97 => 10
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
98
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
99 ![Link Images with normal background](https://github.com/zeljkofilipin/watirbook/raw/master/images/watir-in-five-minutes/flash-1.png)\
100 ![Link Images with red background](https://github.com/zeljkofilipin/watirbook/raw/master/images/watir-in-five-minutes/flash-2.png)\
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
101
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
102 *Link Images changes background from white to red a few times.*
9138097 @zeljkofilipin added flash section
zeljkofilipin authored
103
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
104 Did you see the link flashing? It's background color changes to red a few times. Isn't 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.
9138097 @zeljkofilipin added flash section
zeljkofilipin authored
105
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
106 If you did not see the flash (it flashes 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.
9138097 @zeljkofilipin added flash section
zeljkofilipin authored
107
ad53d3c @zeljkofilipin updated watir in 5 minutes up to executing watir5.rb file
zeljkofilipin authored
108 It is time to click the link:
9138097 @zeljkofilipin added flash section
zeljkofilipin authored
109
ab64e0d @zeljkofilipin refactoring: using browser.a instead of browser.link
zeljkofilipin authored
110 > browser.a(:text => "Images").click
ad53d3c @zeljkofilipin updated watir in 5 minutes up to executing watir5.rb file
zeljkofilipin authored
111 => []
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
112
113 This time, let's check the page title.
114
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
115 > browser.title
ad53d3c @zeljkofilipin updated watir in 5 minutes up to executing watir5.rb file
zeljkofilipin authored
116 => "Google Images"
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
117
118 We got back the string with the page title.
119
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
120 Let's search for something. This will enter *book* in search text field:
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
121
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
122 > browser.text_field(:name => "q").set "book"
ad53d3c @zeljkofilipin updated watir in 5 minutes up to executing watir5.rb file
zeljkofilipin authored
123 => ["book"]
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
124
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
125 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"`). If you do not know how to inspect pages, read on. I will explain it later.
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
126
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
127 Now, click *Search Images* button:
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
128
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
129 > browser.button(:value => "Search Images").click
ad53d3c @zeljkofilipin updated watir in 5 minutes up to executing watir5.rb file
zeljkofilipin authored
130 => []
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
131
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
132 Page with search results will open. Let's check how many images are on the page. (You might get a different number, it does not have to be 250.)
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
133
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
134 > browser.images.size
135 => 250
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
136
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
137 And finally, let's close the browser.
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
138
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
139 > browser.close
bf5a5c0 @zeljkofilipin moved watir in 5 minutes to separate file
zeljkofilipin authored
140 => true
141
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
142 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 file, and save it with *rb* extension. IRB is used only for development or debugging, so do not paste `irb` as the first line of the file. The file should look like this:
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
143
ad53d3c @zeljkofilipin updated watir in 5 minutes up to executing watir5.rb file
zeljkofilipin authored
144 require "watir-webdriver"
145 browser = Watir::Browser.new :ff
146 browser.goto "http://www.google.com/"
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
147 browser.url
ab64e0d @zeljkofilipin refactoring: using browser.a instead of browser.link
zeljkofilipin authored
148 browser.a(:text => "Google.com in English").click
149 browser.a(:text => "Images").click
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
150 browser.title
ad53d3c @zeljkofilipin updated watir in 5 minutes up to executing watir5.rb file
zeljkofilipin authored
151 browser.text_field(:name => "q").set "book"
152 browser.button(:value => "Search Images").click
153 browser.images.size
154 browser.close
155
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
156 Add `require "rubygems"` at the top of the file if you had to type it in IRB.
157
158 You can use any text editor to edit the file. I use [RubyMine](http://www.jetbrains.com/ruby/) or [NetBeans](http://netbeans.org/).
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
159
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
160 Save the file 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.
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
161
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
162 To run the file, navigate in command prompt to the folder where you have saved it and run it with `ruby watir5.rb`:
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
163
ede4cd2 @zeljkofilipin updated watir in five minutes
zeljkofilipin authored
164 $ ruby watir5.rb
165
166 Smile while the browser clicks around.
a86280f @zeljkofilipin minor improvements
zeljkofilipin authored
167
6719da6 @zeljkofilipin minor wording improvement
zeljkofilipin authored
168 What is the output in the command prompt? Nothing? Yes, nothing. IRB displays values that Ruby returns, but when you execute Ruby file 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. Add `puts` in front of three lines. 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:
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
169
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
170 require "watir-webdriver"
171 browser = Watir::Browser.new :ff
172 browser.goto "http://www.google.com/"
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
173 puts browser.url
ab64e0d @zeljkofilipin refactoring: using browser.a instead of browser.link
zeljkofilipin authored
174 browser.a(:text => "Google.com in English").click
175 browser.a(:text => "Images").click
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
176 puts browser.title
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
177 browser.text_field(:name => "q").set "book"
178 browser.button(:value => "Search Images").click
179 puts browser.images.size
180 browser.close
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
181
a86280f @zeljkofilipin minor improvements
zeljkofilipin authored
182 Run the script. This time the output should look like this:
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
183
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
184 $ ruby watir5.rb
185 http://www.google.hr/
186 Google Images
187 246
fc11015 @zeljkofilipin fixed programming ruby links
zeljkofilipin authored
188
189 Later I will show you how to make cool looking reports.
190
cce3e22 @zeljkofilipin updated watir in five minutes to the end
zeljkofilipin authored
191 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. Let's go deeper.
4ce650a @zeljkofilipin minor formatting improvements
zeljkofilipin authored
192
d481860 @zeljkofilipin chapters start on a new page
zeljkofilipin authored
193 \newpage
4ce650a @zeljkofilipin minor formatting improvements
zeljkofilipin authored
194
Something went wrong with that request. Please try again.