如何在测试之间传递变量数据

时间:2018-07-24 00:18:35

标签: automated-tests e2e-testing testcafe

我正在尝试执行与此post on TestCafe

类似的操作

我正在helper.js文件中生成随机电子邮件。我想使用此随机电子邮件登录test.js文件。

这就是我在helper.js中创建电子邮件的方式

var randomemail = 'test+' + Math.floor(Math.random() * 10000) + '@gmail.com'

这就是我要在test.js文件中使用它的方式

.typeText(page.emailInput, randomemail)

我没有运气就尝试了几件事。我该如何在test.js文件中使用生成的电子邮件?

1 个答案:

答案 0 :(得分:3)

两个选项:

1)使用灯具上下文对象(ctx)

/******************************************************
** Function: runGame
** Description: Runs game and keeps track of players
** Parameters: # of players
** Pre-Conditions: c is an integer from 1 to 9
** Post-Conditions:
******************************************************/
void runGame(int players) {
    Player p = new Player[players]; //needs to be deleted at the end
    int dice[] = { -1, -1, -1, -1, -1 };
    int category; // used to hold category chosen
    while (isGameOver(p)) {
        for (int i = 0; i < players; i++) {
            rollDice(dice); //uses reference
            p[i].scoreBoard= updateScore(p[i], dice);
            p[i].catsLeft--;
        }
    }
}

/******************************************************
** Function: rollDice
** Description: rolls dice, prints array and either rerolls
** Parameters: int[] dice
** Pre-Conditions:
** Post-Conditions:
******************************************************/
void rollDice(int (&dice) [5]) {
    int again;
    string indices; // indices of dice to reroll
    cout << "Your dice are" << endl;
    for (int i = 0; i < 5; i++) {
        dice[i] = rand() % (6) + 1;
        cout << dice[i];
    }
    for (int i = 0; i < 2; i++) {
        cout << "Roll again? Type anything except 0 to go again." << endl;
        cin >> again;

        if (again) {
            cout << "Type each index without a space that you would like to reroll";
            cin.ignore();
            getline(cin, indices);
            for (int i = 0; i < indices.length(); i++) {
                dice[(int)indices[i] - '0'] = rand() % (6) + 1;
            }
        }
        else
            break;
    }

}

2)在灯具外部声明一个变量

fixture(`RANDOM EMAIL TESTS`)
    .before(async ctx => {
        /** Do this to initialize the random email and if you only want one random email 
         *  for the entire fixture */

        ctx.randomEmail = `test+${Math.floor(Math.random() * 1000)}@gmail.com`;
    })
    .beforeEach(async t => {
        // Do this if you want to update the email between each test

        t.fixtureCtx.randomEmail = `test+${Math.floor(Math.random() * 1000)}@gmail.com`;
    })

test('Display First Email', async t => {
    console.log(t.fixtureCtx.randomEmail);
})

test('Display Second Email', async t => {
    console.log(t.fixtureCtx.randomEmail);
})