IOS/Swift - 实例成员 'app' 不能用于错误类型

标签 ios swift xcode-ui-testing

我正在我的 UI 测试中创建一个类来调用登录以启动。
因此,我不会经常重复使用代码...我已经创建了新的测试类。
但是,接收“实例成员‘app’不能用于类型‘注册设置’。
我尝试在我的代码周围添加一个get {}(在其他问题中了解到)
/> 然而,这并没有奏效。

我的代码如下:

import XCTest
class signUpSetUp: XCTestCase {
    let app = XCUIApplication()
    var systemAlertMonitorToken: NSObjectProtocol? = nil

    static let signUpSetUp = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")

    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()
        }        

    }
}

有什么想法吗?

最佳答案

app 是一个实例变量。这意味着它只能从实例方法访问。

signUpApp 是一种类型方法。类型方法不是实例方法。类型方法不能访问任何实例变量或任何实例方法。

要么使 app 成为类型变量(就像 signUpSetUp),要么使 signUpApp 成为实例方法。仅进行这两项更改中的一项。

无关,但请注意,以大写字母开头的类名是标准做法。方法和变量名称以小写字母开头。

关于IOS/Swift - 实例成员 'app' 不能用于错误类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40218949/

相关文章:

ios - 写入文本文件并在 Xcode 8 和 Swift 3 中跨应用程序重新启动时保持更改

iphone - 在导航栏上方添加 View

ios - 一次删除 Collection View 中的多个单元格的问题

ios - 当文本字段为空时,如何在 Xcode UI 测试中测试 UITextField?

ios - Xcode 7.1 UI 测试和代码覆盖率

ios - 由于物理设备上的 XCApplicationStateRunningActive,Xcode UI 测试失败

ios - SWTableViewCell的RightUtilityButton点击表格单元格时如何直接显示

ios - 以编程方式将 iPhone 重置为出厂设置

ios - 在 segue 之后继续更新 NSTimer 标签

swift - Rx swift : manage object updates in the app