【发布时间】:2019-11-07 03:40:37
【问题描述】:
我正在实现一个 mocha 测试脚本来登录和注销特定网页,我的目的是使这个测试脚本模块化。
其实我的主要测试脚本如下;
describe('Test is being started for user : ' +
currentUserInfo.email , function () {
it('Login Test', async function () {
await loginTest(page, currentUserInfo.email,
currentUserInfo.password);
});
it('Logout Test', async function () {
await logoutTest(page);
});
});
而logintest.js 如下;
module.exports = function(page, userName, userPass){
before (async function () {
});
after (async function () {
console.log("Login test is finished");
})
describe('Login Test is being started for user : ' +
userName , function () {
it('Enter Email', async function () {
await page.focus('#username_box')
await page.keyboard.type(userName)
});
it('Enter Password', async function () {
await page.focus('#password_box')
await page.keyboard.type(userPass)
});
it('Click "Login in Here" button', async
function () {
await page.click('input[value="Log in
Here"]'); // With type
await page.waitForNavigation();
});
};
在主测试运行时,logoutTest 函数不会等待完成 loginTest。另外,我尝试使用 Promise 对象,但在这种情况下,我的脚本不会在 LoginTest 下运行。
module.exports = async function(page, userName, userPass){
return new Promise(resolve => {
before (async function () {
});
after (async function () {
console.log("Login test is finished");
resolve(10);
})
describe('Login Test is being started for user : ' +
userName , function () {
it('Enter Email', async function () {
await page.focus('#username_box')
await page.keyboard.type(userName)
});
it('Enter Password', async function () {
await page.focus('#password_box')
await page.keyboard.type(userPass)
});
it('Click "Login in Here" button', async function () {
await page.click('input[value="Log in Here"]'); // With type
await page.waitForNavigation();
});
});
});
};
谢谢
【问题讨论】:
标签: javascript npm mocha.js puppeteer