2017-03-01 117 views
1

XCUITest可以驗證通知標題是否發送到屏幕?XCUITest與通知橫幅進行交互。

我可以爲通知添加輔助功能標識符,但是當橫幅顯示到屏幕時,我遇到了使XCUITest與其交互的問題。我知道XCUITest在與應用程序分開的過程中運行,但是如果它仍然可以與通知進行交互,或者超出了XCUITest的範圍,它是否會受到影響?

謝謝,

回答

0

不幸的是,不幸的是。 XCUITest不提供對通知欄的訪問。對於幾乎所有您可能想到的測試用例場景,Appium都是一個很好的選擇,但是Apple仍然沒有提供測試推送通知的方法。解決此問題的一種方法是強制您的應用發送通知,在顯示通知彈出窗口時進行截圖,然後使用圖像識別技術來驗證通知是否正確(如OpenCV)。這是waaaaay更「解決方法」,然後我想使用,但是迄今爲止我知道的唯一可用的方法,希望這有助於。

2

隨着Xcode 9這現在是可能的。您可以訪問其他應用程序。這包括包含通知橫幅的跳板。

XCUIApplication現在有一個新的初始化程序,它將一個包標識符作爲參數。它爲您提供了使用該捆綁標識符的應用程序的參考。然後,您可以使用普通查詢來訪問UI元素。就像你之前用自己的應用程序做過的一樣。

這是一個測試來檢查,如果當應用程序被關閉正在顯示本地通知

import XCTest 

class UserNotificationUITests: XCTestCase { 

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

    func testIfLocalNotificationIsDisplayed() { 
     let app = XCUIApplication() 
     let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") 

     app.launch() 

     // close app to trigger local notification 
     XCUIDevice.shared.press(XCUIDevice.Button.home) 

     // wait for the notification 
     let localNotification = springboard.otherElements["USERNOTIFICATION, now, Buy milk!, Remember to buy milk from store!"] 
     XCTAssertEqual(waiterResultWithExpectation(localNotification), XCTWaiter.Result.completed) 
    } 
} 

extension UserNotificationUITests { 
    func waiterResultWithExpectation(_ element: XCUIElement) -> XCTWaiter.Result { 
     let myPredicate = NSPredicate(format: "exists == true") 
     let myExpectation = XCTNSPredicateExpectation(predicate: myPredicate, 
                 object: element) 
     let result = XCTWaiter().wait(for: [myExpectation], timeout: 6) 
     return result 
    } 
} 

您可以檢出的演示應用程序,包括本次測試here

您還可以測試遠程通知與UITests。這需要更多的工作,因爲您無法直接從您的代碼安排遠程通知。您可以使用名爲NWPusher的服務。我寫了一篇關於how to test Remote Notifications with Xcode UITests的博文,在github上也有一個demo project