我正在学习用自动化测试capybara
,cucumber
并webdriver
我在进行带有期望的测试,并且通常使用它expect(page).to have_selector('p', 'some cool text')
来查找元素,但是有时它碰巧有多个类似的选择器,有时它会在一段时间后被加载。
为了简化我要说的内容,我将整理一个html方案来演示我要寻找的内容。
setTimeout(function(){
document.getElementById('alpha').innerHTML = 'some cool text';
},3000)
<p style="background: rgba(255,0,0,0.1)">This text will load
<p id="alpha">Text to load after 3s</p>
<p style="background: rgba(255,0,0,0.1)">similar text that I don't want to find</p>
<p>some cool text</p>
我需要知道如何仅选择第一个文本来验证单词是否'some cool text'
已加载,欢迎添加id或类
您编写的语句并没有达到您的期望,因为您只是将“一些很酷的文本”作为参数而不是作为文本选项传递。您想要的是
expect(page).to have_selector('p#alpha', text: 'some cool text')
最多等待Capybara.default_max_wait_time
几秒钟,ID为'alpha'的ap元素将包含文本“一些很酷的文本”出现。默认情况下Capybara.default_max_wait_time
为2秒,因此,如果您有很多事情需要花费3秒钟以上的时间在页面上发生,则可能需要增加该值。如果仅此一项,则wait
可以通过传递选项来延长最大等待特定呼叫的时间
expect(page).to have_selector('p#alpha', text: 'some cool text', wait: 5)
另外-如果我是您,我会使用,have_css
而不是have_selector
当您知道使用CSS时
expect(page).to have_css('p#alpha', text: 'some cool text', wait: 5)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句