jeudi 23 avril 2020

Can I use local variables in testCafe?

I am stating using testCafe for one week, and I need to test one page with different logins/profiles.

When I run the test with only one login (without the for loop), it works. but when I try to use the for loop to change the login, it says that there is no user text:

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();
        });
}

The command I used: testcafe edge .\roles_spec.ts

The result I got:

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)

Aucun commentaire:

Enregistrer un commentaire