软件测试学习之路

Jenkins集成自动化测试时,报错stale element

2019-04-30  本文已影响0人  乘风破浪的姐姐

业务场景:鼠标点击一个区域,该区域变为可编辑模式,输入内容后鼠标离开,再次恢复区域模式

问题

在Jenkins集成测试时,执行测试脚本,部分case中引用的元素对象会抛出如下异常:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=68.0.3440.84)
  (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'CCC20026', ip: '10.94.93.218', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_91'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.42.591088 (7b2b2dca23cca0..., userDataDir: C:\Users\ADMINI~1\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:11950}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 68.0.3440.84, webStorageEnabled: true}
Session ID: 326453a085a4d2be2be6f5dc65a7bbd4
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:166)
    at com.cccis.pageFactory.EstimateItemPage.editItem(EstimateItemPage.java:765)
    at com.cccis.pageFactory.EstimateItemPage.customAddItem(EstimateItemPage.java:848)
    at com.cccis.testcase.demo129_133.Case133.addPart(Case133.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

上述错误大概意思是,所引用的元素已过时,不再依附于当前页面。通常情况下,这是因为页面进行了刷新或跳转,解决方法是,重新使用 findElement 或 findElements 方法进行元素定位即可。

解决方案

使用循环控制元素查找次数,先点击区域元素,点击完成后校验区域是否变为可编辑模式(文本框元素),如果是,就在文本框中输入内容,然后鼠标离开。判断区域元素的值是否与预期输入的值保持一致。如果是,就跳出循环。如果不是,循环次数加1.
如果在操作过程中出现StaleElementReferenceException异常,同样循环次数加1.....

 public boolean retryInput(String xpath1,String xpath2,String txt){
        boolean result = false;
        int count = 0;
        while (count<5){
            try{
                driver.findElement(By.xpath(xpath1)).click();
                seleniumUtil.sleep();
                if(seleniumUtil.isElementExist(xpath2)){
                    driver.findElement(By.xpath(xpath2)).sendKeys(txt);
                    this.clickNoGroupViewBtn();
                    String inputText = driver.findElement(By.xpath(xpath1)).getText();
                    if(inputText!=null && !inputText.equals("") && !inputText.equals("0.00")){
                        result = true;
                        SeleniumUtil.log(log,"参数值录入完成"+txt);
                        break;
                    }
                }
                SeleniumUtil.log(log,"参数值未录入完成,请稍后...");
                count++;
            }catch (StaleElementReferenceException e){
                count++;
            }
        }
        return result;
    }
上一篇下一篇

猜你喜欢

热点阅读