如何使用水豚测试图像 alt 值?
我正在尝试定义一个步骤来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Capybara 默认使用 xpath,因此除非您更改该设置,否则这可能是您问题的一部分。 (您可以使用
locate(:css, "img[alt]")
)。我会使用 xpath 编写测试,如下所示:
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:
我相信
value
方法返回输入字段的值,不能用于测试属性。像这样的东西可能会起作用:
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:
主题的另一个变体:
another variation on the theme:
我不确定您用来查找图像的方法,但这对我有用:
一旦您拥有该元素,
[:"tag"]
将为您提供值。如果您有更复杂的测试,会将 thing 设置为该值。
I'm not sure about the method you're using to find the image but this works for me:
Once you have the element the
[:"tag"]
will give you the value.will set thing to the value if you have more complex tests.