【问题标题】:How can I check with cucumber & webrat & selenium that a selector/tag exists exactly two times in a response?如何使用 cucumber & webrat & selenium 检查选择器/标签在响应中恰好存在两次?
【发布时间】:2011-01-22 00:35:53
【问题描述】:

我对黄瓜和 webrat 使用了以下步骤定义,一切正常:

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

我现在已经转移到 selenium 并且“不知何故” have_selector 不再采用 :count 参数。我收到以下错误:

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

接下来我尝试使用 assert_contain,但我找不到检查确切数字的正则表达式。不幸的是,如果 "class="worker_name"" 的数量小于预期的数量,则通过以下步骤定义。

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

我的问题:

1.) 我如何检查在我的示例中“td.worker_name”准确出现多次的最简单方法?

2.) 如果无法绕过正则表达式:我如何重写上面的正则表达式,以便它检查“class="worker_name"”的确切数字?

非常感谢!

【问题讨论】:

    标签: ruby-on-rails regex selenium cucumber webrat


    【解决方案1】:

    我没有 Ruby 或 Webrat 的经验,但我会这样做。

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

    这是对您的查询进行 xpath 计数,然后检查它是否为 2。目前,除非您使用 Selenium 2,否则您无法使用 Selenium 和 CSS 选择器执行此操作

    【讨论】:

      【解决方案2】:

      感谢 AutomatedTester 的回答,我找到了以下可行的解决方案:

      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
      

      Webrat::Selenium:Matchers 不直接支持 get_xpath_count,但我可以通过底层的 selenium api 访问它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-25
        • 1970-01-01
        • 1970-01-01
        • 2010-12-31
        • 1970-01-01
        • 2013-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多