Watir for automating tests
Watir for automating tests

Watir for automating tests

Published
Published March 7, 2018
Author

Web Elements

Text Fields, Select Lists, Radios, Checkboxes, Buttons, Links, Divs, Spans
b = Watir::Browser.start 'bit.ly/watir-webdriver-demo' t = b.text_field id: 'entry_1000000' s = b.select_list id: 'entry_1000001' r = b.radio value: 'A gem' c = b.checkbox value: '1.9.2' btn = b.button value: 'Submit' l = b.link text: 'Google Forms' d = b.div class: 'ss-form-desc ss-no-ignore-whitespace' s = b.span class: 'powered-by-text'
  • Frames
    • b.iframe(id: 'outside').iframe(id: 'inside').div.text
  • Browser Popups / Window
    • When a new browser window is opened, you can then ‘use’ the new window.
      browser.window(title: 'annoying popup').use do browser.button(id: 'close').click end

Locating Elements

ID, Name, TagName, ClassName, Text, Visible Text, Data Attributes, Aria Attributes
Valid or Custom Attributes, labl, Index, Presence/Absence/Multiple Classes, Presence/Absence Attributes, Visible, Adjacent Nodes
b.div id: 'header' b.text_field name: /new_user_email/ b.element tag_name: 'div' b.text_field class: 'name' b.button text: /Button/ b.button visible_text: 'Button' b.p data_type: /ruby-library/ b.p aria_label: 'ruby-library' b.link href: /non.html/ b.text_feild label: 'With text' b.div index: 2 b.text_field class: ['tr', 'odd'] b.div data_bar: false b.div visible: false anchor_element.parent(selectors) anchor_element.previous_sibling(selectors) anchor_element.following_sibling(selectors) anchor_element.siblings(selectors) anchor_element.child(selectors) anchor_element.children(selectors)

Waits

  • wait_until, wait_while
    • browser.text_field(title: 'Search').when_present.set 'Hello World!' # deprecated browser.wait_until { |b| b.title == "Foo" } browser.window(title: "Foo")wait_while(&:exists?) browser.alert.wait_until { |a| a.text == "foo" } browser.button(name: 'submit').wait_until(&:enabled?) browser.text_field(title: 'Search').wait_until(message: "Can't find it" &:present?)
      The default timeout for Watir’s Waits is 30 seconds. You can pass in the :timeout keyword parameter to any of the wait methods, or you can change the global default with:
      Watir.default_timeout = 60
  • when_present
    • Watir 6.0 deprecated #when_present and #when_enabled and every action call on an element effectively executes this logic by default.
      Note that Watir does its automatic waiting when taking actions, not when attempting to locate. This provides additional flexibility for querying the state of an element without needing unnecessary waits.
  • wait_while_present, wait_until_present
    • deprecated

Cookies

browser = Watir::Browser.new browser.goto 'example.com' # Add browser.cookies.add 'foo', 'bar', path: '/path', expires: (Time.now + 10000), secure: true browser.cookies.to_a # => [] # List (wrong path) browser.goto 'example.com/path' # List (right path) browser.cookies.to_a browser.cookies.save '.cookies' # Save to File browser.cookies.load '.cookies' # Load from a File browser.cookies.delete 'foo' # Delete specific cookie browser.cookies.clear # Delete All cooies

JS Popups: Alerts, Confirms, Prompt

browser.alert.exists? # Check if alert is shown browser.alert.text # Get text of alert browser.alert.ok # Close alert / Accept confirm / Accept prompt browser.alert.close # Close alert / Cancel confirm / Accept prompt browser.alert.set 'Prompt answer' # Enter text to prompt