2017-07-08 32 views
0

我需要在AppUITest中使用推送通知。有什么方法使用自定义授权文件来添加/更新AppUITests目标的设置?iOS - 在UITest中使用推送通知目标

+0

。您的应用没有收到推送通知? –

+0

我的应用程序工作正常。我需要测试推动自动UI测试。没有任何办法吗?谢谢。 –

回答

1

和Xcode 9,你可以通过使用一种称为NWPusher

框架使用您的UITests远程通知要测试你的UITests远程通知你必须做以下步骤:

  1. 添加NWPusher到您的UITest目标
  2. 从Apple的开发人员中心下载APN开发证书并将其添加到您的钥匙串(如果它尚未存在)
  3. 将证书导出为p12文件
  4. 编写测试

我试过了,这是我的测试文件:

我的演示应用程序呈现依赖于它接收推送通知三种不同的模式视图控制器。所以在这个测试中有三个不同的推送通知。

import XCTest 
import PusherKit 

class PushNotificationUITests: XCTestCase { 

    override func setUp() { 
     super.setUp() 
     continueAfterFailure = false 
    } 

    func testPushNotifications() { 
     let app = XCUIApplication() 
     app.launchArguments.append("isRunningUITests") 
     app.launch() 

     // access to the springboard (to be able to tap the notification later) 
     let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") 

     // dismiss the system dialog if it pops up 
     allowPushNotificationsIfNeeded() 

     // get the current deviceToken from the app 
     let deviceToken = app.staticTexts.element(matching: .any, identifier: "tokenLabel").label 

     // close app 
     XCUIDevice.shared.press(XCUIDevice.Button.home) 
     sleep(1) 

     // trigger red Push Notification 
     triggerPushNotification(
      withPayload: "{\"aps\":{\"alert\":\"Hello Red\"}, \"vcType\":\"red\"}", 
      deviceToken: deviceToken) 

     // tap on the notification when it is received 
     springboard.otherElements["PUSHNOTIFICATION, now, Hello Red"].tap() 

     // check if the red view controller is shown 
     XCTAssert(app.staticTexts["Red"].exists) 

     // dismiss modal view controller and close app 
     app.buttons["Close"].tap() 
     XCUIDevice.shared.press(XCUIDevice.Button.home) 
     sleep(1) 

     // trigger green Push Notification 
     triggerPushNotification(
      withPayload: "{\"aps\":{\"alert\":\"Hello Green\"}, \"vcType\":\"green\"}", 
      deviceToken: deviceToken) 

     // tap on the notification when it is received 
     springboard.otherElements["PUSHNOTIFICATION, now, Hello Green"].tap() 

     // check if the green view controller is shown 
     XCTAssert(app.staticTexts["Green"].exists) 

     // dismiss modal view controller and close app 
     app.buttons["Close"].tap() 
     XCUIDevice.shared.press(XCUIDevice.Button.home) 
     sleep(1) 

     // trigger blue Push Notification 
     triggerPushNotification(
      withPayload: "{\"aps\":{\"alert\":\"Hello Blue\"}, \"vcType\":\"blue\"}", 
      deviceToken: deviceToken) 

     // tap on the notification when it is received 
     springboard.otherElements["PUSHNOTIFICATION, now, Hello Blue"].tap() 

     // check if the blue view controller is shown 
     XCTAssert(app.staticTexts["Blue"].exists) 

     // dismiss modal view controller 
     app.buttons["Close"].tap() 
    } 
} 

extension XCTestCase { 
    func triggerPushNotification(withPayload payload: String, deviceToken: String) { 
     let uiTestBundle = Bundle(for: PushNotificationUITests.self) 
     guard let url = uiTestBundle.url(forResource: "pusher.p12", withExtension: nil) else { return } 

     do { 
      let data = try Data(contentsOf: url) 
      let pusher = try NWPusher.connect(withPKCS12Data: data, password: "pusher", environment: .auto) 
      try pusher.pushPayload(payload, token: deviceToken, identifier: UInt(arc4random_uniform(UInt32(999)))) 
     } catch { 
      print(error) 
     } 
    } 

    func allowPushNotificationsIfNeeded() { 
     addUIInterruptionMonitor(withDescription: "“RemoteNotification” Would Like to Send You Notifications") { (alerts) -> Bool in 
      if(alerts.buttons["Allow"].exists){ 
       alerts.buttons["Allow"].tap(); 
      } 
      return true; 
     } 
     XCUIApplication().tap() 
    } 
} 

我写这个的详细blogpost和为什么你的UI测试的目标要求使用推送通知,你可以下载试玩here