繁体   English   中英

Appium Android - 截图返回“Illegal base64 character a”

[英]Appium Android - take screenshot returns "Illegal base64 character a"

我有 Android 电话通过 USB 电缆连接到我的电脑,我想在测试失败时截图:

我有以下返回错误的代码:

此行返回错误“File source = driver.getScreenshotAs(OutputType.FILE);” - 有什么不同的解决方案吗?

public String getScreenshotPath(String testCaseName, AppiumDriver driver) throws IOException {

File source = driver.getScreenshotAs(OutputType.FILE);

String destinationFile = System.getProperty("user.dir")+"//reports"+testCaseName+".png";

FileUtils.copyFile(source, new File(destinationFile));

return destinationFile;

}

它返回错误:

java.lang.IllegalArgumentException: Illegal base64 character a

at java.base/java.util.Base64$Decoder.decode0(Base64.java:746)
at java.base/java.util.Base64$Decoder.decode(Base64.java:538)
at java.base/java.util.Base64$Decoder.decode(Base64.java:561)
at org.openqa.selenium.OutputType$2.convertFromBase64Png(OutputType.java:58)
at org.openqa.selenium.OutputType$2.convertFromBase64Png(OutputType.java:55)
at org.openqa.selenium.OutputType$3.convertFromBase64Png(OutputType.java:78)
at org.openqa.selenium.OutputType$3.convertFromBase64Png(OutputType.java:75)
at org.openqa.selenium.remote.RemoteWebDriver.getScreenshotAs(RemoteWebDriver.java:335)
at utils.AppiumUtils.getScreenshotPath(AppiumUtils.java:73)
at Appium.AppiumMaven.Listeners.onTestFailure(Listeners.java:47)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:99)
at org.testng.internal.invokers.TestInvoker.runTestResultListener(TestInvoker.java:277)
at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:978)
at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194)
at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.testng.TestRunner.privateRun(TestRunner.java:806)
at org.testng.TestRunner.run(TestRunner.java:601)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:433)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:427)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:387)
at org.testng.SuiteRunner.run(SuiteRunner.java:330)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1256)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1176)
at org.testng.TestNG.runSuites(TestNG.java:1099)
at org.testng.TestNG.run(TestNG.java:1067)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

那是因为 Appium 在使用它的驱动程序截取屏幕截图时会做一件烦人的事情,但我不确定这是否适用于您的情况。

您可以在控制台中输入 output Base64 来检查具体的 output 吗? 如果 output 是这样的:

"Z3Rlc2dyZXNncmdyZWFncmVzZ3Jlc2dyZWFncmVzZ3Jlc2dlaW93YWpm\n"
"ZW9ndGVzZ3Jlc2dyZ3JlYWdyZXNncmVzZ3JlYWdyZXNncmVzZ2Vpb3dh\n"
"amZlb2d0ZXNncmVzZ3JncmVhZ3Jlc2dyZXNncmVhZ3Jlc2dyZXNnZWlv\n"
"d2FqZmVvZ3Rlc2dyZXNncmdyZWFncmVzZ3Jlc2dyZWFncmVzZ3Jlc2dl\n"
"aW93YWpmZW9ndGVzZ3Jlc2dyZ3JlYWdyZXNncmVzZ3JlYWdyZXNncmVz\n"
"Z2Vpb3dhamZlb2d0ZXNncmVzZ3JncmVhZ3Jlc2dyZXNncmVhZ3Jlc2dy\n"

然后你应该将"\n"替换为空白""

String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
String replaceBase64 = screenshotBase64.replaceAll("\n","");

doSomethingWith(replaceBase64);

这与一些已知问题有关。 阅读https://github.com/appium/java-client/issues/1783

如果要在 android 移动设备上截取 web 应用程序的屏幕截图,则必须截取 Base64 格式并将其添加到 DesiredCapabilites 或 UiAutomator2Options class:

UiAutomator2Options options = new UiAutomator2Options();
options.setNativeWebScreenshot(true);

对于本机应用程序,我不会遇到这个问题。

我添加了以下 2 个依赖项,它对我有用。

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>4.5.0</version>    <!-- Upgrading this version to higher, will fail test script taking screenshot -->
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>4.5.0</version>    <!-- Upgrading this version to higher, will fail test script taking screenshot -->
    </dependency>

我最初添加了 4.6.0 并显示相同的错误,但现在它适用于 4.5.0

试试这个:getScenario().embed(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)

而不是: File source = driver.getScreenshotAs(OutputType.FILE);

String destinationFile = System.getProperty("user.dir")+"//reports"+testCaseName+".png";

FileUtils.copyFile(源,新文件(目标文件));

返回目标文件;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM