ios ui自动化测试
Who knew automated UI Testing could be so easy! Well, I guess Apple did. Automated UI Testing has never been easier with the XCUITest framework for iOS.
谁知道自动化的UI测试是如此简单! 好吧,我想苹果公司做到了。 借助适用于iOS的XCUITest框架,自动化UI测试从未如此简单。
At work, I am not apart of the Quality Assurance (QA) practice but this is starting to make me reconsider.
在工作中,我并不属于质量保证(QA)部门,但这开始使我重新考虑。
What if there was a way to create UI tests without actually writing code? 🤔 We’ll get to that later but first, let’s briefly talk about what is UI testing and XCUITest.
如果有一种方法可以创建UI测试而不实际编写代码呢? later我们稍后再讨论,但首先,让我们简要讨论什么是UI测试和XCUITest。
UI Testing is the process of checking and validating that the visual elements of an application meet the expected functional and performance criteria for that application. This process does not only check that the application is functioning as it should but also ensures that it meets the end user’s expectations such that it is easily adaptable.
UI测试是检查和验证应用程序的视觉元素是否满足该应用程序预期的功能和性能标准的过程。 此过程不仅检查应用程序是否按其应有的功能运行,而且还确保其满足最终用户的期望,从而使其易于适应。
The scope coverage includes the testing of icons, buttons, input fields, toolbar, menus, colours and lots more. It is also used to test different application flows such as a user’s login or registration flow to ensure that user experience and functionality is working as expected.
范围涵盖了对图标,按钮,输入字段,工具栏,菜单,颜色等的测试。 它还用于测试不同的应用程序流,例如用户的登录或注册流,以确保用户体验和功能按预期工作。
The main areas analysed during UI testing includes:
UI测试期间分析的主要领域包括:
Visual Design 视觉设计 Functionality 功能性 Usability 易用性 Performance 性能XCUITest is a user interface testing framework launched by Apple in 2015. With XCUITest, we can test an Apple platform application user interface using the Swift or Objective-C programming language.
XCUITest是Apple在2015年推出的用户界面测试框架。借助XCUITest,我们可以使用Swift或Objective-C编程语言来测试Apple平台应用程序用户界面。
XCUITest will only work for native iOS & macOS applications. It won’t work for cross-platform or hybrid applications such as React Native, Flutter, Xamarin or Ionic.
XCUITest仅适用于本机iOS和macOS应用程序。 它不适用于跨平台或混合应用程序,例如React Native,Flutter,Xamarin或Ionic。
XCUITest is written and ran using XCode. It provides some handy tools that can be used to speed up your automated UI tests. I am going to assume you have some experience with iOS development and/or you have an iOS project you want to add automated UI testing to.
XCUITest是使用XCode编写和运行的。 它提供了一些方便的工具,可用于加快自动化UI测试的速度。 我假设您具有iOS开发方面的经验,并且/或者您要向其添加自动UI测试的iOS项目。
The first thing you would do is to create a new UI test target in the Xcode project. This can be done by:
您要做的第一件事是在Xcode项目中创建一个新的UI测试目标。 这可以通过以下方式完成:
Going to File > New > Target…
转到文件>新建>目标…
Figure 1 图12. Then you would search for “UI testing bundle”.
2.然后,您将搜索“ UI测试包”。
UI testing bundle” UI测试包 ”Select “UI testing bundle“ and click “Next”.
选择“ UI测试包 ”,然后单击“ 下一步 ”。
3. Fill in the following app & developer related information
3.填写以下与应用程序和开发人员相关的信息
After selecting “UI testing bundle” you should be presented with a dialog where you can enter the product name, the team, the organisation, bundle Identifier and language (Whether swift or Objective-C). The information should already be repopulated for you based on your Xcode and project settings, so you shouldn’t need to change anything
选择“ UI测试包 ”后,将显示一个对话框,您可以在其中输入产品名称,团队,组织,包标识符和语言(无论是swift还是Objective-C)。 该信息应该已经根据您的Xcode和项目设置为您重新填充,因此您无需更改任何内容
Figure 3. Filling in the app and developer information 图3.填写应用程序和开发人员信息Upon clicking “Finish” a new UI test target should be created for you. That’s it! you are free to write test cases now!
单击“ 完成 ”后,应为您创建一个新的UI测试目标。 而已! 您现在可以自由编写测试用例了!
All done! Your test target is set up and ready to go 🥳.
全做完了! 设置好您的测试目标并准备就绪。
You can click the highlighted icon below to see your newly created UI Test targets.
您可以单击下面突出显示的图标,以查看新创建的UI测试目标。
Figure 4. Testing tab in Xcode 图4. Xcode中的Testing选项卡You can hover over the “testExample()” test case and click the play icon to run that specific test to see if everything was set up properly. You should see your app being launched in a simulator and closing immediately after because your test case is empty.
您可以将鼠标悬停在“ testExample()”测试用例上,然后单击“播放”图标以运行该特定测试,以查看所有设置是否正确。 您应该看到您的应用程序正在模拟器中启动,然后由于测试用例为空而立即关闭。
Figure 5. Running a single test 图5.运行一个测试If you wanted to run all the tests in the test suite then you would hover over the class name (In my case “TestScheduleUITest”) and click the play button there.
如果要运行测试套件中的所有测试,则将鼠标悬停在类名称上(在我的情况下为“ TestScheduleUITest ”),然后单击 此处的 “播放”按钮。
Now that the base setup for writing automated UI tests is complete, we can get to the exciting stuff. Here we will actually be writing our tests, XCUITest makes this super-duper easy by allowing you to walk through the flow you want to test on the simulator and it adds the code to the test case for you. It’s an automated way to write your automated UI test cases 😉. Yes, you heard right!
既然编写自动UI测试的基本设置已经完成,我们就可以得到令人兴奋的东西。 在这里,我们实际上将编写我们的测试,XCUITest通过允许您遍历要在模拟器上进行测试的流程并将此代码添加到您的测试用例中,从而使此超级计算机更加容易。 这是一种编写自动化UI测试用例的自动化方法。 是的,您没听错!
First, we are going to click inside the test case called “testExample()”.
首先,我们将在名为“ testExample()”的测试用例中单击。
Figure 6. 图6。After that, you will notice a red dot-like button becomes activated above the terminal section at the bottom of the Xcode editor. This is the record button that is used to monitor how you use the app and generate the code to perform those same actions in the test. Some of these actions include:
之后, 您会注意到 Xcode编辑器底部的终端部分上方的红色点状按钮被激活 。 这是一个记录按钮,用于监视您如何使用应用程序并生成代码以在测试中执行相同的操作。 其中一些动作包括:
Tap 点按 Scrolls 卷轴 Typing on keyboard 在键盘上打字 Referencing different elements on the screen. 引用屏幕上的不同元素。Clicking the record button will initiate the recording session and launch your app for you so that you can begin the automated UI test creation.
单击记录按钮将启动记录会话并为您启动您的应用程序,以便您可以开始自动UI测试创建。
Figure 7. Recording session for UI testing 图7.用于UI测试的记录会话This is a sample test case. For this test case, my goal is to check if each tab of an application is presented without crashing. Each line in “Figure 8” below has comments describing what is happening.
这是一个示例测试用例。 对于此测试用例,我的目标是检查是否显示了应用程序的每个选项卡而不会崩溃。 下面“图8”中的每一行都有注释,描述了正在发生的事情。
After clicking the record button, the app launched after which I started using the app. As the comments and code in the screenshot below point out, I basically:
单击“记录”按钮后,该应用程序启动,之后我开始使用该应用程序。 正如下面的屏幕快照中的注释和代码所指出的,我基本上是:
Launched the application 启动了应用程序 Click the “Map“ tab. 点击“地图”标签。 Referenced the title of the tab 引用了标签的标题The checked to see if the title exists using XCTAserts. I the title didn’t exist then the test would fail and exit.
使用XCTAserts检查标题是否存在。 如果标题不存在,则测试将失败并退出。
I then repeated these same steps for the other tabs. 然后,我对其他选项卡重复了相同的步骤。Clicking the circled diamond-shaped button that is in line with the functions name, can be used to execute your test.
单击与功能名称一致的带圆圈的菱形按钮可用于执行测试。
Figure 8. Complete UI test case example 图8.完整的UI测试用例示例The only thing I needed to do was to add the XCTAssert in line 65, to check if the different elements exist, and the comments of course. Everything else was added by the Xcode recording feature through the simulator.
我唯一需要做的就是在第65行中添加XCTAssert,以检查是否存在不同的元素以及注释。 Xcode录制功能通过模拟器添加了其他所有内容。
Sometimes the way that Xcode reference elements (elements are button, view images and more) are not ideal and complicated. An easy way to reference elements is to make use of Accessibility labels. These allow developers to access elements directly without digging through the view hierarchy to find elements. This would be a great next step to research.
有时,Xcode参考元素(元素是按钮,视图图像等)的方式并不理想且复杂。 引用元素的一种简单方法是使用辅助功能标签。 这些使开发人员可以直接访问元素,而无需深入视图层次结构来查找元素。 这将是下一步的伟大研究。
Thank you for reading. This is my first post so I really appreciate it if you made it this far. If you would like to learn more check out the two articles below. They really helped while I was learning.
感谢您的阅读。 这是我的第一篇文章,因此,如果您到目前为止做得如此,我将不胜感激。 如果您想了解更多信息,请查看下面的两篇文章。 在我学习期间,他们确实提供了帮助。
Learn more at:
了解更多信息:
https://www.hackingwithswift.com/articles/83/how-to-test-your-user-interface-using-xcode
https://www.hackingwithswift.com/articles/83/how-to-test-your-user-interface-using-xcode
https://www.hackingwithswift.com/articles/148/xcode-ui-testing-cheat-sheet
https://www.hackingwithswift.com/articles/148/xcode-ui-testing-cheat-sheet
翻译自: https://medium.com/@davxne/ios-automated-ui-testing-with-xcuitest-20dc9e9987c2
ios ui自动化测试