最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass data from a seprate file in my testcafe file - Stack Overflow

programmeradmin1浏览0评论

This is a kind of test I am trying to run in testcafe v1.0.1, I am relativelt new to this.

This my test.js file where I have three different test cases R03, R05,R06 and all search for the element in a webpage using withText() function.

Is there anyway for me make a config file(json/js) where I can save the inputs for Year_1,Year_2,Year_3, & Location_1, Location_2, Location_3 and use it in my current .js file.

`import { Selector } from 'testcafe';

fixture `First Fixture`
    .page ``;

test('R03', async t => {
    await t  
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Year_1'))
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_1'))
});

test('R05', async t => {
    await t
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Year_2'))
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_2'))
});

test('R06', async t => {
    await t
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Year_3'))
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_3'))
});

This is a kind of test I am trying to run in testcafe v1.0.1, I am relativelt new to this.

This my test.js file where I have three different test cases R03, R05,R06 and all search for the element in a webpage using withText() function.

Is there anyway for me make a config file(json/js) where I can save the inputs for Year_1,Year_2,Year_3, & Location_1, Location_2, Location_3 and use it in my current .js file.

`import { Selector } from 'testcafe';

fixture `First Fixture`
    .page `http://devexpress.github.io/testcafe/example`;

test('R03', async t => {
    await t  
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Year_1'))
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_1'))
});

test('R05', async t => {
    await t
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Year_2'))
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_2'))
});

test('R06', async t => {
    await t
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Year_3'))
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_3'))
});
Share Improve this question edited Mar 8, 2019 at 14:08 Alex Skorkin 4,2743 gold badges27 silver badges48 bronze badges asked Mar 7, 2019 at 11:27 Ayush SinghaniaAyush Singhania 16513 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You cannot store inputs in some .js file. But, you can save inputs' selectors and use them in your tests. See example:

element-selectors.json

{
  "developerNameInput": "#developer-name",
  "populateBtn": "#populate"
}

test.js

fixture `Fixture`
    .page('https://devexpress.github.io/testcafe/example/');

const elementSelectors = require('./element-selectors.json');   
    
test('test', async t => {
    await t.typeText(elementSelectors.developerNameInput, 'Peter Parker');
}); 

Found a solution

Create a config.js file
config.js

export default {
        // Preview Paramters for R03 Report
        year:       '2019',
        Location:   'Dublin',
};

Now, let your main text file be test.js
test.js

import { Selector } from 'testcafe';
import data from "./config.js"; 

fixture `First Fixture`
    .page `http://devexpress.github.io/testcafe/example`;

test('R03', async t => {
    var year = data.year.toString();
    var location = data.location.toString();
    await t  
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText(year))
        .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText(location))
});

The solution worked for me perfectly.
If someone had a better solution, I would be happy to implement and test it out.

发布评论

评论列表(0)

  1. 暂无评论