int actualTimeToWait = timeToWait == null || timeToWait <= 0 ? DEFAULT_WAIT_TIMEOUT : timeToWait;
int totalWait = 0;
boolean status;
ElementDescriptor descriptor = new ElementDescriptor( locator, position, type );
ElementDescriptor parent =
parentType != null ? new ElementDescriptor( parentLocator, parentPosition, parentType ) : null;
String successMessage = "exist";
String failedMessage = "did not exist";
if ( checkVisibility && expectedStatus ) {
successMessage = "appear";
failedMessage = "did not appear";
}
else if ( checkVisibility ) {
successMessage = "disappear";
failedMessage = "did not disappear";
}
else if ( !expectedStatus ) {
successMessage = "not exist";
failedMessage = "dit not stop existing";
}
LOG.debug( "Waiting for {}{} to " + successMessage, descriptor, parent != null ? " in " + parent : "" );
do {
if ( parent != null ) {
ElementStub parentStub = elementHandleService.find( parent );
if ( exists( parentStub ) ) {
descriptor.setParent( parentStub );
status = checkVisibility ? isVisible( elementHandleService.find( descriptor ) ) : exists(
elementHandleService.find( descriptor ) );
}
else {
status = false;
}
}
else {
status = checkVisibility ? isVisible( elementHandleService.find( descriptor ) ) : exists(
elementHandleService.find( descriptor ) );
}
if ( status != expectedStatus ) {
browser.waitFor( WAIT_INCREMENT );
totalWait += WAIT_INCREMENT;
}
}
while ( status != expectedStatus && totalWait < actualTimeToWait );
if ( status != expectedStatus ) {
LOG.warn( "{}{} " + failedMessage + " within {} ms", descriptor, parent != null ? " in " + parent : "",
actualTimeToWait );
fail( descriptor.toString() + ( parent != null ? " in " + parent.toString() : "" ) + " " + failedMessage + " within " + actualTimeToWait + " ms" );
}
else {
LOG.debug( "{}{} " + successMessage + "ed after {} ms", descriptor, parent != null ? " in " + parent : "",
totalWait );
}