【问题标题】:In TestCafe is there a way to retry navigating to a URL without a hard wait在 TestCafe 中,有一种方法可以重试导航到 URL 而无需等待
【发布时间】:2019-04-11 17:51:00
【问题描述】:

我们的项目使用 TestCafe 进行 e2e 测试。由于导航到 URL 的环境间歇性失败(Ping 或其他问题)。 testCafe 隔离模式不是正确的解决方案,因为一次成功就表示成功。我正在尝试为自动脚本编写一个解决方案,以便在未加载正确的 URL 时重试。我想实现类似这样的期望语句,而不是“期望”导致测试失败或使用硬 .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);
 }

所需的解决方案是在失败前重试登录功能 3 次。

【问题讨论】:

    标签: automated-tests e2e-testing web-testing testcafe navigatetourl


    【解决方案1】:

    为避免调用login 方法后的硬等待,一种解决方案是在login 方法中添加以下断言:

    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;
      }
    }
    

    所以你的主循环更简单:

    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);      
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2014-05-08
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多