Sate Nheehan Sate Nheehan - 1 month ago 18
Swift Question

IOS/Swift - Instance member 'app' cannot be used on type ERROR

I am creating a class in my UI tests to call to sign in for the start up.
So, I am not constantly reusing code... I have created the new test class.
However, recieving a 'Instance member 'app' cannot be used on type 'signup setup'.
I have tried adding a

get {}
around my code (as learned in other questions)
However, this did not work.

My code is as followed :

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

}
}


Any ideas?

Answer

app is an instance variable. This means it can only be accessed from instance methods.

signUpApp is a type method. Type methods are not instance methods. Type methods can't access any instance variables or any instance methods.

Either make app a type variable (just like signUpSetUp) or make signUpApp an instance method. Only make one of those two changes.

Unrelated but please note that it is standard practice to name classes to start with an uppercase letter. Method and variables names start with lowercase letters.

Comments