iOS UITest loading other applications

UI testing: loading other apps

open other apps

We can initialize our own application and other applications (by package name) through XCUIApplicationthe class

For the bundleid of other system applications, refer to iOS to delete built-in applications

XCUIDevice

XCUIDevice can simulate the specific operations of the device, such as clicking the home button, volume button, etc.

return home

/// 点击 Home 键
func toHome() {
    
    
    XCUIDevice.shared.press(XCUIDevice.Button.home);
}

springboard uses

com.apple.springboardSystem pages can be loaded via . Thereby simulating the main interface operation.

let springboard = XCUIApplication.init(bundleIdentifier: "com.apple.springboard")
springboard.launch()

Pull down to open the notification

Pick two points in the page, and drag to simulate a sliding notification (Objective-c can, but Swift can't).

- (void)openNotification{
   	XCUIApplication *app = self.springboard;
    XCUICoordinate *coord1 = [app coordinateWithNormalizedOffset:CGVectorMake(0.1, 0.01)];
    XCUICoordinate *coord2 = [app coordinateWithNormalizedOffset:CGVectorMake(0.1, 0.8)];
    [coord1 pressForDuration:0.1 thenDragToCoordinate:coord2];
}

Safari browser use

You can com.apple.mobilesafariload the browser through , and you can test loading web pages, universal link wake-up applications, deep link wake-up applications, etc.

Web page load test

Define the loading method

    /// 打开浏览器 访问地址
    /// - Parameter url: 访问地址 url
    /// - Returns: 浏览器实例,以便获取后续元素和延迟处理
    /// - Important: go 按钮点击后,进入异步操作,可能会耗时较久,建议后续元素操作设置较大(50-60)超时时间,以免超时崩溃
    func openSafariInput(url:String) -> XCUIApplication {
    
    
        let safari = XCUIApplication.init(bundleIdentifier: "com.apple.mobilesafari")
        safari.launch()
        // 确保唤醒应用
        XCTAssert(safari.wait(for: .runningForeground, timeout: 5), "应该唤起浏览器")
        
        // 开新 tab,防止其他影响
        safari.buttons["TabOverviewButton"].tap()
        safari.buttons["Close"].tap()
        
        // 输入地址
        let textfield = safari.textFields["Address"]
        textfield.tap()
        print("输入地址:\(url)")
        textfield.typeText(url)
        
        let goButton = safari.buttons["Go"]
        goButton.tap()
        sleep(1)
        return safari
    }

Open Baidu official website test

    func testOpenBaidu() {
    
    
        let safari = openSafariInput(url: "https://www.baidu.com")
        let baidu = safari.otherElements["百度一下"] // 可以通过 safari.debugDescription 来查看元素进行验证
        XCTAssert(baidu.waitForExistence(timeout: 60), "应该加载百度主页") // 设置超长超时,避免没加载出页面导致测试异常
        //safari.terminate() // 关闭
    }

The running effect diagram is as follows:

Simulator Screen Recording - iPhone SE (2nd generation) - 2021-12-14 at 16.55.06

Precautions

  • After entering the address, the request is an asynchronous operation, which may take a long time, so the next step should be to set a larger timeout.

  • The browser styles of different iOS versions may be different, and the search process may also be different, so specific operations need to be performed according to the print element layer (xxx.debugDescription).

How to target global search?

We can trigger the search page by swiping point to point, but there is no textfield element by printing debugDescription method.

Guess you like

Origin blog.csdn.net/qq_14920635/article/details/121930916