如何在 UI 测试期间使用 Xcode 7 截取我的 UI 屏幕截图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31433808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I take screenshots of my UI with Xcode 7 during UI Testing?
提问by Yannis P.
So I downloaded the beta of XCode 7 and I've created some UI tests, but I can't find the functionality of how to take screenshots of my app/UI during the test.
所以我下载了 XCode 7 的测试版,并创建了一些 UI 测试,但我找不到在测试期间如何截取我的应用程序/UI 的功能。
Can someone please help?
有人可以帮忙吗?
回答by Senseful
UI Testing in Xcode automatically takes screenshots of your app after every step.
Xcode 中的 UI 测试会在每一步后自动截取您的应用程序的屏幕截图。
Simply go to one of the tests that has already ran (Report Navigator > choose a Test), then start expanding your tests. When you hover your mouse over the steps, you will see eye icons near each step that has a screenshot.
只需转到已运行的测试之一(报告导航器 > 选择一个测试),然后开始扩展您的测试。当您将鼠标悬停在步骤上时,您会在每个步骤附近看到带有屏幕截图的眼睛图标。
Here's an example... in this case, notice the eye icon at the end of the gray row. If I were to tap on it, I would see a screenshot of the app right when the button in my app was tapped (since the step is Tap the "Button" Button).
这是一个示例...在这种情况下,请注意灰色行末尾的眼睛图标。如果我点击它,当我的应用程序中的按钮被点击时,我会看到应用程序的屏幕截图(因为该步骤是点击“按钮”按钮)。
回答by KrauseFx
If you want to generate screenshots, you can also use snapshot, which describes how to trigger screenshots in UI tests: https://github.com/fastlane/fastlane/tree/master/snapshot#how-does-it-work
如果你想生成截图,你也可以使用snapshot,它描述了如何在 UI 测试中触发截图:https: //github.com/fastlane/fastlane/tree/master/snapshot#how-does-it-work
It basically rotates the device to .Unknown
(Source), which triggers a snapshot without actually modifying your app's state.
它基本上将设备旋转到.Unknown
( Source),这会在不实际修改应用程序状态的情况下触发快照。
Comparing the output with the generated plist file enables you to even properly name the screenshot
将输出与生成的 plist 文件进行比较,您甚至可以正确命名屏幕截图
回答by divergio
Facebook's ios-snapshot-test-caseand KIFboth run as Unit Tests, and therefore are in the same process as the app. As such, they can directly access views and use something like renderView:
or snapshotViewAfterScreenUpdates
. Xcode UI Testing runs in a separate process, and therefore cannot directly access the views.
Facebook 的ios-snapshot-test-case和KIF都作为单元测试运行,因此与应用程序处于同一进程中。因此,他们可以直接访问视图并使用诸如renderView:
或 之类的东西snapshotViewAfterScreenUpdates
。Xcode UI 测试在单独的进程中运行,因此无法直接访问视图。
UI Automation, Apple's now-deprecated Javascript UI testing library, had a function calledcaptureScreenWithName
.
UI 自动化,Apple 现已弃用的 Javascript UI 测试库,有一个名为captureScreenWithName
.
Unfortunately, the new Xcode UI Testing lacks any similar function in its testing library, which to me seems like a glaring omission and I encourage you to submit a Radar for it, as taking screenshots is fundamental to perceptual difference tests (which it sounds like you're trying to do). I hope (and expect) that able will address this deficiency in later Xcode updates.
不幸的是,新的 Xcode UI 测试在它的测试库中缺少任何类似的功能,在我看来这似乎是一个明显的遗漏,我鼓励你为它提交一个雷达,因为截图是感知差异测试的基础(这听起来像你正在尝试做)。我希望(并期望)能够在以后的 Xcode 更新中解决这个缺陷。
In the meantime, there are more creative approaches to taking screenshots. See this stack overflow responsefor a workaround involving taking the screenshot in the app itself and then sending it to the test process.
与此同时,还有更多创造性的截屏方法。请参阅此堆栈溢出响应以获取解决方法,包括在应用程序本身中截取屏幕截图,然后将其发送到测试进程。
回答by TeodorN
I' ve created a tool that saves the tests last n screenshots and generates the JUnit tests results report, parsing TestSummaries plist file from test logs. https://github.com/nacuteodor/ProcessTestSummaries
我创建了一个工具,可以保存测试的最后 n 个屏幕截图并生成 JUnit 测试结果报告,从测试日志中解析 TestSummaries plist 文件。https://github.com/nacuteodor/ProcessTestSummaries
Maybe, that helps you.
也许,这对你有帮助。
回答by ccoroom
Facebook's FBSnapshotTestCase can be alternative solution:
Facebook 的 FBSnapshotTestCase 可以作为替代解决方案: