当我在黄瓜中输入演员姓名时,我如何使用数据驱动或动态用户名和密码,它应该使用相应的密码,可能来自json文件或取决于演员的东西。
所以当我在功能文件中输入
时// example.feature
Feature: Add item and place the order for that item
In order to wear something new
As an online shopping customer
I want to be able to add an item to my shopping bag and then place the order
Scenario: Add an item to shopping bag to place the order using Paypal
Given that "Bruno" is logged in to his "admin" account
When he searches and adds an item from "men" section to his shopping bag
Then he can place the order
// example.steps.ts
import { Actor } from "serenity-js/lib/screenplay";
import { BrowseTheWeb } from "serenity-js/lib/screenplay-protractor";
import { protractor } from "protractor";
import { Start, AddItemsToShoppingBag } from "../website-model/src/index";
import { Authenticate } from '../website-model/src/ability/authenticate';
protractor.browser.ignoreSynchronization = true;
export = function selectItemSteps() {
let actor = Actor.named('Bruno').whoCan(BrowseTheWeb.using(protractor.browser));
this.Given(/^that "([^"]*)" is logged in to his "([^"]*)" account$/, function (name: string, pageUrl: string) {
return actor.attemptsTo(
Start.LaunchUrl(pageUrl, name),
);
});
this.When(/^he searches and adds an item from "([^"]*)" section to his shopping bag$/, function (gender:string) {
return actor.attemptsTo(
AddItemsToShoppingBag.called(gender)
);
});
this.Then(/^he can place the order$/, function () {
return actor.attemptsTo(
// PlaceOrder.hjgkhaksdjh()
);
// expect(james.toSee(thankYouPageElementsMap(lbl_thank_you).Displayed)).eventually.equal('Thank You');
});
}
// login-user.ts
import { Task, step, PerformsTasks } from "serenity-js/lib/screenplay";
import { Click, Enter } from "serenity-js/lib/screenplay-protractor";
import { homePageElementsMap } from "../interactions/html-elements/pages-elements-maps/home-page-elements-maps";
import { loginPageElementsMap } from "../interactions/html-elements/pages-elements-maps/login-page-elements-map";
export class LoginUser implements Task {
static called(name: string): LoginUser {
return new LoginUser(name);
}
@step('{0} Login Page - Login Bruno')
performAs(actor: PerformsTasks): PromiseLike<void> {
return actor.attemptsTo(
Click.on(homePageElementsMap.lnk_login),
Enter.theValue('test@test.com').into(loginPageElementsMap.txt_login_email),
Enter.theValue('password111').into(loginPageElementsMap.txt_login_pwd),
Click.on(loginPageElementsMap.btn_login)
);
}
constructor(private name: string) {
}
}
答案 0 :(得分:0)
实施步骤
Given that "Bruno" is logged in to his "admin" account
随机选择一个用户。 您可以从文件中随机选择它们,也可以从Enum或Collection的等效类型中随机选择它们。
注意:目前您在步骤定义之外分配“Bruno”。我认为这是一种不好的做法,因为测试的实现与你的步骤无关(即,事情发生在他们之外),这可能导致不正确的功能文件/意外行为。例如: 如果您将步骤更改为
Given that "Bob" is logged in to his "customer" account
这将不会自动生效,因为你仍然将演员硬编码为布鲁诺。
您仍然需要在步骤之间共享状态(即方案中的所有步骤都需要知道哪个actor登录到哪个帐户)
答案 1 :(得分:0)
我使用一些奇怪的语法采用以下方法。
与应用程序中的大多数内容相比,用户非常不寻常,因为他们知道应用程序没有(他们的密码)。因此,要登录,您无法使用灯具或工厂从数据库中获取现有用户。
我在测试代码中所做的是专门为用户创建一个类。我称之为TUser
。在我的测试世界中,我只通过他们的名字识别用户,然后我生成其他所有内容,包括他们的密码。因此,如果有一个用户Bruno,我的代码将
TUser.new(first_name: Bruno)
向我提供我的用户并将其存储在变量中,让我们为此示例@bruno
@bruno
来填写详细信息,例如.fill_in(:密码,带:@bruno.password)并且执行此操作的cuke会读取类似
的内容
Given there is a user Bruno
When Bruno signs in
Then Bruno should ...
**以上都是红宝石**
您可以在one of my sample projects中查看更详细的版本。一旦你超越了他们最初的陌生感,拥有Txxx类来创建特定实体的测试版本是非常强大的。