【问题标题】:XCUIElement tap() not workingXCUIElement tap() 不工作
【发布时间】:2016-05-17 12:44:02
【问题描述】:

我有一个非常简单的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)
    }
}

另外,复制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)    
    }

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

【问题讨论】:

  • 我已经向 Apple 报告了一个错误:openradar.appspot.com/26320475
  • 这可能是时间问题吗?如果您在尝试点击之前添加 1 秒等待时间,会发生什么情况?不幸的是,UI 自动化框架,尤其是事件生成,充满了问题。
  • 恐怕不行。即使我等待这个期望也不起作用expectationForPredicate(NSPredicate(format: "hittable == 1"), evaluatedWithObject: button, handler: nil)
  • 你等了多久?您是否尝试了谓词格式“exists == true”?您是否也在 tap() 之前尝试了 sleep(5)?
  • 检查这是否有效:> /*Sends a tap event to a hittable/unhittable element.*/ public extension XCUIElement { func forceTapElement() { if self.hittable { self.tap() } else { let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0)) coordinate.tap() } } }

标签: ios swift integration-testing xcode-ui-testing


【解决方案1】:

所以一位 Apple 工程师回复了我的错误报告说:

第二种可能是你遇到了一个问题 有时会发生在应用程序完成启动但 启动画面不会立即消失,事件会发送到 该应用程序未正确处理。

要尝试解决该问题,请考虑在 测试的开始(sleep(1) 应该足够了)。

所以我做到了,现在它可以工作了:

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

【讨论】:

  • 对我来说,上述问题不一定发生在应用程序启动后。这就是为什么此解决方案无法涵盖所有​​情况的原因。
  • 你解决了这个问题吗?如果你解决了,我有同样的问题请帮忙。
【解决方案2】:

对于可点击的UIWebView,在我通过坐标完成之前,点击不起作用:

extension XCUIElement {
    func forceTap() {
        coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5)).tap()
    }
}

希望对某人有所帮助

附:也适用于不可点击的项目,如标签等。

【讨论】:

  • 当我使用浮动在屏幕上方的视图时必须使用它(在这种情况下为滚动视图,尽管不知道它是否重要)。它是可见的,并且存在但不可点击,因此 tap() 不起作用。
【解决方案3】:

我有类似的东西。对我来说,问题是我试图点击的元素有时由于某种原因不是hittable

来自 Apple 的文档:

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

因此,如果元素不是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

【讨论】:

  • 鉴于我从 Apple 得到的答案,您的解决方案可能会奏效。然而,在我提供的非常简单的测试示例中,按钮始终存在,因此它不必等待hittable(测试应该在 UI 准备好时开始)。此外,Apple 的Record UI Test Xcode 功能不会为按钮填充代码以等待成为hittable。在他们解决此问题之前,我宁愿添加 sleep(1) 解决方案。
  • 太好了,很高兴您找到了解决方案。但我应该说,它远非完美。
  • 对我来说,这两种解决方案并不总是有效。有时他们会,有时不会。还在寻找正确点击按钮的方法……也有问题typeText:
【解决方案4】:

您可以在元素加载时wait,我创建了这个扩展:

import XCTest

extension XCUIElement {
    func tap(wait: Int, test: XCTestCase) {
        if !isHittable {
            test.expectation(for: NSPredicate(format: "hittable == true"), evaluatedWith: self, handler: nil)
            test.waitForExpectations(timeout: TimeInterval(wait), handler: nil)
        }
        tap()
    }
}

这样使用:

app.buttons["start"].tap(wait: 20, test: self)

【讨论】:

    【解决方案5】:

    对我来说最可靠的方法是在元素没有被正确点击的地方添加sleep(1)。使用值小于 1000usleep 函数会导致不可靠的行为,例如正确的测试随机失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多