如何在 Cucumber-js 中使用 Puppeteer 和 Headless Chrome
How to use Puppeteer and Headless Chrome with Cucumber-js
我正在尝试使用 cucumber-js 进行 BDD,并使用 Headless Chrome 和 puppeteer 驱动浏览器测试。
使用来自 cucumber node example and headless chrome, I get the following errors, the entire code base is avaliable here: github repo 的文档。
错误:
- TypeError: this.browser.newPage 不是函数
- 类型错误:this.browser.close 不是函数
// features/support/world.js
const puppeteer = require('puppeteer');
var {defineSupportCode} = require('cucumber');
function CustomWorld() {
this.browser = puppeteer.launch();
}
defineSupportCode(function({setWorldConstructor}) {
setWorldConstructor(CustomWorld)
})
// features/step_definitions/hooks.js
const puppeteer = require('puppeteer');
var {defineSupportCode} = require('cucumber');
defineSupportCode(function({After}) {
After(function() {
return this.browser.close();
});
});
// features/step_definitions/browser_steps.js
const puppeteer = require('puppeteer');
var { defineSupportCode } = require('cucumber');
defineSupportCode(function ({ Given, When, Then }) {
Given('I am on the Cucumber.js GitHub repository', function (callback) {
const page = this.browser.newPage();
return page.goto('https://github.com/cucumber/cucumber-js/tree/master');
});
When('I click on {string}', function (string, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
Then('I should see {string}', function (string, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
});
puppeteer 是完全异步的,所以你必须等待它初始化后才能使用 this.browser
。
但是setWorldConstructor是sync函数,所以你不能在那里等。在我的示例中,我使用了 Before hook
我的例子:
https://gist.github.com/dmitrika/7dee618842c00fbc35418b901735656b
Cucumber 已更新。这就是我用 Cucumber 实现异步人偶设置的方式。要点 here
const { BeforeAll, Before, AfterAll, After } = require('cucumber');
const puppeteer = require('puppeteer');
Before(async function() {
const browser = await puppeteer.launch({ headless: false, slowMo: 50 });
const page = await browser.newPage();
this.browser = browser;
this.page = page;
})
After(async function() {
// Teardown browser
if (this.browser) {
await this.browser.close();
}
// Cleanup DB
})
我们创建了 puppeteer-cucumber-js 来简化 Puppeteer 和 Cucumber 的使用:
- 运行
npm install puppeteer-cucumber-js
- 在项目的根目录中创建一个
features
文件夹
- 使用您的
Given, When, Then
语句添加一个 feature-name.feature
文件
- 创建
features/step-definitions
文件夹
- 为每个功能步骤添加 JavaScript 个步骤
- 运行 测试
node ./node_modules/puppeteer-cucumber-js/index.js --headless
上带有工作示例的源代码
我正在尝试使用 cucumber-js 进行 BDD,并使用 Headless Chrome 和 puppeteer 驱动浏览器测试。
使用来自 cucumber node example and headless chrome, I get the following errors, the entire code base is avaliable here: github repo 的文档。
错误:
- TypeError: this.browser.newPage 不是函数
- 类型错误:this.browser.close 不是函数
// features/support/world.js
const puppeteer = require('puppeteer');
var {defineSupportCode} = require('cucumber');
function CustomWorld() {
this.browser = puppeteer.launch();
}
defineSupportCode(function({setWorldConstructor}) {
setWorldConstructor(CustomWorld)
})
// features/step_definitions/hooks.js
const puppeteer = require('puppeteer');
var {defineSupportCode} = require('cucumber');
defineSupportCode(function({After}) {
After(function() {
return this.browser.close();
});
});
// features/step_definitions/browser_steps.js
const puppeteer = require('puppeteer');
var { defineSupportCode } = require('cucumber');
defineSupportCode(function ({ Given, When, Then }) {
Given('I am on the Cucumber.js GitHub repository', function (callback) {
const page = this.browser.newPage();
return page.goto('https://github.com/cucumber/cucumber-js/tree/master');
});
When('I click on {string}', function (string, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
Then('I should see {string}', function (string, callback) {
// Write code here that turns the phrase above into concrete actions
callback(null, 'pending');
});
});
puppeteer 是完全异步的,所以你必须等待它初始化后才能使用 this.browser
。
但是setWorldConstructor是sync函数,所以你不能在那里等。在我的示例中,我使用了 Before hook
我的例子: https://gist.github.com/dmitrika/7dee618842c00fbc35418b901735656b
Cucumber 已更新。这就是我用 Cucumber 实现异步人偶设置的方式。要点 here
const { BeforeAll, Before, AfterAll, After } = require('cucumber');
const puppeteer = require('puppeteer');
Before(async function() {
const browser = await puppeteer.launch({ headless: false, slowMo: 50 });
const page = await browser.newPage();
this.browser = browser;
this.page = page;
})
After(async function() {
// Teardown browser
if (this.browser) {
await this.browser.close();
}
// Cleanup DB
})
我们创建了 puppeteer-cucumber-js 来简化 Puppeteer 和 Cucumber 的使用:
- 运行
npm install puppeteer-cucumber-js
- 在项目的根目录中创建一个
features
文件夹 - 使用您的
Given, When, Then
语句添加一个feature-name.feature
文件 - 创建
features/step-definitions
文件夹 - 为每个功能步骤添加 JavaScript 个步骤
- 运行 测试
node ./node_modules/puppeteer-cucumber-js/index.js --headless