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

javascript - How to use Puppeteer and Headless Chrome with Cucumber-js - Stack Overflow

programmeradmin0浏览0评论

I am trying to do BDD with cucumber-js and drive the browser testing with Headless Chrome and puppeteer.

Using the documentation from cucumber node example and headless chrome, I get the following errors, the entire code base is avaliable here: github repo.

Errors:

  1. TypeError: this.browser.newPage is not a function
  2. TypeError: this.browser.close is not a function

// 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('');
    });

    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');
    });
});

I am trying to do BDD with cucumber-js and drive the browser testing with Headless Chrome and puppeteer.

Using the documentation from cucumber node example and headless chrome, I get the following errors, the entire code base is avaliable here: github repo.

Errors:

  1. TypeError: this.browser.newPage is not a function
  2. TypeError: this.browser.close is not a function

// 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./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');
    });
});
Share Improve this question edited Oct 5, 2017 at 19:33 YourAboutMeIsBlank asked Oct 5, 2017 at 19:04 YourAboutMeIsBlankYourAboutMeIsBlank 1,9073 gold badges21 silver badges30 bronze badges 1
  • check out this framework - github./igniteram/puppeteer-cucumber-typescript – Ram Pasala Commented Sep 6, 2018 at 17:56
Add a ment  | 

3 Answers 3

Reset to default 2

puppeteer is pletely async, so you have to wait it's initialization before using this.browser.

But setWorldConstructor is sync function, so you can't wait there. In my example I used Before hook

My example: https://gist.github./dmitrika/7dee618842c00fbc35418b901735656b

We created puppeteer-cucumber-js to simplify working with Puppeteer and Cucumber:

  1. Run npm install puppeteer-cucumber-js
  2. Create a features folder in the root of your project
  3. Add a feature-name.feature file with your Given, When, Then statements
  4. Create a features/step-definitions folder
  5. Add JavaScript steps to execute for each of your features steps
  6. Run tests node ./node_modules/puppeteer-cucumber-js/index.js --headless

Source code with a working example on GitHub

Cucumber has been since updated. This is how I have implemented my async puppeteer setup with cucumber. Gist 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
})
发布评论

评论列表(0)

  1. 暂无评论