无法将类型 'XCUIElement' 的值转换为预期的参数类型 'SignUpSetUp'

Cannot convert value of type 'XCUIElement' to expected argument type 'SignUpSetUp'

我创建了一个帮助程序 class SignUpSetUp 以便登录我的应用程序而不是重复使用代码。在这个 class 中,我有一个私有函数 waitForElementToAppear 来等待测试套件中的元素出现。但是,当使用此功能时,它会生成错误:

Cannot convert value of type 'XCUIElement' to expected argument type 'SignUpSetUp'

为什么会这样,我该如何解决?

我的代码是:

import XCTest

class SignUpSetUp: XCTestCase {

    var systemAlertMonitorToken: NSObjectProtocol? = nil

    static let signUpApp = XCUIApplication()
    static let app = XCUIApplication()

    class func signUpApp() {
        // XCUIApplication().launch()
        //signUpSetUp.launch()

        sleep(2)
        let element = app.buttons["Enable notifications"]
        if element.exists {
            element.tap()
        }
        sleep(3)

        let notifcationsAlert = self.app.alerts.buttons["OK"]
        if notifcationsAlert.exists{
            notifcationsAlert.tap()
            notifcationsAlert.tap()
        }
        sleep(2)
        waitForElementToAppear(self.app.tabBars.buttons["Nearby"])
        let nearbyTab = self.app.tabBars.buttons["Nearby"]
        if nearbyTab.exists {
            nearbyTab.tap()
        }
        sleep(2)
        let enableLocation = self.app.buttons["Enable location"]
        if enableLocation.exists {
            enableLocation.tap()
        }
        let allowLocation = self.app.alerts.buttons["Allow"]
        if allowLocation.exists {
            allowLocation.tap()
            allowLocation.tap()
        }
        sleep(2)
        //waitForElementToAppear(self.app.tabBars.buttons.elementBoundByIndex(4))
        let settingsButton = self.app.tabBars.buttons.elementBoundByIndex(4)
        XCTAssert(settingsButton.exists)
        settingsButton.tap()

        let signUpButton = self.app.tables.staticTexts["Sign Up"]
        if signUpButton.exists {
            signUpButton.tap()
        }

    }

    private func waitForElementToAppear(element: XCUIElement,
                                        file: String = #file, line: UInt = #line) {
        let existsPredicate = NSPredicate(format: "exists == true")
        expectationForPredicate(existsPredicate,
                                evaluatedWithObject: element, handler: nil)

        waitForExpectationsWithTimeout(5) { (error) -> Void in
            if (error != nil) {
                let message = "Failed to find \(element) after 5 seconds."
                self.recordFailureWithDescription(message,
                                                  inFile: file, atLine: line, expected: true)
            }
        }
    }

您的问题是您正在从 class 方法调用实例方法。

waitForElementToAppear是实例方法,而signUpApp是class方法。为了使您的代码正常工作,您需要将两者对齐。从 signUpApp 的签名中删除 class,并从您的两个属性中删除 static,并将对 self.app 的引用更改为仅 app.

let signUpApp = XCUIApplication()
let app = XCUIApplication()

func signUpApp() { ... }

除非您确实希望方法处于 class/static 水平,在这种情况下,您可以在另一个方向对齐。

就最佳实践而言,没有必要让两个属性持有 XCUIApplication 的实例 - 只需要一个并使用它,因为它们将以相同的方式发挥作用。