Protractor-typescript-cucumber框架:从命令行运行多个baseUrl
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Protractor-typescript-cucumber框架:从命令行运行多个baseUrl相关的知识,希望对你有一定的参考价值。
我正在努力解决这个问题我需要通过命令行在不同的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();
},
};
答案
您可以通过--params.key=value
在命令行中指定params,并通过browser.params.key
获取脚本中的传入参数。
1)环境
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"
}
}
};
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();
}
}
以上是关于Protractor-typescript-cucumber框架:从命令行运行多个baseUrl的主要内容,如果未能解决你的问题,请参考以下文章