Lapis Lazuli:Interacting with an element

From Test Automation Wiki
Jump to: navigation, search

After you have selected an element, you can start interacting with it. The element is actually a Watir element, and so it follows the same rules to interact with it.

browser.goto 'http://training-page.testautomation.info/'

# Selecting the username input field using :objects
elm = browser.find(:input => {:id => 'login-username'})

# Doing stuff with the element

elm.flash # Make the element blink red 3 times

elm.click # click on it

elm.set 'text' # Write text in the element

elm.send_keys :enter # More info: https://watirmelon.blog/tag/send_keys/

elm.hover # Do a hover effect over the element (handy for drop down menus)

# Getting data about the element
elm.id
=> "login-username"

elm.text
=> "" # The input field does not contain text

elm.html
=> "<input id=\"login-username\" class=\"form-control ng-pristine ng-untouched ng-valid\" ng-model=\"Users.login_data.name\" size=\"60\" placeholder=\"Username\" type=\"text\">"

elm.attribute_value('ng-model')
=> "Users.login_data.name"

elm.parent.class_name
=> "form-group"

Besides the above, there more things you can interact with the browser. If you get stuck with some interaction you want to do, simply search google for "Watir [your question]".