首页 > 解决方案 > 使用 AppiumDriver 截屏时出现浏览器无法访问的异​​常

问题描述

我们使用 Perfecto 来测试我们的移动应用程序。我们的自动化代码在我们尝试捕获移动屏幕屏幕截图的步骤中生成异常。

我们尝试更改 jar 的版本。最初我们有 Selenium 远程驱动程序 2.48.2。我们将其更改为 2.53.1 并更新了所有其他依赖的 jar。在 2.48.2 中,我们继续得到 Browser unreachable 异常,现在在 2.53.1 中,我们正在超时。我在 2.48.2 版中包含了异常详细信息(带有示例代码段)。2.53.1 的例外情况有一些凭据详细信息,我不确定如何共享这些信息。

异常详情

    org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06'

System info: host: 'WDAAS6302-E1087', ip: '10.86.78.84', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_172'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:206)
    at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:325)
    at reusable.ReusableFuncs.capture(ReusableFuncs.java:167)
    at automationFramework.Trial.main(Trial.java:160)
Caused by: java.lang.NullPointerException
    at org.openqa.selenium.remote.ErrorCodes.toStatus(ErrorCodes.java:256)
    at org.openqa.selenium.remote.JsonToBeanConverter.convert(JsonToBeanConverter.java:124)
    at org.openqa.selenium.remote.JsonToBeanConverter.convert(JsonToBeanConverter.java:42)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.decode(JsonHttpResponseCodec.java:78)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:145)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:67)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:620)
    ... 5 more

例外是在步骤 - File source = ts.getScreenshotAs(OutputType.FILE);

移动自动化对我们来说相当新,我们发现很难解决这个问题。

public synchronized String capture(AppiumDriver<MobileElement> driver,String screenShotName) throws IOException
    {
           TakesScreenshot ts = (TakesScreenshot)driver;

           String dest = System.getProperty("user.dir") +"\\ErrorScreenshots\\"+screenShotName+new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date())+".png";
           System.out.println("destination is "+ dest);
           File source = ts.getScreenshotAs(OutputType.FILE);
           File destination = new File(dest);
           FileUtils.copyFile(source, destination);  
           destination = null;
           dest = resize(dest);
           destination = new File(dest);
           String encodedfile = null;
     try {
         FileInputStream fileInputStreamReader = new 
         FileInputStream(destination);
         byte[] bytes = new byte[(int)destination.length()];
         fileInputStreamReader.read(bytes);
         encodedfile = Base64.getEncoder().encodeToString(bytes);
         System.out.println(encodedfile);
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }
  return "data:image/png;base64,"+  encodedfile;         

}

标签: javaseleniumappiumperfecto

解决方案


我想你可以试试Experitest SeeTest你的移动自动化项目它有内置的报告机制,可以截图。它支持AppiumDriver你的情况。您可以查看Experitest 文档

String screenshotPath = client.capture("CaptureScreenshot")

ScreenshotPath将保存捕获的屏幕截图的路径


推荐阅读