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

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.

This will navigate to 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.