确保标签属于其复选框

Ensure label belongs to its checkbox

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

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

之一进行验证
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)