views:

429

answers:

2

I used following step-definition with cucumber and webrat and everything worked fine:

Then /^I should see "([^\"]*)" worker in the workerlist/ do |number|
   response.should have_selector("td.worker_name", :count=>number)
end

I have moved now to selenium and "somehow" the have_selector doesn't take a :count parameter anymore. I get following error:

ArgumentError: wrong number of arguments (2 for 1)   
./features/step_definitions/worker_generation_steps.rb:15:in `have_selector'

Next I tried to use assert_contain, but I couldn't find a regex that checks the exact number. Unfortunately, following step definition passes if the number of "class="worker_name"" is less than the expected number.

Then /^I should see "([^\"]*)" worker in the workerlist/ do |number|
  assert_contain (/((.*)(class="worker_name"))#{number}/m)
end

My questions:

1.) How could I check the easiest way that in my example "td.worker_name" appears exactly a number of times?

2.) If there is no way around regex: How could I rewrite the regex above so that it checks the exact number of "class="worker_name""?

Thanks a lot!

A: 

I have no experience of Ruby or Webrat but the way I would do it is to do is this.

    begin
        assert_equal "2", @selenium.get_xpath_count("//td[@class='worker_name']")
    rescue Test::Unit::AssertionFailedError
        @verification_errors << $!

Which is to do an xpath count on your query and then check that it is 2. At the moment you can't do that with Selenium and CSS selectors unless you use Selenium 2

AutomatedTester
A: 

Thanks to AutomatedTester's answer I found following working solution:

Then /^I should see "([^\"]*)" worker in the workerlist/ do |number|
    response.selenium.get_xpath_count("//td[@class='worker_name']").to_i.should be(number.to_i)
end

get_xpath_count is not supported directly by the Webrat::Selenium:Matchers but I could access it via the underlying selenium api.

Enceradeira