【问题标题】:Cucumber/Webrat: follow link by CSS class?Cucumber/Webrat:点击 CSS 类的链接?
【发布时间】:2010-12-08 05:59:28
【问题描述】:

是否可以通过类名而不是 id、文本或标题来跟踪链接?鉴于我有(哈哈,黄瓜内幕他?)以下html代码:

<div id="some_information_container">
  <a href="edit" class="edit_button">Translation here</a>
</div>
  • 我不想通过文本进行匹配,因为我必须关心测试中的翻译值
  • 我想让我的按钮看起来都一样,所以我将使用 CSS 类。
  • 我不想为每个链接都分配一个 id,因为其中一些是通过容器和链接类完美识别的

我在 Cucumber/Webrat 中有什么遗漏的吗?或者您有什么建议可以更好地解决这个问题?

感谢您的帮助和问候,

编辑: 我在here 发现了一个关于这个话题的有趣讨论 - 现在似乎仍然是一个悬而未决的问题。您对此还有其他解决方案吗?

【问题讨论】:

  • 我添加了 ruby​​ 标签,Brian。

标签: css ruby css-selectors cucumber webrat


【解决方案1】:

这是我用黄瓜做的,希望对你有帮助。步骤定义中的 # 有助于 CSS 理解发生了什么。

这仅适用于 ID 而非类名

步骤定义

Then /^(?:|I )should see ([^\"]*) within a div with id "([^\"]*)"$/ do |text, selector|
  # checks for text within a specified div id
  within "##{selector}" do |content|  
    if defined?(Spec::Rails::Matchers)
      content.should contain(text)
    else
      hc = Webrat::Matchers::HasContent.new(text)
      assert hc.matches?(content), hc.failure_message
    end  
  end     
end

功能

Scenario Outline: Create Project
  When I fill in name with <title>
      And I select <data_type> from data_type
      And I press "Create"
  Then I should see <title> within a div with id "specifications"

Scenarios: Search Terms and Results
   | data_type  | title        |
   | Books      | A Book Title |

【讨论】:

    【解决方案2】:

    这里是如何在类名为“edit_botton”的元素中声明文本

    Then I should see "Translation here" within "[@class='edit_button']"
    

    【讨论】:

      【解决方案3】:

      find('a.some-class').click怎么样?

      【讨论】:

        【解决方案4】:

        我对 WebRat API 不是很熟悉,但是如何使用 DOM 查找来获取您要查找的类的引用 ID,然后将其传递给 click_link 函数?

        这里是一些 javascript 的链接,用于按类检索项目。 http://mykenta.blogspot.com/2007/10/getelementbyclass-revisited.html

        现在我想了想,使用 Javascript 只是简单地将其更改为某个随机 ID 然后单击它呢?

        无论哪种方式,这应该可以工作,直到解决了包含 getbyclass 函数的名称的节俭辩论。

        【讨论】:

          【解决方案5】:

          have_tag 对你有用吗?

          have_tag('a.edit_button')

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-25
            • 1970-01-01
            • 2010-11-19
            • 2017-10-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多