Introduction to Watir

Introduction to Watir

Introduction to using watir for Browser Automation testing.

9abde67b8a52df75a33dc58b5145f2f5?s=128

Hillary Hueter

March 07, 2013
Tweet

Transcript

  1. 2.

    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. 3.
  3. 4.

    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.
  4. 5.

    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.
  5. 6.

    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.
  6. 8.

    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
  7. 9.

    Starting the Browser require ‘watir’ browser = Watir::Browser.new This will

    load the default browser, IE on windows and Firefox on OSX and Linux.
  8. 14.

    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.
  9. 15.

    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
  10. 16.

    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
  11. 19.

    browser.text_field(:tag, “q”) TAGS • :id • :name • :index •

    :class • :text • :value • :xpath ACTIONS • .set • .clear
  12. 20.
  13. 21.

    browser.link(:tag, “q”) TAGS • :id • :name • :index •

    :href • :class • :text • :value • :xpath ACTIONS • .click
  14. 23.

    browser.radio(:tag, “q”) TAGS • :id • :name • :index •

    :class • :xpath ACTIONS • .set • .clear • .click • .set?
  15. 24.
  16. 25.

    browser.checkbox(:tag, “q”) TAGS • :id • :name • :index •

    :class • value • :xpath ACTIONS • .click • .set
  17. 27.

    browser.select_list(:tag, “q”) TAGS • :id • :name • :index •

    :class • :xpath ACTIONS • .select • .select_value
  18. 28.

    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.
  19. 29.

    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.
  20. 31.

    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.
  21. 32.

    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.
  22. 33.

    For further reference, more information can be found in the

    Advanced Interactions on watirwebdriver.com.
  23. 34.
  24. 35.

    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!'