量角器型脚本黄瓜框架:从命令行运行多个baseUrl

时间:2018-12-31 00:27:58

标签: angular typescript protractor cucumber

我正在为这个问题而苦苦挣扎,我需要通过命令行在不同的baseUrl上运行基于typescript-protractor-cucumber的项目,但我无法执行。您能否在同一个github项目上提供一些信息或链接来帮助我。我正在config.ts文件中设置baseUrl,并尝试使用命令行从hook.ts文件中调用它们。

Hook.ts和config.ts

const { BeforeAll, After, AfterAll, Status } = require("cucumber");
import * as fs from "fs";
import { browser } from "protractor";
import { config } from '../config/config';
import { build$ } from "protractor/built/element";
let env = browser.params.environment;

BeforeAll({timeout: 100 * 1000}, async () => {
//await  browser.get(config.params.baseUrl.prod);
await browser.get(env);

});

After(async function(scenario) {
if (scenario.result.status === Status.PASSED) {
    // screenShot is a base-64 encoded PNG
     const screenShot = await browser.takeScreenshot();
     this.attach(screenShot, "image/png");
}

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,

capabilities: {
    browserName: "chrome",
},

framework: "custom",
frameworkPath: require.resolve("protractor-cucumber-framework"),

specs: [
    "../../features/Search.feature",
],
 params:{
baseUrl:{
 prod:'https://www.abdec.com',
     test: 'https://www.facebook.com'
 }
},
onPrepare: () => {
    // switch(browser.params.baseUrl){
    //    case 'firsturl': browser.get("https://www.abdc.com")
    //    default: browser.get("https://www.facebook.com")
    // }
    browser.ignoreSynchronization = true;
    browser.manage().timeouts().implicitlyWait(12000);
    browser.manage().window().maximize();
    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: "@SampleScenario",
},

onComplete: () => {
    Reporter.createHTMLReport();
},

};

1 个答案:

答案 0 :(得分:0)

您可以通过<?php require "boot.php"; echo json_encode(Braintree_ClientToken::generate()); 在命令行中指定参数,并通过--params.key=value在脚本中获取传入的参数。

1)环境

browser.params.key

2)黄瓜hooks.ts

exports let config = {
    prod: {
        baseUrl: "https://www.asde.com",
        loginCred:
            {
                username: "asde.sin@ud.com.com",
                password: "asbcde"
            }        
    },

    test: {
        baseUrl: "https://www.facebook.com",
        loginCred: {
            username: "asde.sin12@ud.com",
            password: "asbcdefd"
        }
    }
};

3)量角器配置

const { BeforeAll, After, AfterAll, Status } = require("cucumber");
import { browser } from "protractor";
import { config } from '../config/env.ts';

BeforeAll({timeout: 100 * 1000}, async () => {
    let env = browser.params.env;
    // you need to put above line into this function, rather than
    // put at the top of import section, the hooks.ts will be loaded by
    // nodejs before browser instance inited. Otherwise, the `browser`
    // variable is null, it leads `browser.params.env` fail.

    await browser.get(config[env].baseUrl);
});

4)从命令行指定环境

export const config: Config = {

    seleniumAddress: "http://127.0.0.1:4444/wd/hub",
    ...

    params: {   
        env: 'test'
    }
};

5)HomePageObject

protractor config.js --params.env=prod