【问题标题】:Unable to take screenshots using Xamarin.UITest无法使用 Xamarin.UITest 截屏
【发布时间】:2020-01-25 14:10:33
【问题描述】:

我正在使用 Andorid 模拟器和 Android 设备在 Windows 7 上运行 Xamarin.UITest 测试。在这两种情况下,我都无法截屏。

我正在初始化这样的东西:

ConfigureApp
    .Android
    .ApkFile(apkPath)
    .EnableLocalScreenshots()
    .StartApp();

然后像这样截屏:

App.Screenshot("App Started");

但是,当调用 App.Screenshot 时,我得到了这个异常:

System.Exception : Error while performing Screenshot("App Started")
  ----> System.Exception : Failed to create C:\Users\USERNAME\.nuget\packages\xamarin.uitest\3.0.3\lib\net45\screenshot-1.png. You may need to set the working directory, which can be done using System.IO.Directory.SetCurrentDirectory().
   at Xamarin.UITest.Utils.ErrorReporting.With[T](Func`1 func, Object[] args, String memberName)
   at Xamarin.UITest.Android.AndroidApp.Screenshot(String title)
   at ACME.App.UITest.BaseUITest.BeforeEachTest() in C:\ACME\ACME.Application.UITest\BaseUITest.cs:line 29
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.GetResult() in D:\a\1\s\src\NUnitFramework\framework\Internal\TaskAwaitAdapter.cs:line 99
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke) in D:\a\1\s\src\NUnitFramework\framework\Internal\AsyncToSyncAdapter.cs:line 60
   at NUnit.Framework.Internal.Commands.SetUpTearDownItem.RunSetUpOrTearDownMethod(TestExecutionContext context, MethodInfo method) in D:\a\1\s\src\NUnitFramework\framework\Internal\Commands\SetUpTearDownItem.cs:line 111
   at NUnit.Framework.Internal.Commands.SetUpTearDownItem.RunSetUp(TestExecutionContext context) in D:\a\1\s\src\NUnitFramework\framework\Internal\Commands\SetUpTearDownItem.cs:line 68
   at NUnit.Framework.Internal.Commands.SetUpTearDownCommand.<>c__DisplayClass0_0.<.ctor>b__0(TestExecutionContext context) in D:\a\1\s\src\NUnitFramework\framework\Internal\Commands\SetUpTearDownCommand.cs:line 48
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.<Execute>b__0() in D:\a\1\s\src\NUnitFramework\framework\Internal\Commands\BeforeAndAfterTestCommand.cs:line 58
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action) in D:\a\1\s\src\NUnitFramework\framework\Internal\Commands\BeforeAndAfterTestCommand.cs:line 73
--Exception
   at Xamarin.UITest.Android.JavaScreenshotTaker.Screenshot(String title)
   at Xamarin.UITest.Android.AndroidApp.<>c__DisplayClass84_0.<Screenshot>b__0()
   at Xamarin.UITest.Utils.ErrorReporting.With[T](Func`1 func, Object[] args, String memberName)

查看生成的日志文件,我看到了:

Could not take screenshot
java.io.IOException: EOF
    at com.android.ddmlib.AdbHelper.read(AdbHelper.java:874)
    at com.android.ddmlib.AdbHelper.getFrameBuffer(AdbHelper.java:333)
    at com.android.ddmlib.CalabashAdbHelper.getFrameBuffer(Unknown Source)
    at com.android.ddmlib.CalabashDevice.getScreenshot(Unknown Source)
    at sh.calaba.screenshot.ScreenshotTaker.takeScreenshot(Unknown Source)
    at sh.calaba.screenshot.ScreenshotTaker.main(Unknown Source)

我尝试过的:

  • 使用 Directory.SetCurrentDirectory() 设置不同的目录 - 这似乎被忽略了,因为我在异常中看到相同的路径无论如何 (C:\Users\USERNAME\.nuget\packages\xamarin.uitest\...)
  • 授予 Everyone 组对路径 C:\Users\USERNAME\.nuget\packages\xamarin.uitest\3.0.3\lib\net45\ 的访问权限 - 无效
  • 确保测试可以将文件写入上述路径 - 他们可以,所以这不应该是权限问题
  • 更新到 Xamarin.UITest 的最新开发版本,3.0.4-dev1 - 结果相同

【问题讨论】:

  • 我自己试试,有结果我会尽快回来的。
  • 尝试在运行时添加SCREENSHOT_VIA_USB="false",例如:calabash-android run path/to/apk.apk -p android SCREENSHOT_VIA_USB="false"。您是否已更新到最新的软件包版本?
  • @Saamer 正如我的问题所说,我当时已经尝试过最新的3.0.4-dev1 - 现在dev2 已经发布,我也会尝试一下。我使用测试运行器(例如 Visual Studio、Rider)运行测试,所以不要自己打电话给calabash-android(现在它仍然是“Xamarin.UITest”吗?)。以前没有遇到过SCREENSHOT_VIA_USB - 它是一个环境变量,还是我可以在代码中设置的东西?另外,有没有关于它的文档?
  • @JessieZhang 是的,一个月前我自己评论过 :)

标签: android xamarin xamarin.forms calabash xamarin.uitest


【解决方案1】:

本周我遇到了同样的问题,这与安全性改进有关,以避免在应用程序上截屏。 如果你这样做了(https://forums.xamarin.com/discussion/96818/disable-the-screenshot-functionality-in-xamarin-forms-application),这可能就是问题所在。只需一个 #if !DEBUG 即可修复它。

希望对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2020-01-13
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多