ios ui自动化测试_Xcuitest的ios自动化ui测试
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)測試? (What is User interface (UI) Testing?)
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? (What is XCUITEST?)
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。
它是如何工作的? (How does it work?)
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項目。
設定測試目標 (Setting up Test Targets)
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…
轉到文件>新建>目標…
2. 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 🥳.
全做完了! 設置好您的測試目標并準備就緒。
學習編寫測試 (Learning to write tests)
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 ”),然后單擊 此處的 “播放”按鈕。
讓我們開始編寫一些測試。 (Let’s start writing some tests.)
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測試的記錄會話例 (Example)
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:
單擊“記錄”按鈕后,該應用程序啟動,之后我開始使用該應用程序。 正如下面的屏幕快照中的注釋和代碼所指出的,我基本上是:
The checked to see if the title exists using XCTAserts. I the title didn’t exist then the test would fail and exit.
使用XCTAserts檢查標題是否存在。 如果標題不存在,則測試將失敗并退出。
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錄制功能通過模擬器添加了其他所有內容。
而已! 您已經進行了第一次UI測試🎉🎊🙌🏽 (That’s it! You have your first UI test 🎉🎊🙌🏽)
下一步是什么? (What’s Next?)
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!)
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自動化測試
總結
以上是生活随笔為你收集整理的ios ui自动化测试_Xcuitest的ios自动化ui测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4.0 苹果系统安装之黑苹果(4)
- 下一篇: PSP目录结构