Skip to content

Instantly share code, notes, and snippets.

@lydemann
Last active June 18, 2019 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydemann/8c0afac6aaee4a0db0b59832b275621d to your computer and use it in GitHub Desktop.
Save lydemann/8c0afac6aaee4a0db0b59832b275621d to your computer and use it in GitHub Desktop.
import { protractor } from 'protractor/built/ptor';
import { from, interval, of } from 'rxjs';
import { map, mergeMap, takeWhile } from 'rxjs/operators';
const EC = protractor.ExpectedConditions;
/*
This is used for evaluation an expression up to x times with y timeout per try
Use this to fix fragile expects
*/
export class ExpectHelper {
public static async expectOrRetry<T>(
expectFunc: () => Promise<boolean>,
timeout: number = 5000,
tryCount: number = 60
): Promise<any> {
try {
const res = await expectFunc();
if (!res) {
throw new Error('Evaluation is false.');
}
} catch (error) {
if (tryCount > 0) {
browser.sleep(timeout);
console.log(
`Expression failed, retries left: ${tryCount} on expression ${expectFunc.toString()}`
);
tryCount = tryCount - 1;
this.expectOrRetry(expectFunc, timeout, tryCount);
} else {
// tslint:disable-next-line:no-console
console.error(`Error while evaluating expression on ${expectFunc.toString()}`);
throw error;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment