Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript GragorJS中的browser.wait调用过多_Javascript_Selenium_Asynchronous_Selenium Webdriver_Protractor - Fatal编程技术网

Javascript GragorJS中的browser.wait调用过多

Javascript GragorJS中的browser.wait调用过多,javascript,selenium,asynchronous,selenium-webdriver,protractor,Javascript,Selenium,Asynchronous,Selenium Webdriver,Protractor,我测试注销流的测试步骤是 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 chang

我测试注销流的测试步骤是

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.manage().timeouts().implicitlyWait(5000)

但是,使用locator:by.buttonText(“注销”)
时,我发现
没有找到任何元素。

如果你不止一次地做同样的事情,那么为它创建一个帮助函数会有帮助。将以下内容添加到助手文件:

this.waiterFunc = function(element){
        browser.wait(function() {
            return element.isPresent();
        })
    };
然后在你的主课上,你可以做如下事情: waiterFunc(元素(by.buttonText('Log out'))

我不确定是否要避免browser.wait,因为它是等待元素在特定时间可访问的过程中不可或缺的一步。如果您不等待,那么您将开始出现令人讨厌的“元素在时间x时不可单击”错误

量角器具有内置检查,您不必一直自己编写自定义函数。下面介绍如何等待元素可见-

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
您还可以创建一个自定义函数,如另一个答案所示。希望这有帮助