User Interface Testing ensures that the user interface behaves properly when expected actions are performed. XCUITests hook into your application’s accessibility elements to read values, trigger actions, and check existence. The Xcode UI test runner will not see an element if it is not visible in the accessibility hierarchy. By writing UI tests, you’ll unintentionally enhance your app’s accessibility. To know more about UI Testing click here.
Let’s implement UI testing in the app.
For this, I have created a simple app where users can login from the first page and on the second page showing all the country lists in a tableview and when the user clicks on a row it shows details of this country.
Now create UI Testing Bundle target for the test case.
After creating Target the Xcode automatically creates a directory with some files.
Now add code for UI testing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
func testExample() throws { let app = XCUIApplication() app.launch() let namettxt = app.textFields["Enter Your Name"] XCTAssertTrue(namettxt.exists) namettxt.tap() namettxt.typeText("myemail@webkul.com") let passwordtxt = app.secureTextFields["Enter Password"] XCTAssertTrue(passwordtxt.exists) passwordtxt.tap() passwordtxt.typeText("Password") app.buttons["Sign In"].tap() let cell = app.tables.staticTexts["India"] while !cell.exists { app.swipeUp() } cell.tap() app.waitForExistence(timeout: 1) } |
XCUIApplication provide a proxy which can launch, monitor, and terminate app in the UI Testing.
TextFields is part if UIElement groups which help to access UITextFiled form page.
WaitForExistence use to hold screen for a duration.
Tap to click the element like button.
TypeText use to set value.
Now run the test code.
I hope this code will help you better to understand UI Testing in Swift. If you feel any doubt or query please comment below.
Thanks for the read this blog and if you want to visit my other blog click here.