编写 login() 函数的最终黄金标准是什么?

tes*_*nch 0 javascript authentication testing automation testcafe

我在网上搜索最终的“登录模式”,其中包括登录实际成功的验证。问题 -根据我们在团队中决定的 DRY 和编码约定,我们不允许在 Pagemodels 中使用expect(又名断言)。这是当前的登录方法,有时仍然不稳定。我想问你亲爱的 Automators,你是如何设计包括验证在内的登录的?

/**
 * Actual login function
 */
async performLogin(): Promise<void> {
   console.log(`perform login`);
   await t
   .typeText(this.Email, username, {
      replace: true,
      paste: true
   })
   .typeText(this.Password, password, {
      replace: true,
      paste: true
   })
   .click(this.buttonSignIn)
}

/**
 * Login validation
 */
async login(): Promise<void> {
   await t.wait(7000)
   const getURL: any = ClientFunction(() => window.location.href)
   let currentURL: string = await getURL()

   while (currentURL === basePM.urlLogin) {
      this.performLogin()
      await t.eval(() => location.reload(true))
      currentURL = await getURL()
   }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*aev 5

没有一个有效的例子,很难说任何精确的东西。

wait(7000)看起来有点多余。如果您想确保您在正确的页面上,您可以通过选择器检查元素是否存在。TestCafe 有一个内置的等待机制,可以让你避免使用该wait方法:https : //devexpress.github.io/testcafe/documentation/test-api/built-in-waiting-mechanisms.html 。

awaitthis.performLogin()通话前错过了关键字。片状测试的原因可能在这里。

while语句看起来也是多余的,因为您可以对客户端函数使用智能断言。但是,您网站的某些细节似乎迫使您重新加载页面。一般情况下,登录后不需要重新加载页面。

  • 您可以在函数内使用断言。至于你的第二个问题,有两种选择。您不仅可以验证当前页面 URL,还可以检查当前页面是否包含某些指示登录状态的元素(例如,带有用户名的个人资料链接)。因此,您可以使用这两种方法中的任何一种。 (2认同)