gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:38:22 27 4
gpt4 key购买 nike

我创建了一个辅助类 SignUpSetUp 以便登录我的应用程序而不是重复使用代码。在这个类中,我有一个私有(private)函数 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)
}
}
}

最佳答案

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

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

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

func signUpApp() { ... }

除非您确实希望方法处于类/静态级别,在这种情况下您可以朝另一个方向对齐。

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

关于swift - 无法将类型 'XCUIElement' 的值转换为预期的参数类型 'SignUpSetUp',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40222250/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com