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

kin*_*net 7 testing ruby-on-rails webrat cucumber capybara

我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

但是我无法想出这一个...类似于:

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

任何人都知道如何找到alt文本值?

Eli*_*cum 12

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
Run Code Online (Sandbox Code Playgroud)


tri*_*ine 10

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

这样的事情可能会起作用:

page.should have_css("img[alt=the_alt_text_you_are_expecting]")
Run Code Online (Sandbox Code Playgroud)


Jos*_*ews 7

主题的另一个变化:

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