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

Viewed 90

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

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

we are using the Page object model to achieve our scenario.

Please let us know how can we achieve this in testcafe. As we suspect, the 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 the below keywords to define our selector but it didn't work.

  1. global

  2. globalthis

    We want to pass the data(fetched web element value) from one testscript to another testscript. Our question is whether it's possible or not

    //page.js

    import { Selector, t } from 'testcafe'; class PageModel { constructor() { global.ClaimNumber = Selector('#Txt_claimnumber'); //selector for Claim Number

    this.DateOfEvent = Selector('#dateofevent'); //selector for Date of Event

    this.DateOfClaim = Selector('#dateofclaim') //selector for Date of Claim

    this.TimeOfEvent = Selector('#timeofevent') //selector for Time of Event

    this.TimeOfClaim = Selector('#timeofclaim') //selector for Time of Claim

    this.ClaimStatus = Selector('#claimstatus') //selector for Claim Status

    this.Save = Selector('#Save'); //selector for Save Button

    }};

    export default new PageModel();

    //test.js

    import { Selector } from 'testcafe';

    import PageModel from './page';

    fixtureGetting Started

    .pagehttps://devexpress.github.io/testcafe/example;

    var claimid;//claimid will be generate after saving a claim

    test('My first test', async t => { await t .typeText(this.DateOfEvent, '20/09/2022')

    .typeText(this.DateOfClaim, '20/09/2022')

    .typeText(this.TimeOfEvent, '12:00')

    .typeText(this.TimeOfClaim, '12:00')

    .typeText(this.ClaimStatus, 'xyz')

    .click(this.Save)

    claimid=global.ClaimNumber.value

    //After saving the claim we want to fetch claimid and want to use that claim id in another testscript

    });

//test1.js

import { Selector } from 'testcafe';

import PageModel from './page';

import Test from './test'

fixtureGetting Started

.pagehttps://devexpress.github.io/testcafe/example;

test('My first test', async t => {

var claimid1='23445'; await t.expect(claimid1).eql('claimid');

//want to verify claimid getting from test.js is equal to claimid from test1.js or not

//this is just an example but our requirement is to use claimid (getting from test.js) for different different operation into test1.js testscript.

});

Could you please tell us how to achieve this scenario.

2 Answers

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

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