我测试注销流程的测试步骤是,
1) 单击注销按钮
2) 等待 url 更改为 login.html
3) 等待登录页面文本字段加载。
代码看起来像,
//wait for logout menu/button
browser.wait(function(){
return element(by.buttonText('Log out')).isPresent()
})
element(by.buttonText('Log out')).click()
//wait for url to change to login.html
browser.wait(function(){
return browser.getCurrentUrl().then(function(url){
return url.indexOf("login") != -1
})
})
//wait for login page text boxes
browser.wait(function(){
return element(by.css('[type=text]')).isPresent()
})
这使得我的代码变得冗长,因为我将每个操作包装在 browser.wait
调用中。
有没有办法可以避免 browser.wait
。我尝试添加,
browser.manage().timeouts().implicitlyWait(5000)
但是随后,我收到 No element found using locator: by.buttonText("Log out")
错误。
最佳答案
Protractor 已内置ExpectedConditions
检查,您不必一直自己编写自定义函数。以下是如何等待元素可见 -
var EC = protractor.ExpectedConditions;
browser.wait(EC.presenceOf(element(by.buttonText('Log out'))), 10000); //Checks only if element is present in DOM
browser.wait(EC.visibilityOf(element(by.buttonText('Log out'))), 10000); //Checks if element is present in DOM and visible to user on page
您还可以创建自定义函数,如另一个答案中所示。希望这会有所帮助。
关于javascript - protractorjs 中的 browser.wait 调用过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34916699/