2016-10-24 16 views
0

コードを再利用するのではなく、自分のアプリケーションにサインインするためにヘルパークラスSignUpSetUpを作成しました。このクラスで私はプライベート関数waitForElementToAppearを持って、テストスイート内の要素が現れるのを待つ。しかし、この関数を使用するとエラーが発生します:'XCUIElement'型の値を期待される引数型 'SignUpSetUp'に変換できません

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

なぜあなたのクラス名と同じ変数( 'SignUpSetUp')に名前を付けようとしていますか? – rmaddy

+0

申し訳ありませんが、これが正しいです、編集されました。 –

+0

最後の質問に自分のコメントを忘れないでください。メソッド名と変数名は小文字で始まる必要があります。変数の名前を変更したので、あなたの質問を更新してください。 – rmaddy

答えて

2

あなたの問題は、あなたがクラスメソッドからインスタンスメソッドを呼び出しているということです。

waitForElementToAppearはインスタンスメソッドですが、signUpAppはクラスメソッドです。コードを機能させるには、2つを整列させる必要があります。 signUpAppの署名からclassを削除し、2つのプロパティからstaticも削除し、self.appへの参照をappに変更してください。

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

func signUpApp() { ... } 

メソッドをクラス/静的レベルにする必要がある場合を除いて、他の方向に整列することができます。

ベストプラクティスの面では、XCUIApplicationのインスタンスを保持する2つのプロパティを持つ必要はありません。どちらも同じ方法で機能するため、1つしか使用しないでください。

+0

本当に助けてくれたあなたの説明をありがとう! –

関連する問題