【问题标题】:Capybara - check for the existence of a data attribute on a linkCapybara - 检查链接上是否存在数据属性
【发布时间】:2013-12-28 22:16:03
【问题描述】:

我的页面上有一个带有特定属性的链接。使用 Rspec + Capybara 如何检查此链接是否存在?

<a href="#" id="text" data-content="This is a discrete bar chart.">Foo</a>

不起作用:

page.find(:css, 'a[data-content="This is a discrete bar chart."]')

作品:

page.find(:css, 'a#test')

【问题讨论】:

  • 看起来应该可以了。您能否发布您看到的完整错误消息?
  • 使用这种选择器需要注意的是,Capybara 选择器匹配的是属性,而不是属性,这意味着如果您在页面加载后添加或更改数据值,您将无法使用数据属性的选择器查找元素。

标签: ruby-on-rails rspec ruby-on-rails-4 capybara


【解决方案1】:

我有相同的查询,但引号不同。

find("a[data-method='delete']").click

为我工作。

【讨论】:

    【解决方案2】:

    这里有一些不同的方法:

    expect(page).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
    
    page.has_selector?("a[href='#'][data-content='This is a discrete bar chart.']")
    
    page.find("a[href='#'][data-content='This is a discrete bar chart.']")  # returns the node found
    

    如果你无权访问页面但可以访问渲染,那么

    expect(Capybara.string(rendered)).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
    

    【讨论】:

      【解决方案3】:

      这可能是 Capybara 发现太多结果的问题。也许将“查找”更改为“全部”,看看您是否获得了一系列可供选择的结果。祝你好运。

      【讨论】:

      • 温馨提示:这可能应该是对原始问题的评论,以获得 OP 的澄清。因为这真的不是一个答案/解决方案。
      【解决方案4】:

      在搜索链接的 title 属性(工具提示)时,这对我有用:

      Then(/^I should see tooltip "(.*?)" on link "(.*?)"$/) do |tip, link|
        within('#results') do
          link = find(:link, link)
          expect(link['title']).to eql(tip)
        end
      end
      

      注意:within 是可选的,可用于减少歧义的可能性。

      HTML源代码:

      <a title="Owner: lfco Fire Department" href="/water_supplies/597df82566e76cfcae000018">Hydrant LFCO One</a>
      

      【讨论】:

        猜你喜欢
        • 2015-11-13
        • 1970-01-01
        • 2018-02-28
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        • 2017-10-05
        相关资源
        最近更新 更多