Robert Price
Robert Price

Reputation: 249

In TestCafe is there a way to retry navigating to a URL without a hard wait

Our project uses TestCafe for e2e tests. Due to the environment navigating to a URL intermittently fails (Ping or other issues). The testCafe quarantine-mode isn't the correct solution because a single success indicates a success. I'm trying to code a solution for the automated script to retry when the correct URL doesn't load. I'd like to implement something like this expect a statement, without the 'expect' causing a test to fail or use a hard .wait(30000)

await t.expect(getLocation()).contains('/page', { timeout: 30000});

 test('Should login and navigate to desired URL', async t => {
     console.log('Login Page');
     await login.login('userName', 'password', '/page');

     for (let i = 0; i < 3; i++) {
         await t.wait(30000);
         url = await getUrl();
         if (!url.includes('/page')) {
             console.log('Retrying Login ' + (i + 1) + ' of 3');
             await login.login('userName', 'password', '/page);
             // there is a delay before the page loads
             // .wait(30000);  <== trying to avoid this if possible
             // await t.expect(getLocation()).contains('/page',  { timeout: 30000});  <== would prefer something like this without the causing a test failure
             url = await getUrl();
         } else {
             console.log('Login Valid');
             i = 3;
         }
     }

     console.log('Location Page');
     await t.expect(getLocation()).contains('/page',  { timeout: 30000});
     // ... script continues...

 //---------------------------

 export async function getUrl() {
     const getLocation = ClientFunction(() => document.location.href);
     const url = getLocation();
     return url;
 }

 async login(username: string, password: string, endpoint: string) {
         let url = await setUrl(environment);
         url = url + endpoint;
         console.log(url);
         await t
            .wait(5000)
            .navigateTo(url)
            .expect(this.userName.exists).ok('username field exists', {timeout: 20000})
            .expect(this.userName.hasAttribute( 'disabled')).notOk('username field enabled', {timeout: 20000})
            .hover(this.userName)
            .typeText(this.userName, username)
            // ----
            .expect(this.password.exists).ok('password field exists', {timeout: 2000})
            .expect(this.password.hasAttribute( 'disabled')).notOk('username field enabled', {timeout: 2000})
            .hover(this.password)
            .typeText(this.password, password)
            // ----
            .expect(this.submitBtn.exists).ok('submit button field exists', {timeout: 2000})
            .expect(this.submitBtn.hasAttribute( 'disabled')).notOk('username field enabled', {timeout: 2000})
            .hover(this.submitBtn)
            .click(this.submitBtn);
 }

The desired solution would retry the login function 3 times before failing.

Upvotes: 2

Views: 1779

Answers (1)

hdorgeval
hdorgeval

Reputation: 3030

To avoid the hard wait after calling the login method, a solution could be to add the following assertions at the login method:

await t
  .expect(this.submitBtn.exists).notOk({timeout: 30000});

for (let i = 0; i < 30 ; i++) {
  await t.wait(1000);
  url = await getUrl();
  if (url.includes(endpoint)) {
    return;
  }
}

so your main loop is more simple:

for (let i = 0; i < 3; i++) {
  url = await getUrl();
  if (url.includes('/page')) {
    console.log(`Login Valid at step ${i}`);
    break;
  }
  console.log(`Login Invalid at step ${i}, retrying ...`);
  await login.login('userName', 'password', '/page);      
}

Upvotes: 4

Related Questions