Selenium/Ruby/Debugging

< Selenium‎ | Ruby
(Redirected from Browser testing/Debugging)

Interactive exploration edit

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:

$ irb
 2.0.0p247 :001 > require 'watir-webdriver'
 => true 
 2.0.0p247 :002 > browser = Watir::Browser.new :firefox
 => #<Watir::Browser:0x..fac3245de232edb40 url="about:blank" title=""> 
 2.0.0p247 :003 > browser.goto('http://en.wikipedia.beta.wmflabs.org/wiki/Talk:Flow_QA')
 => "http://en.wikipedia.beta.wmflabs.org/wiki/Talk:Flow_QA"

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.

Unfortunately you can't do this with the mediawiki_selenium gem that handles our MEDIAWIKI_XXX and Sauce Labs variables. marxarelli comments

Some of the global cucumber hooks in mediawiki_selenium make it unfriendly to irb (or inclusion outside the context of cucumber). I'd recommend pry for now.

Breaking into a debugger edit

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.

echo 'gem "pry-debugger"' >> Gemfile
bundle install

(Replace "pry-debugger" with pry-byebug if you're using Ruby 2.x)

At the top of the step definition file add:

require 'pry'

At the line where you want to break add:

binding.pry

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:

page = on(MyPage)
binding.pry
page.some_element

Any valid Ruby expression can be evaluated directly in the shell, including intermediate assignment of local variables like the one just mentioned.

pry> page = on(MyPage)
=> ...
pry> page.some_element.visible?
=> true

You can even define or redefine elements of the page-object class to test whether they can be located.

pry> MyPage.div("someother", id: "some-other")
=> ...
pry> page = on(MyPage)
=> ...
pry> page.someother_element.visible?
=> true

Problem areas edit

    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