2016-08-05 46 views
3

我想簡化我的UITest代碼。此時,我有數百行代碼用於檢查最多八個表格行,每行都有三個文本字段。這不僅會減少我擁有的代碼行數,而且還會減少由複製/粘貼/編輯過程導致的錯誤。Swift 3 - 不能調用非函數類型的值'XCUIElement'

我在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) 
} 

回答

6

element(...函數的函數簽名看起來像:

func element(boundBy index: UInt) -> XCUIElement 

The co mpiler正在將您的0文字解釋爲上下文的適當類型,該文本在直接傳遞時爲UInt。但是,將0傳遞到checkRow時,它會將其解釋爲Int,因爲這是您爲thisRow指定的類型。

我的猜測是,你需要你的thisRow參數的類型更改爲UInt

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


或者,轉換 thisRowUInt,如:

XCTAssert(app.tables.cells.element(boundBy: UInt(thisRow)).staticTexts[thisAward].exists) 
+0

也做了工作,感謝名單!我還有很多要學習! –

+3

剛剛更新到Swift 4並開始再次獲得相同的錯誤,沒有更改代碼。我不得不刪除Uint(xxx)。我們可以通過下面的例子來說明這個問題:XCTAssertFalse(app.tables.cells.element(boundBy:thisRow).exists) –

+9

好像蘋果用'Swift 4'將簽名從'UInt'改爲'Int'。'func element(boundBy index:Int) - > XCUIElement ' – d4Rk

相關問題