Как с помощью CucumberJS/Puppeteer расширить объект Page несколькими сценариями?

Я пишу пакет автоматизации тестирования, используя Puppeteer и CucumberJS.

У меня возникла проблема, когда объект Page не передается между сценариями в файле функций.

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

Это заставляет меня поверить, что во втором сценарии доступ к объекту страницы не осуществляется.

Что я делаю не так?


person Huckleberry Carignan    schedule 03.09.2018    source источник
comment
Вы пытались заменить Before на BeforeAll в index.js?   -  person lloyd    schedule 03.09.2018
comment
ознакомьтесь с этой структурой — github.com/igniteram/puppeteer-cucumber-typescript   -  person Ram Pasala    schedule 06.09.2018


Ответы (1)


С 2 сценариями у вас есть совершенно разные объекты мира. Член, который вы инициировали в первом сценарии, будет воссоздан в функции ловушки второго сценария, что, вероятно, может быть причиной вашей проблемы.

Хук «До» будет выполняться для каждого сценария, поэтому у вас есть два разных браузера в каждом сценарии.

person Lean Prop    schedule 04.09.2018