2016-05-30 84 views
0

我寫了一個測試,應該等待15秒才能評估一個條件。它目前等待時間更短,並立即進入評估塊,產生錯誤。 waitWithTimeout之後的seconds參數似乎被忽略。EarlGrey GREYCondition waitWithTimeout:15不等待15秒

我的測試代碼:

- (void)testAsyncEvent { 

    [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Button")] 
     performAction:grey_tap()]; 

    // Wait for the main view controller to become the root view controller. 
    BOOL success = [[GREYCondition conditionWithName:@"Wait for label to change text" 
     block:^{ 

     NSError *error; 
     [[EarlGrey selectElementWithMatcher:grey_text(@"Delayed Appearance")] 
      performAction:grey_tap() 
        error:&error]; 

     if ([error.domain isEqual:kGREYInteractionErrorDomain] && 
      error.code == kGREYInteractionElementNotFoundErrorCode) { 
      return NO; 
     } 

     return YES; 

    }] waitWithTimeout:15]; 

    GREYAssertTrue(success, @"Label text should be changed after 5 seconds. "); 
} 

這裏是按鈕輕擊動作:

- (IBAction)buttonPressed:(id)sender 
{ 
    self.titleLabel.text = @"Button Pressed"; 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 4 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), 
    ^(void) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
     self.titleLabel.text = @"Delayed Appearance"; 
     }); 
    }); 
} 

titleLabel的文本應該4秒通過調度異步後更改爲「出現延遲」。但是測試中的模塊會很快啓動,儘管設置爲15秒。 (並且因爲沒有找到具有這種文本的元素而失敗)。

+0

我通過禁用GRAY API上的同步來解決此問題,如下所示:[[GREYConfiguration sharedInstance] setValue:@(NO) forConfigKey:kGREYConfigKeySynchronizationEnabled];它現在等待我指定的全部金額。所以我想知道是什麼導致它在啓用同步API時提前觸發.... – Alex

回答

2

我不認爲這就是API的意圖。該API用於在滿足條件的情況下等待X秒。超時中的seconds參數應該等待滿足條件多長時間。如果到那時還沒有滿足條件,它將超時並返回NO。如果你真的只是想等待15秒使用: [[NSRunLoop currentRunLoop] runUntilDate:]

而且,由於你使用dispatch_after,可以增加dispatch_after調用的跟蹤時間: [[GREYConfiguration sharedInstance] setValue:@(5.0) forConfigKey:kGREYConfigKeyDispatchAfterMaxTrackableDelay];

只記得重置爲默認之後不再希望在未來的通話時間爲5.0秒後追蹤分派情況。