Protractor-typescript-cucumber框架:从命令行运行多个baseUrl

问题描述 投票:0回答:1

我正在努力解决这个问题我需要通过命令行在不同的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();
},

};

angular typescript protractor cucumber
1个回答
0
投票

您可以通过--params.key=value在命令行中指定params,并通过browser.params.key获取脚本中的传入参数。

1)环境

exports let config = {
    prod: {
        baseUrl: "https://www.asde.com",
        loginCred:
            {
                username: "[email protected]",
                password: "asbcde"
            }        
    },

    test: {
        baseUrl: "https://www.facebook.com",
        loginCred: {
            username: "[email protected]",
            password: "asbcdefd"
        }
    }
};

2)黄瓜钩.ts

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);
});

3)量角器配置

export const config: Config = {

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

    params: {   
        env: 'test'
    }
};

4)从命令行指定env

protractor config.js --params.env=prod

5)HomePageObject

import { config } from './config/env.ts';
export class HomePageObject { 

    constructor(env: string) {
        this.env = browser.params.env;
        this.username = config[this.env].username;
        this.password = config[this.env].password;
    }

    login() {
        element(by.css('input.username')).sendKeys(this.username);
        element(by.css('input.password')).sendKeys(this.password);
        element(by.css('button.login')).click();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.