Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Troubleshooting Watir Code

Justin Ko edited this page Jun 22, 2013 · 1 revision

Although Watir is not bug-free, errors when running Watir are usually the result of the user's code. The best tools for troubleshooting your code are error messages, watir-console, and debug code.

Ruby Error Messages

Ruby error messages may look cryptic, but they're actually very helpful. Look at this error message generated by Ruby:

1) Error:
test15_cancel_policy(TestSuite):
Watir::Exception::UnknownObjectException: Unable to locate element, using :name, "namedInsured"
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/element.rb:52:in `assert_exists'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/element.rb:284:in `enabled?'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/element.rb:56:in `assert_enabled'
c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/input_elements.rb:323:in `set'
./cancel_policy.rb:143:in `cancel_policy'
./cancel_policy.rb:28:in `each'
./cancel_policy.rb:28:in `cancel_policy'
C:/WatirScripts/Regression/epicenter_regression.rb:252:in `test15_cancel_policy'

First, you can see that the error is coming from the test15_cancel_policy method in the TestSuite class. Next, you're given the specific problem Ruby has. In this case, it can't find an element with a name of "namedInsured", so I've likely given it the wrong name parameter or when this code ran, it wasn't on the page where the object exists. The next four lines show the Watir methods where the error was thrown and since the problem is likely with my code, we'll keep moving. The next line of the error message is very useful - it tells us which line of my code is causing the problem. The next few lines show where the problem line is called. If I open the cancel_policy.rb file, I find that the line of my code causing problems reads:

ie.text_field(:name, 'namedInsured').set(username)

The Watir syntax here looks correct - I'm setting the field with a name of "namedInsured" with the variable "username". Next, we'll make sure that the text field exists on the page.

Using irb

Open a command prompt and type 'irb' , you should get this prompt:

irb(main):001:0>

Open a new browser and navigate to the desired page in your applications:

ie = Watir::Browser.new
ie.goto('http://myurl.com')

If you're using Internet Explorer (via watir-classic), you can attach to a web page that is already open. Instead of the two commands above, type:

ie = Watir::Browser.attach(:title, 'My Webpage Title')

When you're on the page where the error is happening, you can see if the object you're trying to work with exists, in this case:

ie.text_field(:name, 'namedInsured').exists?

If the text field exists, you should get a return value of "true", if not, you'll get a return value of "false".

=> true

Since the text field exists, verify that you can set it:

ie.text_field(:name, 'My Name').set('My Name')

You can continue trying out any Watir command you like to verify that you can manipulate page objects as you've defined in your tests.

Using Debug Code

Since the webpage is working with Watir as expected, the next step is to troubleshoot the Watir code itself. Adding debug code to the Watir code allows you to check the variable values, check the state of the webpage and see if you're entering code contained in conditionals.

Although it's likely not our problem, given that the error message says that the text field we're trying to work with doesn't exist, we can check the value contained in "username" to make sure it's valid by writing the value to the console. You can do this with a "puts" statement just before setting the value in the text field (when using double quotes, you can implement string interpolation by using #{}):

puts "username = #{username}"

You can also make sure that the text field exists before trying to set it:

puts "Does the field exist?  #{ie.text_field(:name, 'insuredName').exists?}"

In this case, when I run my code, the output looks like this:

username = My Name
Does the field exist? false

The variable was set correctly, and it looks like the problem is that the text field doesn't exist. We verified that it was on the page with irb, so we should verify that we're actually getting to the page we expect to be on.

One thing to check is whether or not the navigation that takes us to the page is contained in a conditional statement. Again, add a puts statement:

if ie.h1(:text, 'Homepage').exists?
  puts 'entered Homepage conditional'
  ie.link(:text, 'Cancel Policy').click
end

In this case, the message indicating that the conditional was entered was not shown in the console, so we know that we need to troubleshoot the conditional. We'd then repeat the steps above to find out if we actually make it to the Homepage or if the header "Homepage" exists. To tie up this story, I'm going to say that I had a typo and that the header is actually "Home Page". Because the conditional was false, the code that clicks the "Cancel Policy" link didn't get executed.

Other ways to use debug code are to write iterator variables to the console to find out which iteration of some code may be causing problems or writing when various tests are complete so you can easily see which tests have successfully run.