【问题标题】:Protractor: Stale Element reference with using conditions量角器:带有使用条件的陈旧元素参考
【发布时间】:2015-08-20 17:14:21
【问题描述】:

我有一段代码尝试查看元素中是否不存在文本

browser.wait(protractor.ExpectedConditions.not(protractor.ExpectedConditions.textToBePresentInElement(self.btnDashboards(),dashboardName)));

在哪里

self.btnDashboards = function() {
    return element(this.selectors.btnDashboards);
};
var dashboardName = 'foo';

但是这偶尔会返回

 致命错误:09:54:11.129 WARN - 抛出异常
org.openqa.selenium.StaleElementReferenceException:过时的元素引用:元素未附加到页面文档
  (会话信息:chrome=44.0.2403.155)
  (驱动程序信息:chromedriver=2.15.322455 (ae8db840dac8d0c453355d3d922c91adfb61df8f),platform=Mac OS X 10.10.4 x86_64)(警告:服务器未提供任何堆栈跟踪信息)
命令持续时间或超时:182 毫秒
有关此错误的文档,请访问:http://seleniumhq.org/exceptions/stale_element_reference.html
构建信息:版本:'2.45.0',修订:'5017cb8',时间:'2015-02-26 23:59:50'
系统信息:主机:'Sujits-MacBook-Pro.local',ip:'10.80.132.90',os.name:'Mac OS X',os.arch:'x86_64',os.version:'10.10.4' , java.version: '1.8.0_51'
驱动程序信息:org.openqa.selenium.chrome.ChromeDriver
功能 [{applicationCacheEnabled=false,rotatable=false,mobileEmulationEnabled=false,chrome={userDataDir=/var/folders/tc/89n0t1qs5p3fyw_n9hk1x7fc0000gn/T/.org.chromium.Chromium.QSwwCm},takesHeapSnapshot=true,databaseEnabled=false,handlesAlerts =true,版本=44.0.2403.155,平台=MAC,browserConnectionEnabled=false,nativeEvents=true,acceptSslCerts=true,locationContextEnabled=true,webStorageEnabled=true,browserName=chrome,takeScreenshot=true,javascriptEnabled=true,cssSelectorsEnabled=true}]
会话 ID:bbd4b5004df6ff3855bccacf8d16e8c0
    在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    在 java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    在 org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    在 org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:152)
    在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在 java.lang.reflect.Method.invoke(Method.java:497)
    在 org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement$1.invoke(EventFiringWebDriver.java:331)
    在 com.sun.proxy.$Proxy2.getText(未知来源)
    在 org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.getText(EventFiringWebDriver.java:381)
    在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在 java.lang.reflect.Method.invoke(Method.java:497)
    在 org.openqa.selenium.remote.server.KnownElements$1.invoke(KnownElements.java:63)
    在 com.sun.proxy.$Proxy3.getText(未知来源)
    在 org.openqa.selenium.remote.server.handler.GetElementText.call(GetElementText.java:29)
    在 org.openqa.selenium.remote.server.handler.GetElementText.call(GetElementText.java:1)
    在 java.util.concurrent.FutureTask.run(FutureTask.java:266)
    在 org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:168)
    在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    在 java.lang.Thread.run(Thread.java:745)

有没有更好的方法来编写代码,这样它就不会发生?另外,为什么这种情况偶尔发生而不是经常发生?

【问题讨论】:

  • btnDashboards() 是如何定义的?另外,发布完整的错误回溯。
  • 这似乎是选择器本身的问题。我也不能说我喜欢代码。可以声明一些变量,因此 Protractor 不会一次处理所有内容。 Protractor 允许在代码中插入魔法,但结果并不令人愉快。
  • @DejanToteff 您能否解释一下“可以声明一些变量,因此 Protractor 不会一次处理所有内容。Protractor 允许在代码中插入魔法,但结果并不令人愉快”更多或将我链接到文档。我是量角器的新手(我来自 java 背景)所以任何提示都会很好。
  • 当然——量角器不像看起来那么稳定。尽一切可能摆脱测试的复杂性。更多信息 - 我在github.com/selfrefactor/Protractor-testing-patterns 上写了一些提示
  • 谢谢。对于我当前的问题,我应该将每个函数调用分配为一个变量来改进这一点?

标签: protractor


【解决方案1】:

因为 browser.wait 返回一个承诺,您可以使用 .then({passed function}, {failed function}) 捕获错误并重试。这是我用的。

browser.prototype.wait2 = function (waitFunction, timeout, timeoutMessage) {
  var self = this;
  return browser.wait(waitFunction, timeout, timeoutMessage).then(function(pass){return pass}, function(error){
    //retry if the error is a stale element, half the timeout
    if(error.name == "StaleElementReferenceError") {
      return self.wait2(waitFunction, timeout/2, timeoutMessage);
    } else {
      throw error;
    }
  });
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2018-12-14
    • 2019-08-10
    • 1970-01-01
    • 2018-08-21
    相关资源
    最近更新 更多