Selenium/Ruby/Debugging

Interactive exploration
Watir in particular (even before Selenium existed) was always intended to be used interactively as well as batch-wise. Use irb, the interactive Ruby shell: If you get "Server not found" the first time you run browser.goto, try repeating the command. At this point your browser has the page open and you can zoom around in it in some really spectacular ways. A place to start looking is https://github.com/watir/watir/wiki/Using-IRB, but there is a lot more available.

Breaking into a debugger
First, try using the _element suffix, rather than relying on the implicit action if you omit _element.

You can break into a debugger and make sure the element is being located as you expect. In Ruby 1.9 use the pry debugger pry-debugger; in Ruby 2.x use pry-byebug. (Replace "pry-debugger" with  if you're using Ruby 2.x)

At the top of the step definition file add:

At the line where you want to break add:

Then, run the tests form the command line as normal. It should put you into a pry shell at the point where you put binding.pry.

You can then run pry-debugger commands including the usual next, step , and continue commands to step through code. (To save typing, follow these instructions).

You may have to do s/step for a while, but eventually you can see how it actually tries to find the element, and there will be a located field showing whether it found it. To avoid stepping through on(MyPage) (if you're not using a page block), you can do:

Any valid Ruby expression can be evaluated directly in the shell, including intermediate assignment of local variables like the one just mentioned. You can even define or redefine elements of the page-object class to test whether they can be located.

Problem areas
ERROR: Error installing ruby-debug: ERROR: Failed to build gem native extension. /home/spage/.rvm/rubies/ruby-2.1.0/bin/ruby extconf.rb  Can't handle 1.9.x yet
 * How to get irb to load a PageObject from features/support/pages
 * How to get irb to run the cucumber command line.
 * Suggestions requiring ruby-debug such as http://www.agileforall.com/2011/12/cucumber-tip-irb-from-inside-a-step-definition/ failed with