시스템 알림을 해제하는 방법을 알아 냈지만 앱에 시스템 경고가 표시되지 않으므로 알림이 표시 될 때까지 기다릴 수 없습니다. app.debugDescription 및 app.alerts.count를 사용하여 디버깅하려했지만 운이 없었습니다.위치 알림 (시스템 알림)이 표시 될 때까지 기다리는 방법은 무엇입니까?
답변
addUIInterruptionMonitor:withDescription:handler:
을 사용하여 중단 모니터를 등록하십시오. 시스템 경고가 표시 될 때까지 '대기'하려면 처리기를 사용하여 처리 된 변수를 설정하고 알림을 기다릴 때 앱과의 양호한 상호 작용을 실행합니다.
트리거 인터럽트 모니터가 상호 작용하기 때문에 기다리는 동안 계속해서 앱과 상호 작용해야합니다.
class MyTests: XCTestCase {
let app = XCUIApplication()
func testLocationAlertAppears() {
var hasDismissedLocationAlert = false
let monitor = addUIInterruptionMonitor(withDescription: "LocationPermissions") { (alert) in
// Check this alert is the location alert
let location = NSPredicate(format: "label CONTAINS 'Location'")
if alert.staticTexts.element(matching: location).exists {
// Dismiss the alert
alert.buttons["Allow"].tap()
hasDismissedLocationAlert = true
return true
}
return false
}
// Wait for location alert to be dismissed
var i = 0
while !hasDismissedLocationAlert && i < 20 {
// Do some benign interaction
app.tap()
i += 1
}
// Clean up
removeUIInterruptionMonitor(monitor)
// Check location alert was dismissed
XCTAssertTrue(hasDismissedLocationAlert)
}
}
감사합니다 aletha! –
@Oletha가 쓴대로 addUIInterruptionMonitor
을 사용해야합니다.
여기 까다로운 부분은 시스템 경고 단추가 접근성 식별자를 사용하지 않기 때문에 텍스트를 검색하여이를 탭해야한다는 것입니다. 이 텍스트는 시뮬레이터/장치를 실행하는 언어로 번역됩니다. 영어 옆에 여러 언어로 테스트를 실행하려면 어려울 수 있습니다.
AutoMate framework을 사용하면 간단하게 할 수 있습니다. locationAlert.allowElement.tap()
위의 예에서
func locationWhenInUse() {
let token = addUIInterruptionMonitor(withDescription: "Location") { (alert) -> Bool in
guard let locationAlert = LocationWhenInUseAlert(element: alert) else {
XCTFail("Cannot create LocationWhenInUseAlert object")
return false
}
locationAlert.allowElement.tap()
return true
}
// Interruption won't happen without some kind of action.
app.tap()
// Wait for given element to appear
wait(forVisibilityOf: locationPage.requestLabel)
removeUIInterruptionMonitor(token)
}
자동화가 iOS 시뮬레이터에서 지원하는 모든 언어를 처리 할 수 있기 때문에 가능하다 : 여기 자동화를 사용하여 시스템 경고를 처리하는 방법을 예를 들어 있습니다. 지금까지 시도 무엇 PermissionsTests.swift
: 자동화가 들여다하시기 바랍니다 사용하여 시스템 경고를 처리하는 방법에 대한 자세한 예는
? 참조 : [질문]. [MCVe]를 게시하십시오. 또한 물어보기 전에 2 분 사이트 투어를 완료하지 않으 셨습니다. –