Xcode 9 a
introduit de nouvelles astuces avec XCTWaiter
Le cas de test attend explicitement
wait(for: [documentExpectation], timeout: 10)
Délégués d'instance de serveur pour tester
XCTWaiter(delegate: self).wait(for: [documentExpectation], timeout: 10)
La classe de serveur renvoie le résultat
let result = XCTWaiter.wait(for: [documentExpectation], timeout: 10)
switch(result) {
case .completed:
case .timedOut:
case .incorrectOrder:
case .invertedFulfillment:
case .interrupted:
}
Avant Xcode 9
Objectif c
- (void)waitForElementToAppear:(XCUIElement *)element withTimeout:(NSTimeInterval)timeout
{
NSUInteger line = __LINE__;
NSString *file = [NSString stringWithUTF8String:__FILE__];
NSPredicate *existsPredicate = [NSPredicate predicateWithFormat:@"exists == true"];
[self expectationForPredicate:existsPredicate evaluatedWithObject:element handler:nil];
[self waitForExpectationsWithTimeout:timeout handler:^(NSError * _Nullable error) {
if (error != nil) {
NSString *message = [NSString stringWithFormat:@"Failed to find %@ after %f seconds",element,timeout];
[self recordFailureWithDescription:message inFile:file atLine:line expected:YES];
}
}];
}
USAGE
XCUIElement *element = app.staticTexts["Name of your element"];
[self waitForElementToAppear:element withTimeout:5];
Rapide
func waitForElementToAppear(element: XCUIElement, timeout: NSTimeInterval = 5, file: String = #file, line: UInt = #line) {
let existsPredicate = NSPredicate(format: "exists == true")
expectationForPredicate(existsPredicate,
evaluatedWithObject: element, handler: nil)
waitForExpectationsWithTimeout(timeout) { (error) -> Void in
if (error != nil) {
let message = "Failed to find \(element) after \(timeout) seconds."
self.recordFailureWithDescription(message, inFile: file, atLine: line, expected: true)
}
}
}
USAGE
let element = app.staticTexts["Name of your element"]
self.waitForElementToAppear(element)
ou
let element = app.staticTexts["Name of your element"]
self.waitForElementToAppear(element, timeout: 10)
LA SOURCE
NSThread.sleepForTimeInterval(1)
devrait fonctionner