The UI Testing of the Mac App – Test the Dialog

This article says about the test of the dialog with the UI Testing.

スポンサーリンク

Wait until appear

The dialog will be opened when you click the button, launch the application, select the menu item and so on. The dialog will be appeared by many ways. However, there is a common thing, waiting until appear.

The time interval is slightly changed by the machine spec or the state of the machine. To wait until the dialog appeared without effects from them, the code can be following:

let app = XCUIApplication()
// Wait until "alert_window" appeared
let alertDialog = app.dialogs["alert_window"]
XCTAssertTrue(alertDialog.waitForExistence(timeout: 5))

The value of the timeout, 5 seconds is not important. I think that it will be quickly than it.

Which property we should use, “dialogs” or “windows”

When you want to get the window in XCTest code, which property we should use, “dialogs” or “windows”? It is decided by the state of the window.

If the window is displayed as the modal dialog, we need use “dialogs” property. If the application display the window without modal session, such as a document window, we need use “windows” property.

When the application shows the common dialog class

When the application display the common dialog class such as an alert window, the identifier of them are same. The application shows some of them sequentially, we can use sleep to test them.

Next code, confirm “Do you want to remove? at first. If the user clicks “OK”, the application confirm “There is a locked item, remove it force?”. This code tests that two dialogs are displayed. The text is get from the Localizable.strings file.

let app = XCUIApplication()
var bundle = Bundle(for: type(of: self))

XCTContext.runActivity(named: "1st alert") { (activity) -> Void in
    // Wait untile "alert_window" is displayed
    let alertDialog = app.dialogs["alert_window"]
    XCTAssertTrue(alertDialog.waitForExistence(timeout: 5))

    // Test the "message_label"
    let label = alertDialog.staticTexts["message_label"]
    XCTAssertTrue(label.exists)

    // Test the content of the message.
    XCTAssertEqual(label.value as? String,
        bundle.localizedString(forKey: "ConfirmDelete", value: nil, table: nil))

    // Test the OK Button.
    let okButton = alertDialog.buttons["ok_button"]
    XCTAssertTrue(okButton.exists)

    // Click it
    okButton.click()
}

// Close the alert and open the next alert. Wait a second.
Thread.sleep(forTimeInterval: 1)

XCTContext.runActivity(named: "2nd alert") { (activity) -> Void in
    // Wait untile "alert_window" is displayed
    let alertDialog = app.dialogs["alert_window"]
    XCTAssertTrue(alertDialog.waitForExistence(timeout: 5))

    // Test the "message_label"
    let label = alertDialog.staticTexts["message_label"]
    XCTAssertTrue(label.exists)

    // Test the content of the message.
    XCTAssertEqual(label.value as? String,
        bundle.localizedString(forKey: "ItemIsLocked_ConfirmDeleteForce", value: nil, table: nil))

    // Test the OK Button.
    let okButton = alertDialog.buttons["ok_button"]
    XCTAssertTrue(okButton.exists)

    // Click it
    okButton.click()
}
スポンサーリンク
最新情報をチェックしよう!
>現役のプログラマーが書くプログラミング情報

現役のプログラマーが書くプログラミング情報

日々の開発の中での学びや分かったこと、調べたことなどを書いていくブログです。

CTR IMG