3

I have one fixture, "Page1" which I use a random word generator to create a named input box. I will need to use this variable on a different fixture, "Page2". How can I accomplish this? We can't use window for testcafe, and all efforts to export the result in a ReferenceError: fixture is not defined error.

On Page 1, I have such code:

const words = require('random-words');
export let inputName;

fixture('Page1'); 


test('Create Page1', async (t) => {
    await loginPage.login();

    inputName = `input ${words()}`;

and on Page 2

import {inputName} from './page1';

if I take out the import statement everything works.

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
Justin
  • 137
  • 2
  • 10

1 Answers1

4

You can store your variables in a separate file, e.g.:

fixture1.js

import getInputName from './get-input-name';   
 
fixture('Page1');   
 
test('Create Page1', async t => {
    await t.typeText('input', getInputName());
});

fixture2.js

import getInputName from './get-input-name';
 
fixture('Page2');   
 
test('Create Page2', async t => {
    await t.typeText('input', getInputName());
});

get-input-name.js

const words = require('random-words');  
 
let inputName = null;   
 
export default function getInputName () {
    if (!inputName)
        inputName = `input ${words()}`;       
 
    return inputName;
}

To get more info about using helpers in TestCafe, have a look at a recipe in Docs.

Helen Dikareva
  • 1,026
  • 6
  • 7