【发布时间】: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
这让我相信在第二种情况下没有访问页面对象。
我做错了什么?
【问题讨论】:
-
你试过在 index.js 中用 BeforeAll 替换 Before?
标签: javascript automated-tests puppeteer cucumberjs