import XCTest @testable import StudyCK class StudyCKUITests: XCTestCase { var app: XCUIApplication! = nil override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. // 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 = XCUIApplication() app.launch() // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. } override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() } func testEmptyUsernameAndPassword() { app.buttons["登入"].tap() let test = expectation(for: NSPredicate(format: "exists == 1"), evaluatedWith: app.alerts["錯誤"], handler: nil) wait(for: [test], timeout: 10) XCTAssert(app.alerts["錯誤"].staticTexts["帳號不存在"].exists) app.alerts["錯誤"].buttons["重試"].tap() } func testEmptyUsername() { app.secureTextFields["passwordField"].tap() app.secureTextFields["passwordField"].typeText("aa") app.buttons["登入"].tap() let test = expectation(for: NSPredicate(format: "exists == 1"), evaluatedWith: app.alerts["錯誤"], handler: nil) wait(for: [test], timeout: 10) XCTAssert(app.alerts["錯誤"].staticTexts["帳號不存在"].exists) } func testEmptyPassword() { app.textFields["usernameField"].tap() app.textFields["usernameField"].typeText("ck1060535") app.buttons["登入"].tap() let test = expectation(for: NSPredicate(format: "exists == 1"), evaluatedWith: app.alerts["錯誤"], handler: nil) wait(for: [test], timeout: 10) XCTAssert(app.alerts["錯誤"].staticTexts["密碼錯誤,請再試一次"].exists) } func testWrongPassword() { app.textFields["usernameField"].tap() app.textFields["usernameField"].typeText("ck1060535") app.secureTextFields["passwordField"].tap() app.secureTextFields["passwordField"].typeText("aa") app.buttons["登入"].tap() let test = expectation(for: NSPredicate(format: "exists == 1"), evaluatedWith: app.alerts["錯誤"], handler: nil) wait(for: [test], timeout: 10) XCTAssert(app.alerts["錯誤"].staticTexts["密碼錯誤,請再試一次"].exists) } func testLogin() { app.textFields["usernameField"].tap() app.textFields["usernameField"].typeText("tUsername") app.secureTextFields["passwordField"].tap() app.secureTextFields["passwordField"].typeText("tPassword") app.buttons["登入"].tap() expectation(for: NSPredicate(format: "exists == 1"), evaluatedWith: app.navigationBars["choiceController"], handler: nil) waitForExpectations(timeout: 10, handler: nil) } }