XCUIElement tap()无效

Xav*_*Gil 9 integration-testing ios swift xcode-ui-testing

我有一个非常简单的XCTestCase实现,测试按下按钮并期望显示警报控制器.问题是该tap()方法不起作用.在相关按钮的IBAction中放置一个断点我意识到逻辑甚至没有被调用.

class uitestsampleUITests: XCTestCase {

    var app: XCUIApplication!

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        app = XCUIApplication()
        app.launch()
    }

    func testButton() {
        let button = app.buttons["Button"]
        button.tap()

        expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: button, handler: nil)
        waitForExpectationsWithTimeout(5.0, handler: nil)
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,复制button.tap()指令使测试通过,如下所示:

    func testButton() {
        let button = app.buttons["Button"]
        button.tap()
        button.tap()

        expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: button, handler: nil)
        waitForExpectationsWithTimeout(5.0, handler: nil)    
    }
Run Code Online (Sandbox Code Playgroud)

我在Xcode 7.3.1中遇到这个问题我错过了什么吗?这是一个错误吗?

Xav*_*Gil 8

因此,一位Apple工程师回复了我的错误报告:

第二种可能性是您遇到的问题有时会发生在应用程序完成启动的地方,但启动画面不会立即消失,并且调度到应用程序的事件处理不当.

要尝试解决该问题,请考虑在测试开始时稍微延迟(睡眠(1)应该足够).

所以我做了它现在它的工作原理:

override func setUp() {
    super.setUp()
    continueAfterFailure = false
    app = XCUIApplication()
    app.launch()
    sleep(1)
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,提到的问题不一定是在应用程序启动后立即发生的。这就是为什么这个解决方案不能涵盖所有情况。 (3认同)

Ale*_*e G 6

对于UIWebView哪个是可命中的,在我通过坐标完成之前,水龙头不起作用:

extension XCUIElement {
    func forceTap() {
        coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5)).tap()
    }
}
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助某人

PS 也适用于不可击中的物品,如标签等。


Art*_*nko 5

我有类似的事情。对我来说,问题是我试图挖掘的元素有时不是hittable出于某种原因。

来自苹果的文档:

将点击事件发送到为元素计算的可命中点。

因此,如果元素不是hittable,则点击操作不会做太多事情,这会破坏测试用例的逻辑。

为了解决这个问题,在我点击某些东西之前,我会等到适当的元素变得可命中。很简单。

#import <XCTest/XCTest.h>

@interface XCUIElement (Tap)

- (void)tapInTestCase:(XCTestCase *)testCase;

@end

@implementation XCUIElement (Tap)

- (void)tapInTestCase:(XCTestCase *)testCase
{
    // wait until the element is hittable
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hittable == true"];
    [testCase expectationForPredicate:predicate evaluatedWithObject:element handler:nil];
    [testCase waitForExpectationsWithTimeout:5.0f handler:nil];

    // and then tap
    [self tap];
}

@end
Run Code Online (Sandbox Code Playgroud)