Javascript 使用CucumberJS/Puppeter,如何在多个场景中扩展页面对象?

Javascript 使用CucumberJS/Puppeter,如何在多个场景中扩展页面对象?,javascript,automated-tests,puppeteer,cucumberjs,Javascript,Automated Tests,Puppeteer,Cucumberjs,我正在使用Puppeter和CucumberJS编写一个测试自动化套件 我遇到了一个问题,页面对象没有在功能文件中的场景之间传递 测试功能 Feature: Login and hit some buttons In order to show my issue As a stackOverflow users I want be able to login and select some buttons Scenario: Log in Given I

我正在使用Puppeter和CucumberJS编写一个测试自动化套件

我遇到了一个问题,页面对象没有在功能文件中的场景之间传递

测试功能

Feature: Login and hit some buttons
    In order to show my issue
    As a stackOverflow users
    I want be able to login and select some buttons

  Scenario: Log in
      Given I have navigated to to the App "foobar"
      When I have selected the Imports button 
      I should see the Console on the page title

      When I have selected the Imports button 
      Then I should see the Imports on the page title
测试功能

Feature: Login and hit some buttons
    In order to show my issue
    As a stackOverflow users
    I want be able to login and select some buttons

  Scenario: Log in
      Given I have navigated to to the App "foobar"
      When I have selected the Imports button 
      I should see the Console on the page title

  Scenario: Navigate from the Imports page/list to the Console
      When I have selected the Imports button 
      Then I should see the Imports on the page title
我有一个世界

'use strict';

const { setWorldConstructor } = require("cucumber");
const puppeteer = require("puppeteer");

class thisApp {
  constructor() {
      this.app = "";
  }

// Open browser
async openBrowser() {
  this.browser = await puppeteer.launch({
  headless: false,
  slowMo: 25 
  });
  this.page = await this.browser.newPage();
}

async appLogIn(username_password) {
   await this.page.waitFor(settings._3000);
   await this.navigateToAppLoginPage();
   await this.appEnterUsernamePassword(username_password);
   await this.page.click('[data-test-button-SignIn="true"]');
   await this.page.waitFor(settings._3000);
}

// Select the Imports button
async selectButtonNavigationPaneImports() {
  await this.page.click('[data-test-button-navigation-pane-imports="true"]');
}

// Select the Console button
async selectButtonNavigationPaneConsole() {
  await this.page.click('[data-test-button-navigation-pane-console="true"]');
}

}
setWorldConstructor(ePayApp);
我并没有把所有的步骤都放在这里——我只是想举个例子

app_steps.js

// Login
 Given(/^I have navigated to to the App "([^"]*)"$/, async 
  function(username_password){
  return this.appLogIn(username_password);
});

// import button
 When(/^I have selected the Imports button$/, async 
  function(){
  return this.selectButtonNavigationPaneImports();
});

// console button
 When(/^I have selected the Console button$/, async 
  function(){
  return this.selectButtonNavigationPaneConsole();
});
我有index.js

const { BeforeAll, Before, AfterAll, After } = require('cucumber');
const puppeteer = require('puppeteer');

Before(async function() {
   const browser = await puppeteer.launch({ headless: false, slowMo: 50 });
  this.browser = browser;
  this.page = page;
})
现在,当我在一个场景(Test_1.features)下完成所有步骤后,它就可以工作了。当我将测试分解为多个场景(测试2.功能)时,我得到:

这让我相信在第二种情况下,页面对象没有被访问


我做错了什么?

在两个场景中,您拥有完全不同的世界对象。在第一个场景中启动的成员将在第二个场景的钩子函数中重新创建,这可能是问题的原因


“Before”钩子将针对每个场景执行,因此每个场景中都有两个不同的浏览器。

您是否尝试在index.js中用BeforeAll替换BeforeAll?请查看此框架-
 TypeError: Cannot read property 'click' of undefined