如何捕获webdriver因appium会话超时而抛出的SessionNotFoundException

时间:2016-08-16 20:11:44

标签: selenium-webdriver appium

我需要处理我的套件在使用Appium 1.4.16.1在Android实际设备上的Chrome浏览器上运行测试时抛出的以下异常。我相信由于Appium上的超时(设置为10秒)而抛出异常,之后会话被删除。但是,一旦Selenium Webdriver抛出异常(在行wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(UILoc)))),就不会使用catch块捕获它,并且任何进一步的执行都被挂起。

请注意,在桌面Chrome浏览器上执行时,相同的测试用例可以捕获异常。

如果有办法解决这个问题,请告诉我,因为我需要进一步处理。请注意,我可以通过增加Appium会话的超时来避免这种情况。但是,我仍然想要处理最大限制。

Appium超时日志

> info: --> POST /wd/hub/session/5ff4eaccea1df145b80f6df4af4c3d7b/url {"url":"http://test.com"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/5ff4eaccea1df145b80f6df4af4c3d7b/url] to [POST hubsession] with body: {"url":"http://test.com"}
> info: [debug] Didn't get a new command in 10 secs, shutting down...
> info: Shutting down appium session
> info: Chromedriver: Changed state to 'stopping'
> info: JSONWP Proxy: Proxying [DELETE /] to [DELETE hubsession] with no body
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"5ff4eaccea1df145b80f6df4af4c3d7b","status":0,"value":null}
> info: JSONWP Proxy: Replacing sessionId 5ff4eaccea1df145b80f6df4af4c3d7b with 5ff4eaccea1df145b80f6df4af4c3d7b
> info: <-- POST /wd/hub/session/5ff4eaccea1df145b80f6df4af4c3d7b/url 200 110858.244 ms - 72 
> info: --> POST /wd/hub/session/5ff4eaccea1df145b80f6df4af4c3d7b/element {"using":"xpath","value":"//div[@id='form-1010']"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/5ff4eaccea1df145b80f6df4af4c3d7b/element] to [POST hubsession] with body: {"using":"xpath","value":"//div[@id='form-1010']"}
> info: JSONWP Proxy: Got response with status 200: "{\"sessionId\":\"5ff4eaccea1df145b80f6df4af4c3d7b\",\"status\":0,\"value\":null}"
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"","status":6,"value":{"message":"no such session\n  (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 10.0.10586 x86_64)"}}
> info: <-- POST /wd/hub/session/5ff4eaccea1df145b80f6df4af4c3d7b/element 200 743.048 ms - 189 
> info: Chromedriver: Changed state to 'stopped'
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
> info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
> info: [debug] [BOOTSTRAP] [debug] Closed client connection

Webdriver异常日志

Aug 17, 2016 12:49:44 AM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.xpath: //div[@id='form-1010'])
org.openqa.selenium.remote.SessionNotFoundException: no such session
  (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 10.0.10586 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 753 milliseconds
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'Pune832', ip: '10.10.148.44', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129)}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=true, version=52.0.2743.98, platform=ANDROID, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 5ff4eaccea1df145b80f6df4af4c3d7b
    *** Element info: {Using=xpath, value=//div[@id='form-1010']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:899)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:897)
    at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:181)
    at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)

2 个答案:

答案 0 :(得分:0)

无法使用给定的xpath找到元素。 ***元素信息:{Using = xpath,value = // div [@id =&#39; form-1010&#39;]}

由于您使用ID作为定位器,因此我建议使用以下代码 -

driver.findElements(By.id("form-1010"));

我使用下面的代码来检查元素是否存在。如果element不存在,它将捕获错误日志。

protected boolean isElementPresent(By by) throws IOException {
    boolean isElement = false;
    try
    {
        if (driver.findElement(by) != null)
        {
            isElement = true;
            return isElement;
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
        System.out.println("Element not found");
        isElement = false;
        return isElement;
    }
    return isElement;
}

答案 1 :(得分:0)

启动appium服务器时尝试增加超时60秒。超时可以作为参数传递给appium服务器。

--command-timeout 60

服务器用于所有会话的默认命令超时。仍然会被newCommandTimeout cap`

覆盖