水豚查找显示页面 link 无法找到路径
capybara find show page link unable to find path
我正在尝试测试图像 link 是否存在,它是 rails 显示页面。
<a href="/entries/1"></a>
我试过几个水豚,但都不管用。我知道 id
存在。
步骤代码
find_link('/entries/#{@photo_upload_entry.id}').visible?
find_link('/entries/1').visible?
expect(page).to have_link("/entries/#{@photo_upload_entry.id}")
expect(page).to have_link("/entries/1")
黄瓜测试错误
Unable to find link "/entries/\#{@photo_upload_entry.id" (Capybara::ElementNotFound)
Unable to find link "/entries/1" (Capybara::ElementNotFound)
expected to find link "/entries/1" but there were no matches (RSpec::Expectations::ExpectationNotMetError)
expected to find link "/entries/1" but there were no matches (RSpec::Expectations::ExpectationNotMetError)
来自 capybara 文档 - http://www.rubydoc.info/gems/capybara/Capybara/Node/Finders#find_link-instance_method - link 可以通过其 id 或文本找到(它实际上也可以通过包含图像的 title 属性或 alt 属性找到 - 所以显然文档需要更新)。如果您还想匹配 href,可以将其作为选项传入。您在问题中提到它是一个 "image link" 但您的示例中没有显示 img 元素。如果那里有一个图像元素并且它有一个 alt 属性(根据 html 规范的必需属性)你可以匹配它所以下面的任何一个都会 find/determine 元素的存在
find_link('alt of contained img') # will only find if visible so visible? not really needed
find_link('alt of contained img', href: '/entries/1')
expect(page).to have_link('alt value', href: '/entries/1')
如果您引用的图像是通过 CSS 应用的图标或类似的东西,您可以为定位器传递一个空字符串,并传递 href 选项作为匹配对象。类似于以下内容,具体取决于您是要引用 link 还是只是断言它存在
find_link('', href: '/entries/1')
expect(page).to have_link('', href: '/entries/1')
我正在尝试测试图像 link 是否存在,它是 rails 显示页面。
<a href="/entries/1"></a>
我试过几个水豚,但都不管用。我知道 id
存在。
步骤代码
find_link('/entries/#{@photo_upload_entry.id}').visible?
find_link('/entries/1').visible?
expect(page).to have_link("/entries/#{@photo_upload_entry.id}")
expect(page).to have_link("/entries/1")
黄瓜测试错误
Unable to find link "/entries/\#{@photo_upload_entry.id" (Capybara::ElementNotFound)
Unable to find link "/entries/1" (Capybara::ElementNotFound)
expected to find link "/entries/1" but there were no matches (RSpec::Expectations::ExpectationNotMetError)
expected to find link "/entries/1" but there were no matches (RSpec::Expectations::ExpectationNotMetError)
来自 capybara 文档 - http://www.rubydoc.info/gems/capybara/Capybara/Node/Finders#find_link-instance_method - link 可以通过其 id 或文本找到(它实际上也可以通过包含图像的 title 属性或 alt 属性找到 - 所以显然文档需要更新)。如果您还想匹配 href,可以将其作为选项传入。您在问题中提到它是一个 "image link" 但您的示例中没有显示 img 元素。如果那里有一个图像元素并且它有一个 alt 属性(根据 html 规范的必需属性)你可以匹配它所以下面的任何一个都会 find/determine 元素的存在
find_link('alt of contained img') # will only find if visible so visible? not really needed
find_link('alt of contained img', href: '/entries/1')
expect(page).to have_link('alt value', href: '/entries/1')
如果您引用的图像是通过 CSS 应用的图标或类似的东西,您可以为定位器传递一个空字符串,并传递 href 选项作为匹配对象。类似于以下内容,具体取决于您是要引用 link 还是只是断言它存在
find_link('', href: '/entries/1')
expect(page).to have_link('', href: '/entries/1')