黄瓜测试用于检查具有特定类的元素的父元素

发布于 2024-10-18 00:39:07 字数 294 浏览 1 评论 0原文

我有一个 id 为:#post 的元素,并且该元素具有父级

  • 。现在我想检查
  • 是否具有类 .current 。 步骤可能是这样的:
  • Then the element "post" with parent "li" should have class "current"
    

    如果有人可以帮助我定义step_definitions,那就是gr8!

    I have an element with id: #post and that element has parent <li>. Now I want to check whether the <li> has class .current or not.
    The step might be like

    Then the element "post" with parent "li" should have class "current"
    

    If any body could help me with step_definitions, that'll be gr8!

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    独﹏钓一江月 2024-10-25 00:39:08

    我用过这个(有效):

    Then /^"([^\"]*)" should have class "([^\"]*)"$/ do |id, parent_class|
      assert page.has_xpath?('//a[@id="'+id+'"]/..[@class="'+parent_class+'"]')
    end
    

    I used this, (which works):

    Then /^"([^\"]*)" should have class "([^\"]*)"$/ do |id, parent_class|
      assert page.has_xpath?('//a[@id="'+id+'"]/..[@class="'+parent_class+'"]')
    end
    
    拿命拼未来 2024-10-25 00:39:07

    使用 webrat,您可以搜索 css 选择器。也许这可以解决您的问题:

    Then the element "([^"]*)" with parent "([^"]*)" should have class "([^"]*)" do |element_id,parent,css_class|
      response.should have_selector "#{parent} .#{css_class}" do |matches|
        matches.should have_selector element_id
      end
    end
    

    我没有尝试这段代码,但它应该可以满足您的目的。

    With webrat you can search for an css selector. Maybe this solves your problem:

    Then the element "([^"]*)" with parent "([^"]*)" should have class "([^"]*)" do |element_id,parent,css_class|
      response.should have_selector "#{parent} .#{css_class}" do |matches|
        matches.should have_selector element_id
      end
    end
    

    I didn't try this code, but it should work for your purpose.

    亽野灬性zι浪 2024-10-25 00:39:07

    这些建议应该适合您,但您可能还需要考虑将选择器与故事分离,以使您的测试套件更易于维护,并且更能体现业务价值。例如:您可以将步骤更改为以下内容:

    Then the post should be displayed as the current post
    

    如果您使用 泡菜

    # /features/posts/viewing.feature
    Given the following posts exist:
      | post  | title             | slug              |
      | Lorem | Lorem Ipsum Dolor | lorem-ispum-dolor |
      | Hello | Hello World       | hello-world       |
    When I go to the post page for post "Hello"
    Then post "Hello" should be displayed as the current post
    
    # /features/step_definitions/post_steps.rb
    Then /^#{capture_model} should be displayed as the current post$/ do |post_reference|
      post = model!(post_reference)
      page.should have_css("li.current ##{dom_id(post)}")
    end
    

    These suggestions should work for you, but you might also want to consider decoupling your selectors from your stories to make your test suite easier to maintain, and more expressive of business value. Eg: you might change the step to something like:

    Then the post should be displayed as the current post
    

    This becomes pretty easy to implement if you're using Pickle:

    # /features/posts/viewing.feature
    Given the following posts exist:
      | post  | title             | slug              |
      | Lorem | Lorem Ipsum Dolor | lorem-ispum-dolor |
      | Hello | Hello World       | hello-world       |
    When I go to the post page for post "Hello"
    Then post "Hello" should be displayed as the current post
    
    # /features/step_definitions/post_steps.rb
    Then /^#{capture_model} should be displayed as the current post$/ do |post_reference|
      post = model!(post_reference)
      page.should have_css("li.current ##{dom_id(post)}")
    end
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文