3

Page model-

import { Selector } from 'testcafe';

export default class Page {
    constructor () {
        this.nameInput = Selector('#developer-name');
    }
}

Page model2-

import { Selector } from 'testcafe';

export default class Page2 {
    constructor () {
        this.nameInput2 = Selector('#tester-name');
    }
}

common spec to instantiate the above-

import Page from './page-model';
import Page2 from './page-model2';

const page = new Page();
const page2 = new Page2();

can we have a single file\spec to instantiate above two Pages and then access all Selectors across Pages from a single reference variable?

1 Answer 1

2

If you don't want to create new instances of page models in each test, you can export them directly in model.js:

test.js

import { pageOne, pageTwo } from './model';

fixture `fixture 1`
    .page `http://example.com`;

test(`test`, async t => {
    await t
    .click(pageOne.el1)
    
    //...navigation to page two
    
    .click(pageTwo.el2);
});

model.js

import { Selector } from 'testcafe';

class PageOne {
    constructor () {
        this.el1 = Selector('#el1');
        //....
    }
}

class PageTwo {
    constructor () {
        this.el2 = Selector('#el2');
        //....
    }
}

export const pageOne = new PageOne();
export const pageTwo = new PageTwo();

UPDATE

Also you can organize all selectors in a separate module as follows:

test.js

import selectors from './selectors';

fixture `fixture 1`
    .page `http://example.com`;

test(`test`, async t => {
    await t
    .click(selectors.el1)
    .click(selectors.el2);
});

selectors.js

import { Selector } from 'testcafe';

export default {
    el1: Selector('#el1'),
    el2: Selector('#el2'),
    //....
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response. But I am looking to have a common reference spec where in i only need to import one function and access the Selectors across multiple files\pages. something like this in below- I am not sure if TestCafe allow us to achieve this? ' import { commonSelectorSpec } from './model'; fixture fixture 1 .page http://example.com; test(test, async t => { await t .click(commonSelectorSpec.el1) //...navigation to page two .click(commonSelectorSpec.el2); }); '
Thanks for the clarification. See the updated answer above.
Thanks for the response. On top of this can u please suggest how to do it for one more separate page containing the Selectors?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.