确保标签属于其复选框

时间:2018-01-13 12:55:25

标签: ruby-on-rails rspec capybara

我有一个复选框和一个标签。现在我想确保复选框属于其标签,例如checkbox1 - > LABEL1。如何在水豚上做到这一点?

1 个答案:

答案 0 :(得分:0)

有几种方法可以执行此操作,具体取决于您要确认的确切内容以及您已找到的元素。要验证是否存在与给定标签关联的复选框,您可以使用

中的任何一个进行验证
expect(page).to have_field('associated label text', type: 'checkbox')
expect(page).to have_selector(:checkbox, 'associated label text')

如果您已找到复选框元素(checkbox1)并且只想验证它是否具有相关标签

expect(page).to have_selector(:label, 'label text', for: checkbox1) # if you also want to verify the label text
expect(page).to have_selector(:label, for: checkbox1)

如果您已找到标签和复选框元素(label1,checkbox1)并想要验证找到的元素是否相关

expect(label1).to match_selector(:label, for: checkbox1)
相关问题