Home > other >  Store web element's value in a parameter and use it in various js files in testcafe
Store web element's value in a parameter and use it in various js files in testcafe

Time:09-19

In our insurance domain, the below scenario we want to achieve using testcafe:-

1st file :- Login into application 2nd file :- create claim, store claim number into global variable 3rd file :- use that globally declared claim number in all the Testscripts.

we are using Page object model to achieve our scenario.

Please let us know how can we achieve this in testcafe. As we suspect, web element value that we get in 2nd file, gets vanished as soon as the test case gets executed.so how can we pass that web element value in our 3rd file. If possible, please let us know the steps in detail.

we have tried below keywords to define our selector but it didn't work.

  1. global
  2. globalthis

CodePudding user response:

It isn't correct to use information from one test in another one. If you want to prepare something before any test starts, you can use hooks. Also, if you need to reuse auth information, use Roles. It will be great practice.

Please see the following example with a global variable:

//test.js
import { Selector } from 'testcafe';
import PageModel from './page';

 

fixture`Getting Started`
    .page`https://devexpress.github.io/testcafe/example`;

 

test('My first test', async t => {
    await t
        .typeText(global.developerNameSelector, 'John Smith')
        .click('#submit-button')

 

        // Use the assertion to check if the actual header text is equal to the expected one
        .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');
});
//page.js
import { Selector, t } from 'testcafe';

 

class PageModel { 
    constructor() { 
        global.developerNameSelector = Selector('#developer-name'); 
    } 
}; 

 

export default new PageModel();

CodePudding user response:

As my colleague mentioned above, it is very bad practice to use data from one test in another one. However, if it is required, you can use the "global" object in a common JavaScript way to accomplish this task:

test('My first test', async () => {
global.someVar = 'developer-name';
});

test('My second test', async t => {
await t.typeText(`#${global.someVar}`, 'some text');
});

Note that if, for some reason, the order in which the tests are executed changes (for example, in concurrency mode), then you will encounter unexpected behavior.

Also, I just checked your code and found out that you are trying to save the result of the "expect" method call (Assertion object) to your variable. Would you please clarify why? What behavior are you are trying to achieve?

  • Related