如何执行方案并在运行下一个方案时再次启动URL,每次都关闭浏览器?

时间:2019-04-08 10:35:03

标签: typescript protractor cucumber

我正在使用protractor-cucumber -typescript框架,其中我的要求是从功能文件中运行2个场景。执行一个方案后,它应关闭浏览器,然后再次在浏览器中启动URL并运行第二个方案。我了解我必须在我的钩子文件中进行相应的更新,但无法识别它。

我尝试调用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并运行下一个方案,因为每个方案都位于不同的页面。

2 个答案:

答案 0 :(得分:0)

在配置中添加以下内容

restartBrowserBetweenTests: true

这会为每个it块重新启动浏览器。

希望它对您有帮助

答案 1 :(得分:0)

我了解你的痛苦。基本上,Javascript不是自动测试的最佳语言,因为很难覆盖提供的API /测试运行程序(对于Python / Java来说很容易)。 如果您将restartBrowserBetweenTests: true添加到您的protractor.conf中,则将在每种情况下重新启动浏览器。但!您正在使用的After钩子将在之后执行-这样您将有空白页作为屏幕截图(浏览器刚刚启动)。 您需要更新后挂钩到以下内容(因为您截图取决于结果-我已对其进行了一些编辑(通过未指定使用restartBrowserBetweenTests的默认值。或直接将其设置为restartBrowserBetweenTests:false ):

After(async function (scenario) {
browser.takeScreenshot().then(function(png) {
      var decodedImage = new Buffer(png, "base64");
      scenario.attach(decodedImage, "image/png");
    });
return browser.restart();
});