简体   繁体   English

Testcafe - 测试多个页面

[英]Testcafe - Test multiple pages

I'm testing a website with a login page and then other pages only visible after login.我正在测试一个带有登录页面的网站,然后其他页面仅在登录后可见。

I created a login_page.js model as follows:我创建了一个login_page.js模型如下:

// my_login_page_model.js
import { Selector, t } from 'testcafe';

export default class LoginPage {
  constructor () {
    this.email = Selector('#email');
    this.password = Selector('#password');
    this.signin = Selector('#signinButton');
  }
}

and then I created similar page models for the pages after login.然后我在登录后为页面创建了类似的页面模型。

On my test file, I then instantiate a login page object, run the login page test在我的测试文件中,然后实例化一个登录页面对象,运行登录页面测试

// my_test_spec.js
test('login in', async t => {
  await t
    .typeText(loginPage.email, config.name)
    .typeText(loginPage.password, config.password)
    .click(loginPage.signin);
  await t.expect(getURL()).contains(pageUrl);
});

test('second test', async t => {
console.log('Creating a new experience');
  await t
    .click(// click a button on the following page);
});

The problem is the second test starts from the login page and of course, it fails because it can't find the ids of the page after login.问题是第二次测试从login page开始,当然失败了,因为它在登录后找不到页面的id。

How can this be done?如何才能做到这一点?

Every test starts from the scratch and that means clean profile, that why you are not authorized, when second test starts.每个测试都从头开始,这意味着干净的配置文件,这就是为什么您在第二次测试开始时未被授权的原因。 The simplest way to fix is to move you login code at the beginning of your second test.最简单的修复方法是在第二次测试开始时移动登录代码。 However, it's not good practice, because you probably need authorization before majority of your test.但是,这不是一个好习惯,因为您可能需要在大部分测试之前获得授权。 To solve this, you can choose one of this:要解决此问题,您可以选择以下之一:

1) Http Auth for some cases 1) 某些情况下的Http Auth

2) Roles 2) 角色

3) Use beforeEach Hook . 3) 使用beforeEach Hook Just put Login code to this hook and it's will execute before every test in fixture.只需将登录代码放入这个钩子,它就会在夹具中的每个测试之前执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM