Capybara - 如何根据多个属性进行资格认证?

时间:2015-06-28 13:20:39

标签: selenium attributes capybara

我可以用

expect(page).to have_selector("label.field_with_errors")

expect(page).to have_selector("label[for='landing_zip_code']")

他们工作正常。
但是我想检查它们是否在同一个字段中都是真的。

我试过了:

expect(page).to
have_selector("label[class='field_with_errors']:and([for='landing_zip_code'])")

expect(page).to
have_selector("label[class='field_with_errors']") &&
have_selector('landing_zip_code')

但都没有奏效。 第二次尝试的错误:

Failure/Error: expect(page).to
have_selector("label[class='field_with_errors']") &&
have_selector('landing_zip_code')
expected to find css "landing_zip_code" but there were no matches

我也试过

expect(find("label[class='field_with_errors']")).to
have_attribute('for=landing_zip_code')

但是

Failure/Error: expect(find("label[class='field_with_errors']")).
to have_attribute('for=landing_zip_code')
expected #<Capybara::Node::Element:0x00003b5de90> to respond to `has_attribute?`
# ./spec/features/quote_spec.rb:65:in `block (3 levels) in <top (required)>'

我试过

expect(find("label[class='field_with_errors']")['for=landing_zip_code']).to be

但是

Failure/Error: expect(find("label[class='field_with_errors']")['for=landing_zip_code']).to be
expected nil to evaluate to true

3 个答案:

答案 0 :(得分:0)

在此(我是OP)中我可以使用&#34; .&#34;类和&#34; #&#34;标识符来执行此操作,即< / p>

expect(page).to
have_selector("label.field_with_errors[for='landing_zip_code']")

但是我也想知道如何检测何时除了class和id之外还有其他多个属性

答案 1 :(得分:0)

你试过以下吗?

expect(page).to have_selector("label[for='landing_zip_code']" && "label.field_with_errors")

答案 2 :(得分:0)

expect(find("label[class='field_with_errors']['for=landing_zip_code']")).to be

相关问题