swift test unit (III) XCTest the UI testing UITest

Following the article introduces the unit testing XCTest in this land under Apple introduces UITest official test in the framework XCTest.

1, UITest Profile

        UI Tests Testing is an automated component test UI and interaction. UI testing allows you to find and interact with UI elements, attributes, and also check the status can be performed together, and unit testing. OC UITest use / Swift language automated tests.

        You can write code, or the operation of the recording and coding developer to automatically click a button, view, or automatic input text and other features. With the increase of project functions, old functions need to re-test, resulting in a lot of duplication of work, some of these repetitive work can be done automatically, UI Tests can help solve this problem.

 

2, UITest be UI test

1) create a UI test target

UI test target creation method similar way to create unit tests, but also there are two, just choose different types of target

Method one: When you create a new project, check Include UI Tests

Second way: Create a project that already exists, press comman + 5 xcode open test navigator, click the + button in the lower left corner, then select New UI Test Target from the menu ...

                                               

2) If the existing target, think the new test different classes, press command + n to open a new file, select UI Test Case Class, create different UI test files

3) Run

Run UI test file is command + diamond logo u, test all, or click on the class / method

4) Using the UI test files

After the file is created, it will automatically create the following function

From the comments we can know the meaning of these three functions

function use
setUp 

1) When inheritance and XCTestCase function test file started running 

2)setup中的XCUIApplication().launch()用于启动app

3)可以自定义一个setup函数,将launch方法调用放在自定义函数里,然后添加自己的数据加载

tearDown  继承与XCTestCase 测试函数运行完之后执行
testExample  测试的例子函数

(1)将自带的testExample删除,添加testUI,方法是空白的

(2)光标放进到这个大括号里面,看到如下图中 的小红点。点击这个录制屏幕的按钮。应用启动。我们点击项目中的按钮跳转,函数中会根据您的操作自动生成代码

(3)你可以点击该函数的钻石标志运行,会重复您刚才的操作

(4)您也可以在操作代码自动加入后,或者手写操作代码后,加入断言判断是否是您想要的结果。

5)常用基本操作

(1)点击按钮:app.buttons["登录"].tap()

(2)输入框输入文本

app.textFields["手机号"].tap()    //要先聚焦文本框,才能继续输入

app.textFields["手机号"].typeText("13038865629")

等等

 

3、代码中控件的获取方式

类似于 app.buttons["TDD驱动测试界面"] 这样的,如果我们需要人为手写操作代码,如何获取控件,可以通过button的title或者设置的identifer获取button,类似的还有app.tables, app.textFields,app.staticTexts等等.

xcode也有一个帮助工具 Accessibility Inspector 来获取控件的属性

点击中间的按钮,选中控件,可以看到控件的属性,我们一般利用其title来获取控件

到此,UITest的简单使用就介绍完了......

 

Guess you like

Origin blog.csdn.net/lin1109221208/article/details/93032230