使用CucumberJS / Puppeteer,如何使用多个场景扩展Page对象?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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
这让我相信在第二种情况下没有访问页面对象。
我究竟做错了什么?
答案
在2个场景中,您拥有完全不同的World对象。您在第一个场景中启动的成员将在第二个场景的钩子函数中重新创建,这可能是您的问题的原因。
“之前”将为每个场景执行钩子,因此每个场景中有两个不同的浏览器。
以上是关于使用CucumberJS / Puppeteer,如何使用多个场景扩展Page对象?的主要内容,如果未能解决你的问题,请参考以下文章