运行 对多个对象进行测试
Running tests on several objects
我有以下测试:
expect(page).to have_selector('ul', count: 1)
expect(page).to have_selector('li', count: 21)
expect(page).to have_selector('li img', count: 21)
基本上我想检查列表是否存在,它是否恰好有 21 个项目并且每个项目都有一个图像。
我的代码的问题是,如果第一个元素有 2 个图像,第二个元素有 none,它仍然会通过。
如何单独检查每个列表项?像这样...
expect(page).to have_selector('ul', count: 1)
expect(page).to have_selector('li', count: 21)
foreach('li') do |item|
expect(item).to have_selector('img', count: 1)
end
我会简单地用 XPath 检查图像计数不为 1 的地方没有 li
:
expect(page).to have_selector('ul', count: 1)
expect(page).to have_selector('ul li', count: 21)
expect(page).to have_selector('ul li img', count: 21)
expect(page).to have_no_xpath('//ul//li[count(.//img) != 1]')
Florent B.的答案是正确的,测试你想要的。要按照您所想的方式进行检查,您可以执行类似
的操作
expect(page).to have_selector('ul', count: 1)
lis = page.all('li', count: 21) # same effect as expectation since it will raise an exception if not 21 on page but gets access to the elements
lis.each do |li|
expect(li).to have_selector('img', count: 1)
end
然而,由于查询数量的原因,Florent B. 会执行答案会更快
我有以下测试:
expect(page).to have_selector('ul', count: 1)
expect(page).to have_selector('li', count: 21)
expect(page).to have_selector('li img', count: 21)
基本上我想检查列表是否存在,它是否恰好有 21 个项目并且每个项目都有一个图像。
我的代码的问题是,如果第一个元素有 2 个图像,第二个元素有 none,它仍然会通过。
如何单独检查每个列表项?像这样...
expect(page).to have_selector('ul', count: 1)
expect(page).to have_selector('li', count: 21)
foreach('li') do |item|
expect(item).to have_selector('img', count: 1)
end
我会简单地用 XPath 检查图像计数不为 1 的地方没有 li
:
expect(page).to have_selector('ul', count: 1)
expect(page).to have_selector('ul li', count: 21)
expect(page).to have_selector('ul li img', count: 21)
expect(page).to have_no_xpath('//ul//li[count(.//img) != 1]')
Florent B.的答案是正确的,测试你想要的。要按照您所想的方式进行检查,您可以执行类似
的操作expect(page).to have_selector('ul', count: 1)
lis = page.all('li', count: 21) # same effect as expectation since it will raise an exception if not 21 on page but gets access to the elements
lis.each do |li|
expect(li).to have_selector('img', count: 1)
end
然而,由于查询数量的原因,Florent B. 会执行答案会更快