我已经使用for循环编写了一些测试,最近发现了这个方便的文档页面,该页面描述了如何编写数据驱动的测试。
https://devexpress.github.io/testcafe/documentation/recipes/create-data-driven-tests.html
我现在正在尝试重构测试,但是遇到了问题。该测试的目的是作为一堆不同的帐户登录,然后验证是否存在某些页面元素。 (我认识到这是一个沉重的使用锤子,但是我们的应用程序拥有大量的权限,开发新功能时常常会忘记组合,因此这似乎是了解实际情况的最快方法。实际用户的屏幕)。
我的旧测试看起来像这样:
test('Account manager', async (t) => {
const existingItems = [
[mediaSidePanel.customize, 'Customize'],
[mediaSidePanel.stats, 'Stats'],
[mediaSidePanel.download, 'Download'],
[mediaSidePanel.delete, 'Delete'],
];
const notExistingItems = [
[mediaSidePanel.adminTools, 'Admin Tools'],
];
await t
.useRole(advAccountManager)
.navigateTo(`https://${accountKey}.wistia.io/medias/${mediaHashedID}`);
await Promise.all(existingItems.map(async item => await t
.expect(item[0].exists).ok(`${item[1]} should exist for an Account Manager`)));
await Promise.all(notExistingItems.map(async item => await t
.expect(item[0].exists).notOk(`${item[1]} should not exist for an Account Manager`)));
});
该测试工作正常,但在测试中存在循环的明显问题除外:我需要掌握全面的诊断消息以确保知道哪个元素实际上是失败的,更糟糕的是,如果循环中的某些早期操作失败,则测试结束,并且我无法知道以后是否会出现故障。
我开始尝试通过将所有现有/不存在的项目拉入一个单独文件中定义的数组来重构此代码,并写成这样:
import * as dataSet from '../helpers/rolePermissions';
fixture `Advanced Account Manager`
.page `https://app.wistia.io/logout`
.beforeEach(async (t) => {
await t
.useRole(advAccountManager);
});
dataSet.advAccountManager.project.forEach(data => {
test.page `https://${accountKey}.wistia.io/projects/${projectHashedID}`(`Project - ${data.name}`, async t => {
if (data.present) {
await t
.expect(await data.selector.exists).ok(`${data.name} should exist for an Account Manager`);
}
else {
await t
.expect(await data.selector.exists).notOk(`${data.name} should not exist for an Account Manager`);
}
});
});
它的完美之处在于,它消除了最大的问题,即使较早的测试失败了,也可以继续运行测试。但是,它引入了一个更大的问题。现在这要慢得多,因为每次迭代测试时都必须登录。如您所见,我已经在使用Roles来加快速度,但是相对而言它仍然很慢。如果它最终不会成功,我不想继续沿着这个重构道路前进。
有没有办法同时兼顾两个方面?理想情况下,我想执行以下操作:
TestCafe会在每次测试之前重新加载页面,以避免由于测试彼此之间的相互影响而导致的不确定行为。但是,有一个[[experimental和undocumented功能禁用了此机制。根据您的情况,您可以尝试使用fixture.disablePageReloads
方法,如下所示:
fixture `Advanced Account Manager`
.page `https://app.wistia.io/logout`
.disablePageReloads
.beforeEach(async (t) => {
await t
.useRole(advAccountManager);
});
请使用,后果自负。