Getting value from xpath query with and without selenium

Using cucumber with webrat is not the same as using them with selenium. I found this out the hard way. In tracks, you have the ‘badge’ in the top-left corner which shows the number of items on the page. To check the number increases or decreases you want to retrieve the value of the badge.

I had:

xpath= "//span[@id='badge_count']"
response.should have_xpath(xpath) do |node|
  badge = node.first.content.to_i
end

Which did work without selenium, but selenium does not return a node to query. Instead AFAICS selenium wants:

xpath= "//span[@id='badge_count']"
response.should have_xpath(xpath)
badge = response.selenium.get_text("xpath=#{xpath}").to_i

Problem is that this cucumber step is used for features that require selenium and features that do not, so I ended up with

Then /the badge should show (.*)/ do |number|
  badge = -1
  xpath= "//span[@id='badge_count']"
  if Rails.env == 'selenium'
    response.should have_xpath(xpath)
    badge = response.selenium.get_text("xpath=#{xpath}").to_i
  else
    response.should have_xpath(xpath) do |node|
      badge = node.first.content.to_i
    end
  end
  badge.should == number.to_i
end

Not very nice, but functional.

Explore posts in the same categories: Tracks

Comment:

Powered by WP Hashcash