如何使用水豚测试图像 alt 值?

发布于 2024-09-01 06:42:45 字数 481 浏览 5 评论 0原文

我正在尝试定义一个步骤来使用 Capybara 和 CSS 选择器测试图像的替代文本的值。

我根据自述文件示例为输入值编写了一个:

Then /^I should see a value of "([^\"]*)" within the "([^\"]*)" input$/ do |input_value, input_id|
  element_value = locate("input##{input_id}").value
  element_value.should == input_value
end

但我无法弄清楚这个...类似:

Then /^I should see the alttext "([^\"]*)"$/ do | alt_text |
  element_value = locate("img[alt]").value
end

有人知道如何找到替代文本值吗?

I'm trying to define a step to test the value of alt text of an image using Capybara and CSS selectors.

I wrote one for input values based on the readme examples:

Then /^I should see a value of "([^\"]*)" within the "([^\"]*)" input$/ do |input_value, input_id|
  element_value = locate("input##{input_id}").value
  element_value.should == input_value
end

But I can't figure this one out...something like:

Then /^I should see the alttext "([^\"]*)"$/ do | alt_text |
  element_value = locate("img[alt]").value
end

Anyone know how I can locate the alt text value?

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

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

发布评论

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

评论(4

可爱暴击 2024-09-08 06:42:45

Capybara 默认使用 xpath,因此除非您更改该设置,否则这可能是您问题的一部分。 (您可以使用 locate(:css, "img[alt]"))。

我会使用 xpath 编写测试,如下所示:

Then /^I should see the alt text "([^\"]*)"$/ do | alt_text |
    page.should have_xpath("//img[@alt=#{alt_text}]")
end

Then /^I should see a value of "([^\"])" within the "([^\"])" input$/ do |input_value, input_id|
    page.should have_xpath("//input[@id=#{input_id} and text()=#{input_value}]
end

Capybara uses xpath by default, so unless you changed that setting, that could be part of your problem. (You could use locate(:css, "img[alt]")).

I would write the tests using xpath to look something like this:

Then /^I should see the alt text "([^\"]*)"$/ do | alt_text |
    page.should have_xpath("//img[@alt=#{alt_text}]")
end

Then /^I should see a value of "([^\"])" within the "([^\"])" input$/ do |input_value, input_id|
    page.should have_xpath("//input[@id=#{input_id} and text()=#{input_value}]
end
故人的歌 2024-09-08 06:42:45

我相信 value 方法返回输入字段的值,不能用于测试属性。

像这样的东西可能会起作用:

page.should have_css("img[alt=the_alt_text_you_are_expecting]")

I believe the value method returns the value of input fields and cannot be used to test an attribute.

Something like this might work instead:

page.should have_css("img[alt=the_alt_text_you_are_expecting]")
深海夜未眠 2024-09-08 06:42:45

主题的另一个变体:

Then /^I should see the image alt "([^"]*)"$/ do |alt_text|
  page.should have_css('img', :alt => alt_text)
end

another variation on the theme:

Then /^I should see the image alt "([^"]*)"$/ do |alt_text|
  page.should have_css('img', :alt => alt_text)
end
深海蓝天 2024-09-08 06:42:45

我不确定您用来查找图像的方法,但这对我有用:

expect(find_by_id("image-1")[:alt]).to have_content('tree')

一旦您拥有该元素, [:"tag"] 将为您提供值。

$thing = find_by_id("image-1")[:alt] 

如果您有更复杂的测试,会将 thing 设置为该值。

I'm not sure about the method you're using to find the image but this works for me:

expect(find_by_id("image-1")[:alt]).to have_content('tree')

Once you have the element the [:"tag"] will give you the value.

$thing = find_by_id("image-1")[:alt] 

will set thing to the value if you have more complex tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文