【问题标题】:How to close the browser each time after executing a scenario and launching the url again on running the next scenario?如何在执行场景后每次关闭浏览器并在运行下一个场景时再次启动 url?
【发布时间】: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


    【解决方案1】:

    在您的配置中添加以下内容

    restartBrowserBetweenTests: true
    

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

    希望对你有帮助

    【讨论】:

    • 我正在使用量角器黄瓜框架而不是 jasmine。hooks.ts 文件一切正常。正如我尝试的那样,您建议的命令但它没有关闭浏览器并在另一个浏览器中运行新场景.我尝试运行的两个场景都失败了
    【解决方案2】:

    我理解你的痛苦。基本上 Javascript 不是自动化测试的最佳语言,因为很难覆盖提供的 API/测试运行器(这对于 Python/Java 来说很容易)。 如果您在 protractor.conf 中添加restartBrowserBetweenTests: true,这将在每个场景后重新启动浏览器。但! After 您正在使用的钩子将在之后执行 - 因此您将拥有空白页面作为屏幕截图(因为浏览器刚刚启动)。 您需要将 After hook 更新为以下内容(因为您根据结果截取屏幕截图 - 我已经对其进行了一些编辑(使用默认值 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();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多