Slide 1

Slide 1 text

INTRODUCTION TO WATIR Hillary Hueter

Slide 2

Slide 2 text

About Me • QA Engineer. • My primary duty is to develop the test automation suite for the client’s management console. Which is written in .NET. • I’ve been using Ruby and Watir for 3 years.

Slide 3

Slide 3 text

WATIR

Slide 4

Slide 4 text

What is Watir? • Stands for Web Application Testing in Ruby • A Browser automation tool used for functional cross- browser testing. • There are two supported watir drivers: watir- classic and watir-webdriver. • An addional driver exists for opera, called operawatir.

Slide 5

Slide 5 text

Classic v. Webdriver watir-classic • Windows • Supports Internet Explorer. • Interacts directly through the DOM. It’s built on top of OLE. watir-webdriver • Windows/OSX/Linux • Supports the major browsers (IE, Firefox, Chrome, Safari), headless, and mobile. • Built on top of Selenium Webdriver’s ruby bindings. • Uses Native Events.

Slide 6

Slide 6 text

Considerations in Choosing a driver • As of Watir 3.0 watir is just a initializer for the two watir libraries: watir-classic and watir-webdriver. • Watir 4.0 takes this further by choosing the driver (classic or webdriver) based on the declared browser. • Selenium Webdriver for IE is not the most reliable so it’s a better practice to use watir-classic for IE and webdriver for other browsers.

Slide 7

Slide 7 text

GETTING STARTED

Slide 8

Slide 8 text

Installation >gem install watir • This will install the latest version of watir, and the two drivers watir-classic (if on windows) and watir-webdriver. • Additionally for webdriver, for all browser other than Firefox a separate server will need to be downloaded and placed in the path. • https://code.google.com/p/selenium/downloads/list

Slide 9

Slide 9 text

Starting the Browser require ‘watir’ browser = Watir::Browser.new This will load the default browser, IE on windows and Firefox on OSX and Linux.

Slide 10

Slide 10 text

Start IE require ‘watir’ browser = Watir::Browser.new :ie

Slide 11

Slide 11 text

Start Firefox require ‘watir’ browser = Watir::Browser.new :firefox

Slide 12

Slide 12 text

Start Chrome require ‘watir’ browser = Watir::Browser.new :chrome

Slide 13

Slide 13 text

ACCESSING ELEMENTS

Slide 14

Slide 14 text

browser.element(:tag, “text1”).action browser : the definition of the browser object element: is the element we are trying to access :tag : is the html tag we are going to use find the element. Each element has a set of tags that are applicable. “text1” : the value of the html tag .action: the action we are going to take. Each html element as a set of allowed actions.

Slide 15

Slide 15 text

Defining an Element Single Element, Single Tag browser.element(:tag, “text1”).action Single Element, Multiple Tags browser.element(:tag1=> “value”, :tag2 => “value2”).action Nested Element browser.element_1(:tag=> “value”).element_2(: tag => “value”).action Find the first tag Nested in an container browser.container(:attribute => “value”).element_2

Slide 16

Slide 16 text

Defining an Element First element on the page, i.e the first h2 tag browser.element Find the first tag Nested in an container, i.e the first li in a list browser.container(:attribute => “value”).element_2

Slide 17

Slide 17 text

ACCESSORS FOR COMMON HTML ELEMENTS

Slide 18

Slide 18 text

TEXT FIELDS

Slide 19

Slide 19 text

browser.text_field(:tag, “q”) TAGS • :id • :name • :index • :class • :text • :value • :xpath ACTIONS • .set • .clear

Slide 20

Slide 20 text

LINKS

Slide 21

Slide 21 text

browser.link(:tag, “q”) TAGS • :id • :name • :index • :href • :class • :text • :value • :xpath ACTIONS • .click

Slide 22

Slide 22 text

RADIO BUTTONS

Slide 23

Slide 23 text

browser.radio(:tag, “q”) TAGS • :id • :name • :index • :class • :xpath ACTIONS • .set • .clear • .click • .set?

Slide 24

Slide 24 text

CHECKBOX

Slide 25

Slide 25 text

browser.checkbox(:tag, “q”) TAGS • :id • :name • :index • :class • value • :xpath ACTIONS • .click • .set

Slide 26

Slide 26 text

SELECT LISTS

Slide 27

Slide 27 text

browser.select_list(:tag, “q”) TAGS • :id • :name • :index • :class • :xpath ACTIONS • .select • .select_value

Slide 28

Slide 28 text

IN GENERAL • The element can be accessed by using the html tag instead of the watir defined method. • Using the anchor tag (a) instead of link • Except for input elements • Elements can also be accessed using xpath, though this isn’t the best way. • If accessing an element using an index, Watir uses a zero based indexing.

Slide 29

Slide 29 text

Watir v. Webdriver Syntax • The syntax between the two is for the most part uniform. • Therefore it is possible to use watir-classic to drive IE and webdriver for other browsers. • If there are variances, both classic and webdriver are being developed to use the same syntax.

Slide 30

Slide 30 text

ADVANCED INTERACTIONS JavaScript Popups, Special Keys

Slide 31

Slide 31 text

JAVASCRIPT POPUPS JAVASCRIPT ALERTS # Check if alert is shown browser.alert.exists? # Get text of alert browser.alert.text # Close alert browser.alert.ok browser.alert.close JAVASCRIPT CONFIRMS # Accept confirm browser.alert.ok # Cancel confirm browser.alert.close JAVASCRIPT PROMPT # Enter text to prompt browser.alert.set "Prompt answer" # Accept prompt browser.alert.ok # Cancel prompt browser.alert.close Watir has an alert method that can interact with javascript popups.

Slide 32

Slide 32 text

SENDING SPECIAL KEYS #Press Enter b.send_keys :enter # ctr + a, then delete b.element.send_keys [:control, 'a'], :backspace Watir can also send special keys to a form. The entire list is here.

Slide 33

Slide 33 text

For further reference, more information can be found in the Advanced Interactions on watirwebdriver.com.

Slide 34

Slide 34 text

EXAMPLE

Slide 35

Slide 35 text

https://gist.github.com/certonaqa/5112119 require ‘watir’ b = Watir::Browser.new b.goto ‘http://bit.ly/watir-example’ b.text_field(:name => 'entry.0.single').set ‘Watir Demo’ b.text_field(:name => 'entry.1.single').set “This is a demo for SD Ruby \n This is a scripting demo.“ b.radio(:value => ‘Watir’).set b.checkbox(:value => ‘Ruby’).set b.select_list(:name => 'entry.6.single').select ‘Firefox‘ b.button(:name => 'submit').click puts browser.text.include? 'Your response has been recorded.‘ puts browser.title == 'Thanks!'

Slide 36

Slide 36 text

Further Reading • http://watir.com/ (http://watir-webdriver.com/) • Github: https://github.com/watir/watir • Useful watir blogs

Slide 37

Slide 37 text

Questions?