Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Watir

Introduction to Watir

Introduction to using watir for Browser Automation testing.

Hillary Hueter

March 07, 2013
Tweet

Other Decks in Programming

Transcript

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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
  6. Starting the Browser require ‘watir’ browser = Watir::Browser.new This will

    load the default browser, IE on windows and Firefox on OSX and Linux.
  7. 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.
  8. 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
  9. 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
  10. browser.text_field(:tag, “q”) TAGS • :id • :name • :index •

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

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

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

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

    :class • :xpath ACTIONS • .select • .select_value
  15. 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.
  16. 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.
  17. 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.
  18. 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.
  19. For further reference, more information can be found in the

    Advanced Interactions on watirwebdriver.com.
  20. 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!'