【问题标题】:Using CucumberJS / Puppeteer, how do you extend the Page object with multiple scenarios?使用 CucumberJS / Puppeteer,如何扩展具有多个场景的 Page 对象?
【发布时间】:2018-09-03 01:00:22
【问题描述】:

我正在使用 Puppeteer 和 CucumberJS 编写测试自动化套件。

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

Test_1.features

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

Test_2.features

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

我有一个 world.js

'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)下完成所有步骤时,它就可以工作了。当我将测试分解为多个场景(Test_2.features)时,我得到:

 TypeError: Cannot read property 'click' of undefined

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

我做错了什么?

【问题讨论】:

标签: javascript automated-tests puppeteer cucumberjs


【解决方案1】:

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

每个场景都会执行“Before”钩子,所以每个场景都有两个不同的浏览器。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 2019-10-08
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多