Billy Boyo
Billy Boyo

Reputation: 889

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

I have created a helper class SignUpSetUp in order to sign in to my app instead of reusing code. In this class I have a private function waitForElementToAppear to wait for elements in the testing suite to appear. However when using this function it is generating the error:

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

Why is this and how do I resolve this?

My code is:

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

Upvotes: 0

Views: 2277

Answers (1)

Oletha
Oletha

Reputation: 7649

Your problem is that you are calling an instance method from a class method.

waitForElementToAppear is an instance method, but signUpApp is a class method. In order for your code to work, you need to align the two. Remove class from the signature of signUpApp, and also remove static from your two properties, and change references to self.app to just app.

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

func signUpApp() { ... }

Unless you do want the methods to be on a class/static level, in which case you can align in the other direction.

In terms of best practices, there's no need to have two properties holding an instance of XCUIApplication - just have one and use that as they will both function in the same way.

Upvotes: 2

Related Questions