【问题标题】:Swift 3 - Cannot call value of non-function type 'XCUIElement'Swift 3 - 无法调用非函数类型“XCUIElement”的值
【发布时间】:2016-08-05 20:59:16
【问题描述】:

我正在尝试简化我的 UITest 代码。目前,我有数百行代码用于检查最多八个表格行,每个表格行包含三个文本字段。这不仅会减少我拥有的代码行数,而且会减少由复制/粘贴/编辑过程引起的错误。

我在 checkRow 函数的三行中收到“无法调用非函数类型 'XCUIElement' 的值”错误。

如果我将三行中的“thisRow”变量替换为整数,代码将编译。

这是之前和之后。

func testAkcCh() {
    navConfig.tap()
    pickCD.adjust(toPickerWheelValue: "4")
    pickCB.adjust(toPickerWheelValue: "5")
    pickSD.adjust(toPickerWheelValue: "3")
    pickSB.adjust(toPickerWheelValue: "2")
    XCTAssert(app.tables.cells.count == 8)
    XCTAssert(app.tables.cells.element(boundBy: 0).staticTexts["Best of Breed"].exists)
    XCTAssert(app.tables.cells.element(boundBy: 0).staticTexts[p5].exists)
    XCTAssert(app.tables.cells.element(boundBy: 0).staticTexts[d13].exists)
    XCTAssert(app.tables.cells.element(boundBy: 1).staticTexts["Best of Opposite Sex"].exists)
    XCTAssert(app.tables.cells.element(boundBy: 1).staticTexts[p5].exists)
    XCTAssert(app.tables.cells.element(boundBy: 1).staticTexts[d06].exists)
}

进入

func checkRow(thisRow: Int, thisAward: String, thisPoints: String, thisDefeated: String) {
    XCTAssert(app.tables.cells.element(boundBy: thisRow).staticTexts[thisAward].exists)
    XCTAssert(app.tables.cells.element(boundBy: thisRow).staticTexts[thisPoints].exists)
    XCTAssert(app.tables.cells.element(boundBy: thisRow).staticTexts[thisDefeated].exists)
}

func testAkcCh() {
    navConfig.tap()
    pickCD.adjust(toPickerWheelValue: "4")
    pickCB.adjust(toPickerWheelValue: "5")
    pickSD.adjust(toPickerWheelValue: "3")
    pickSB.adjust(toPickerWheelValue: "2")
    XCTAssert(app.tables.cells.count == 8)
    checkRow(0, "Best of Breed", p5, d13)
    checkRow(1, "Best of Opposite Sex", p5, d06)
}

这可以编译,但破坏了大部分好处......

func checkRow(thisRow: Int, thisAward: String, thisPoints: String, thisDefeated: String) {
    XCTAssert(app.tables.cells.element(boundBy: 0).staticTexts[thisAward].exists)
    XCTAssert(app.tables.cells.element(boundBy: 0).staticTexts[thisPoints].exists)
    XCTAssert(app.tables.cells.element(boundBy: 0).staticTexts[thisDefeated].exists)
}

【问题讨论】:

    标签: xcode compiler-errors coded-ui-tests swift3


    【解决方案1】:

    element(... 函数的函数签名如下:

    func element(boundBy index: UInt) -> XCUIElement
    

    编译器将您的 0 文字解释为上下文的适当类型,直接传递时为 UInt。但是,当将 0 传递给 checkRow 时,它会将其解释为 Int,因为这是您为 thisRow 指定的类型。

    我的猜测是您需要将thisRow 参数的类型更改为UInt

    func checkRow(thisRow: UInt, thisAward: String, thisPoints: String, thisDefeated: String) {
    


    或者,将thisRow 转换为UInt,例如:
    XCTAssert(app.tables.cells.element(boundBy: UInt(thisRow)).staticTexts[thisAward].exists)
    

    【讨论】:

    • 刚刚更新到 Swift 4 并再次开始出现相同的错误,而代码没有任何更改。我不得不删除 Uint(xxx)。 XCTAssertFalse(app.tables.cells.element(boundBy: thisRow).exists)
    • 似乎苹果使用 Swift 4 将签名从 UInt 更改为 Intfunc element(boundBy index: Int) -> XCUIElement
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多