【发布时间】:2019-04-08 10:35:03
【问题描述】:
我正在使用 protractor-cucumber -typescript 框架,我的要求是从功能文件中运行 2 个场景。执行一个场景后,它应该关闭浏览器,然后再次在浏览器中启动 URL 并运行第二个场景。我知道我必须在我的 hooks 文件中进行相应的更新,但无法识别它。
我尝试调用 browser.get("URL");在 beforeEach 场景中并关闭浏览器(即) browser.quit();在 afterEach 但那不起作用(VError:在每个钩子出错之后)。
hooks.ts:
const { BeforeAll, After, Before, Status } = require("cucumber");
import * as fs from "fs";
import { browser } from "protractor";
import { config } from "../config/config";
import { async } from "q";
import { AfterAll } from "cucumber";
BeforeAll({ timeout: 1000 * 1000 }, async () => {
await browser.get(config.baseUrl);
});
After(async function (scenario) {
if (scenario.result.status === Status.FAILED) {
// screenShot is a base-64 encoded PNG
const screenShot = await browser.takeScreenshot();
this.attach(screenShot, "image/png");
}
else {
var attach = this.attach;
return browser.takeScreenshot().then(function (png) {
var decodedImage = new Buffer(png, "base64");
return attach(decodedImage, "image/png");
})
}
});
AfterAll({ timeout: 1000 * 1000 }, async () => {
await browser.quit();
});
config.ts:
import * as path from "path";
import { browser, Config } from "protractor";
import { Reporter } from "../support/reporter";
const jsonReports = process.cwd() + "/reports/json";
export const config: Config = {
seleniumAddress: "http://127.0.0.1:4444/wd/hub",
SELENIUM_PROMISE_MANAGER: false,
baseUrl: "url",
capabilities: {
browserName: "chrome",
},
framework: "custom",
frameworkPath: require.resolve("protractor-cucumber-framework"),
specs: [
"../../features/*.feature",
],
onPrepare: () => {
browser.ignoreSynchronization = true;
browser.manage().window().maximize();
browser.manage().deleteAllCookies();
browser.manage().timeouts().implicitlyWait(20000);
Reporter.createDirectory(jsonReports);
},
cucumberOpts: {
compiler: "ts:ts-node/register",
format: "json:./reports/json/cucumber_report.json",
require: ["../../typeScript/stepdefinitions/*.js",
"../../typeScript/support/*.js"],
strict: true,
tags: "(@Scenario1 or @Scenario2 )",
},
onComplete: () => {
Reporter.createHTMLReport();
},
};
预计在每个场景之后关闭浏览器并启动 URL 并运行下一个场景,因为每个场景都是不同的页面。
【问题讨论】:
标签: typescript protractor cucumber