我声明使用 testCafe 一星期,并且我需要使用不同的登录名/配置文件测试一个页面。
当我仅使用一次登录(没有 for 循环)运行测试时,它可以工作。但是当我尝试使用 for 循环更改登录名时,它说没有用户文本:
import 'testcafe';
import { Selector, ClientFunction } from 'testcafe';
var user = ['admin','sales.test'];
var pass = ['admin','Test@123'];
var role = ['Admin','Sales'];
var x;
for ( x=0; x < user.length; x++)
{
fixture('Compass - Main page - Profile: ' + role[x])
.page('http://localhost:3000/')
.beforeEach(async t => {
//login
await t.typeText(Selector('input').withAttribute('name','username'), user[x], {
paste: true,
replace: true,
});
await t.typeText(Selector('input').withAttribute('name','password'), pass[x], {
paste: true,
replace: true,
});
await t.click(Selector('button').withAttribute('tabindex','0'));
})
.afterEach(async t => {
//logout
await t.click(Selector('#logoutBtn'));
});
test('Check if the Main page is loading (button debug).', async t => {
await t.expect(
Selector('#toggleNotifier').exists,
).ok();
});
test('Check if the Organization page is loading...', async t => {
await t.click(Selector('a').withAttribute('href','#organizations'));
await t.expect(
Selector('a').withAttribute('href','/#/organizations/new').exists,
).ok();
});
}
我使用的命令:testcafe edge .\roles_spec.ts
我得到的结果:
PS C:\ThinkOn\Compass_Test\Test1> testcafe edge .\roles_spec.ts
Using locally installed version of TestCafe.
Running tests in:
- Microsoft Edge 17.17133 / Windows 10
Compass - Main page - Profile: Admin
× Check if the Main page is loading (button debug).
1) - Error in fixture.beforeEach hook -
The "text" argument is expected to be a non-empty string, but it was undefined.
Browser: Microsoft Edge 17.17133 / Windows 10
12 | fixture('Compass - Main page - Profile: ' + role[x])
13 | .page('http://localhost:3000/')
14 | .beforeEach(async t => {
15 |
16 | //login
> 17 | await t.typeText(Selector('input').withAttribute('name','username'), user[x], {
18 | paste: true,
19 | replace: true,
20 | });
21 | await t.typeText(Selector('input').withAttribute('name','password'), pass[x], {
22 | paste: true,
at <anonymous> (C:\ThinkOn\Compass_Test\Test1\roles_spec.ts:17:21)
at <anonymous> (C:\ThinkOn\Compass_Test\Test1\roles_spec.ts:8:71)
at __awaiter (C:\ThinkOn\Compass_Test\Test1\roles_spec.ts:4:12)
at fixture.page.beforeEach (C:\ThinkOn\Compass_Test\Test1\roles_spec.ts:14:31)
at <anonymous> (C:\ThinkOn\Compass_Test\Test1\node_modules\testcafe\src\api\wrap-test-function.js:17:28)
at TestRun._executeTestFn (C:\ThinkOn\Compass_Test\Test1\node_modules\testcafe\src\test-run\index.js:295:19)
at TestRun._runBeforeHook (C:\ThinkOn\Compass_Test\Test1\node_modules\testcafe\src\test-run\index.js:316:31)
at TestRun.start (C:\ThinkOn\Compass_Test\Test1\node_modules\testcafe\src\test-run\index.js:344:24)
最佳答案
这是变量作用域的问题。在您的代码中,x 变量是使用 var 语句声明的,并且具有全局作用域。由于 beforeeach 钩子(Hook)是异步执行的,因此它使用 for 循环完成后 x 的值,并且 user[x] 表达式将是未定义的。
要避免这种情况,请在 for block 中使用 let 语句:
for(let x = 0; x++; x< users.length)
更多信息请参阅 https://www.typescriptlang.org/docs/handbook/variable-declarations.html#block-scoping
关于testing - 我可以在 testCafe 中使用局部变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61395709/