我正在使用jest + puppeteer,并且我有一个想在整个项目中重复使用的代码。
我使用以下指令: https://jestjs.io/docs/en/getting-started
//adminLogin2.js
const admLog = function admLog () {
return
page.goto(data.config.env.host);
page.waitForSelector(data.selectors.admin.auth.input_login);
page.click(data.selectors.admin.auth.input_login);
page.type(data.selectors.admin.auth.input_login, data.credentials.business_email.login);
page.click(data.selectors.admin.auth.form_button_first);
// second step
page.waitForSelector(data.selectors.admin.auth.input_login_password);
page.click(data.selectors.admin.auth.input_login_password);
page.type(data.selectors.admin.auth.input_login_password, data.credentials.business_email.password);
page.click(data.selectors.admin.auth.form_button_second);
page.waitForSelector(data.selectors.admin.auth.business_login_button);
page.click(data.selectors.admin.auth.business_login_button);
page.waitForSelector(data.selectors.admin.auth.business_body);
}
module.exports = admLog;
//test
const data = require('../config');
const admLog = require('../struct/Login/adminLogin2');
describe('GetPackage :: Auth', () => {
it('Admin Email', async () => {
await admLog();
});
});
可以无例外地运行测试,但在无头:错误模式下什么也没发生。
应该解决什么问题?
答案 0 :(得分:0)
async/await
函数中添加admLog
。return
语句以防止Automatic semicolon insertion 因此,最终的adminLogin2.js
文件应如下所示:
//adminLogin2.js
const admLog = async function() {
await page.goto(data.config.env.host);
await page.waitForSelector(data.selectors.admin.auth.input_login);
await page.click(data.selectors.admin.auth.input_login);
await page.type(data.selectors.admin.auth.input_login, data.credentials.business_email.login);
await page.click(data.selectors.admin.auth.form_button_first);
// second step
await page.waitForSelector(data.selectors.admin.auth.input_login_password);
await page.click(data.selectors.admin.auth.input_login_password);
await page.type(data.selectors.admin.auth.input_login_password, data.credentials.business_email.password);
await page.click(data.selectors.admin.auth.form_button_second);
await page.waitForSelector(data.selectors.admin.auth.business_login_button);
await page.click(data.selectors.admin.auth.business_login_button);
await page.waitForSelector(data.selectors.admin.auth.business_body);
}
module.exports = admLog;