iOS UI Testing


WWDC 2015 introduces new UI testing features fully integrated into the Xcode 7. In this blog will show how it work.

Create new project, do not forget to check the include UI Tests.

screenshot-2016-08-22-18-35-27

 

Xcode will create the test class for us.

Screenshot 2016-08-22 18.36.05

 

Then, create simple login screen like this.

Screenshot 2016-08-29 21.33.22

 

Don’t forget to add identifier at accessibility for each view. Because we will use it to access in the test.

Screenshot 2016-08-22 20.34.00

 

Here this is a code of view controller that connect to above the screen.

class ViewController: UIViewController {

    @IBOutlet weak var tfUsername: UITextField!
    @IBOutlet weak var tfPassword: UITextField!
    @IBOutlet weak var btnLogin: UIButton!
 
    @IBAction func login() {
        let alertController = UIAlertController(title: "Success", message: "Login successfully.", preferredStyle: .Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil))
            
        if (tfUsername.text?.isEmpty)! || (tfPassword.text?.isEmpty)! {
            alertController.title = "Error"
            alertController.message = "Username and Password cannot be empty."
        } else if !(tfUsername.text == "username" && tfPassword.text == "123456") {
            alertController.title = "Error"
            alertController.message = "Username or Password incorrect."
        }
        presentViewController(alertController, animated: true, completion: nil)
    }
}

We hardcode the username and password, username is ‘username’ and password is ‘123456’ for make the test easy to write.
Next test, when a user does not fill username or password, it will display an alert with an error message like this.

Simulator Screen Shot Aug 22, 2559 BE, 10.57.13 PM

We can access view by using XCUIApplication.(ViewType)["Identifier"].

let app = XCUIApplication()
    var tfUsername: XCUIElement!
    var tfPassword: XCUIElement!
    var btnButton: XCUIElement!
    
    override func setUp() {
        super.setUp()
        // In UI tests it is usually best to stop immediately when a failure occurs.
        continueAfterFailure = false
        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
        app.launch()
        
        tfUsername = app.textFields["Username"]
        tfPassword = app.secureTextFields["Password"]
        btnButton = app.buttons["Login Button"]
    }

First test, when username and password empty, we need to tap() the textfield to focus it before typeText().

func testUsernameAndPasswordAreEmpty() {
        tfUsername.tap()
        tfUsername.typeText("")
        tfPassword.tap()
        tfPassword.typeText("")
        btnButton.tap()
    
        XCTAssert(app.staticTexts["Error"].exists)
        XCTAssert(app.staticTexts["Username and Password cannot be empty."].exists)
    }

Next, we need to check text on AlertController is exists by using staticTexts. Then use XCTAssert to check the response are matches or not.

Second, when username is empty.

    func testUsernameIsEmpty() {
        tfUsername.tap()
        tfUsername.typeText("")
        tfPassword.tap()
        tfPassword.typeText("123456")
        btnButton.tap()
        
        XCTAssert(app.staticTexts["Error"].exists)
        XCTAssert(app.staticTexts["Username and Password cannot be empty."].exists)
    }

Next, when password is empty.

    func testPasswordIsEmpty() {
        tfUsername.tap()
        tfUsername.typeText("username")
        tfPassword.tap()
        tfPassword.typeText("")
        btnButton.tap()
        
        XCTAssert(app.staticTexts["Error"].exists)
        XCTAssert(app.staticTexts["Username and Password cannot be empty."].exists)
    }

Next, we test when username or password are not correct.

Simulator Screen Shot Aug 22, 2559 BE, 11.00.43 PM

    func testUsernameNotCorrect() {
        tfUsername.tap()
        tfUsername.typeText("not_exist_username")
        tfPassword.tap()
        tfPassword.typeText("123456")
        btnButton.tap()
     
        XCTAssert(app.staticTexts["Error"].exists)
        XCTAssert(app.staticTexts["Username or Password incorrect."].exists)
    }
    func testPasswordNotCorrect() {
        tfUsername.tap()
        tfUsername.typeText("username")
        tfPassword.tap()
        tfPassword.typeText("654321")
        btnButton.tap()
        
        XCTAssert(app.staticTexts["Error"].exists)
        XCTAssert(app.staticTexts["Username or Password incorrect."].exists)
    }

Finally, when username and password are correct.

Simulator Screen Shot Aug 22, 2559 BE, 11.00.50 PM

    func testUsernameAndPasswordAreCorrect() {
        tfUsername.tap()
        tfUsername.typeText("username")
        tfPassword.tap()
        tfPassword.typeText("123456")
        btnButton.tap()
        
        XCTAssert(app.staticTexts["Success"].exists)
        XCTAssert(app.staticTexts["Login successfully."].exists)
    }

Here the result.

Screenshot 2016-08-22 23.11.05

Source Code: GitHub
Reference:
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html


One thought on “iOS UI Testing

  1. Hi there,I check your new stuff named “iOS UI Testing – Banana Blog” daily.Your writing style is awesome, keep up the good work! And you can look our website about love spell.

Leave a Reply

Your email address will not be published.