The Issue
Interacting with textboxes is a fundamental part of browser testing. Filling out usernames, passwords, and a variety of other fields will be necessary to test the feature-functionality of your application. There are multiple Watir Webdriver/Selenium commands that are helpful to us when working with text boxes.
The Answer
1 2 3 4 5 6 7 8 |
# enter value browser.text_field(:id => "text").set "watir-webdriver" # get value browser.text_field(:id => "text").value # clear browser.text_field(:id => "text").clear |
The above commands show three useful interactions for textboxes, set, value, and clear. Set will input your desired text into the field, value will return the value within the field, and clear will delete all text within the field. Knowing how to use these interactions will allow you to tackle the majority of textbox related tests.
The Code
Lets utilize all three commands in an example.
1 2 3 4 5 6 7 8 |
#textfieldtest.rb require 'rubygems' require 'watir-webdriver' browser = Watir::Browser.new :chrome browser.goto "google.com" browser.text_field(:id,"lst-ib").set "3qi labs" puts "Text Field Value: #{browser.text_field(:id,"lst-ib").value}" browser.text_field(:id,"lst-ib").clear puts "Text Field Value: #{browser.text_field(:id,"lst-ib").value}" |
This will navigate to google.com and attempt to validate values within the search box.
The Result
We can save the file (textfieldtest.rb) and run it from the command line, with the following result:
- Browser opens
- Browser navigates to the Google URL
- Enters 3qi labs in the text field
- Returns the current value of the text field
- Clears 3qi labs from the text field
- Returns the current value of the text field
The Takeaway
Whether performing an action such as logging in, or validating a search for an item, you will need to interact with text boxes while browser testing. Getting comfortable with these Watir Webdriver & Selenium commands will make your testing much more efficient.