首页 > 解决方案 > TestCafe expect() 范围

问题描述

我是 TestCafe 的新手,我正在努力使用角色以“干净的方式”实现测试。我一直在阅读 TestCafe 的文档,并受到这个“教程” https://github.com/qualityshepherd/testcafe-example的启发,用于创建我的测试。

我必须编写一个测试来测试用户是否正确登录。为此我想使用角色。这是我的测试脚本的架构,我将解释为什么我会这样做:

让我们深入研究一些代码。这是我的login.test.js文件:

import basePage from '../../pages/base-page';

fixture `Account management`;

test('Login with valid account',
    async (t) => {
        await t
            .useRole(validUser) // this logs me in using a Role defined in another file
            .expect(basePage.userSection.exists).ok() // check if exists
            .expect(basePage.userSection.child(1).textContent).eql('somevalid.username'); // check if value is the one I expect
    }
);

// other tests...

角色.js文件:

import { Role } from 'testcafe';
import loginPage from '../pages/login-page.js';

// I will obviously not display any valid data on this post
export const validUser = Role(
    loginPageUrl,
    async (t) => {
        await loginPage.login(t, someValidEmail, someValidPassword);
    },
    { preserveUrl: true }
);

我的base-page.js文件:

// imports 

const baseUrl = 'http://localhost:3000/';

const basePage = {
    baseUrl,
    userSection: $('div.navigation-account-dropdown.js-user-menu'), // some section on my navbar
    [... other stuff you don't need to know about]
}

export default basePage;

我的login-page.js文件:

import { Selector as $, t } from 'testcafe';
import basePage from './base-page';

// FIXME: I do not want to use .expect(...) here but in the proper test file (login.test.js)
const loginPage = {
    url: `${basePage.baseUrl}signin`,
    usernameInput: $('#at-field-email'),
    passwordInput: $('#at-field-password'),
    loginBtn: $('#at-pwd-form').find('button'),

    async login(t, username, password) {
        await t
            .typeText(this.usernameInput, username)
            .typeText(this.passwordInput, password)
            .click(this.loginBtn) // I want to stop the logic here and do the assertion on the proper test.js file
            .expect(basePage.userSection.exists).ok(); // I do not want to do assertions here but I have to do it otherwise tests fails
    }
}

export default {
    ...basePage,
    ...loginPage
};

问题是当我.expect(basePage.userSection.exists).ok();login-page.js文件中注释该行时,测试没有按预期运行。它填写表单,单击提交,然后什么也没有发生(我仍在登录页面,它已重新加载)。所以我在这里添加了一个断言以使其“工作”。但我不想要任何断言,它不应该在这里,而是在实际的测试文件中。我的主要问题是:如何从我的 login-page.js 文件中删除任何断言。为什么我必须这样做 .expect 那里才能通过测试?

您对如何构建项目测试有任何提示/建议(我必须编写很多测试,这就是我试图分离一些逻辑的原因)?

标签: testingmeteorautomated-teststestcafeend-to-end

解决方案


您描述的行为是出乎意料的。作为一种可能的解决方案,请使用以下preserveUrl选项:https ://devexpress.github.io/testcafe/documentation/test-api/authentication/user-roles.html#optionspreserveurl 。

如果没有帮助,我们需要详细研究问题。请分享您的示例并使用以下表格在 TestCafe github 存储库中创建一个单独的问题:https ://github.com/DevExpress/testcafe/issues/new?template=bug-report.md 。

如果您无法在此处分享您的项目,您可以将其发送至 support@devexpress.com。

请注意,我们的政策禁止我们在未经网站所有者事先书面批准的情况下访问内部资源。如果您希望我们直接在您身边进一步研究问题,请要求网站所有者向我们发送 (support@devexpress.com) 书面确认。它必须允许 DevExpress 人员出于研究/测试/调试目的远程访问网站及其内部资源。


推荐阅读