gpt4 book ai didi

ruby-on-rails - 如何使用 capybara 测试图像替代值?

转载 作者:行者123 更新时间:2023-11-28 19:45:25 25 4
gpt4 key购买 nike

我正在尝试使用 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

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

最佳答案

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

关于ruby-on-rails - 如何使用 capybara 测试图像替代值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2838117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com