Tuesday, August 9, 2011

Watir Tutorial: Getting a value from a text field


I ran into this problem today, doing a comparison and thought I should write about it.  I will be using the Watir example to illustrate. 

The short and simple answer is that if you have this defined as your browser, and you set the given text field to a specific value:

  1. require 'rubygems'
  2. require 'watir'
  3. browser = Watir::Browser.new
  4. browser.goto("http://bit.ly/watir-example")
  5. browser.text_field(:name => "entry.0.single").set "Watir"
  6. happy = browser.text_field(:name => "entry.0.single").value
  7. puts happy

Then happy will return the value in that text field:  Watir.

The value that is passed back will be a string, and the same is true even if the value is numerical in nature:  

  1. browser.text_field(:name => "entry.0.single").set "42"
  2. happy = browser.text_field(:name => "entry.0.single").value
  3. happy.is_a?(String)

This will return true if you output it to console. 

No comments:

Post a Comment