我认为我完全理解SpecFlow背后的概念和想法,但即使在阅读Secret Ninja Cucumber Scrolls,The Cucumber Book并浏览各种论坛后,我仍然不确定可重用性的路径。< / p>
我们的方案已经符合各种指南
我们的步骤必须遵守以下准则(一些特定于SpecFlow):
但即使我们使用正则表达式占位符,我们仍然会得到相同步骤的大量变体。特别是如果某些事情不重要,你就不应该提到这些变化的规则。是的,在内部这些步骤重复使用,但不在场景中。
例如考虑以下场景:
Feature: Signing where both persons are physically available
@Smoke
Scenario: Show remaining time to sign based on previous signature
Given a draft proposal
And the first signature has been set
When I try to set the second signature
Then the remaining time to sign should be shown
@Smoke
Scenario: Re-signing of the first proposal
Given a signature that has not been set within the configured time
And the first signature has just been re-signed
When I try to set the second signature
Then the remaining time should start over
将两个“给定”步骤合并为一个并放松一些可重用性会更好吗?
其他一些例子:
Feature: Conditionally show signatures to be signed
@Smoke
Scenario: Show the correct signature for a proposal with a night shift
Given I have a proposal for the day shift
When I change it to the night shift
Then I should only be able to sign for the night shift
@Smoke
Scenario: Show additional signature when extending the shift
Given I have a suspended proposal for the night shift
When I extend the period to the day shift
Then I should confirm extening the period over the shift
我在这里错过了一个基本概念吗?
答案 0 :(得分:12)
这不是答案,而是一些提示:
你需要一个类来代表带有装饰的测试中的许可证:
class PermitDescription{
bool suspended;
bool draft;
}
创建转换器方法:
[StepArgumentTransformation("permit")]
public PermitDescription CreateSimple(){
return new PermitDescription();
}
[StepArgumentTransformation("draft permit")]
public PermitDescription CreateDraft(){
return new PermitDescription() { draft = true; }
}
[StepArgumentTransformation("suspended permit")]
public PermitDescription CreateSuspended(){
return new PermitDescription() { suspended = true; }
}
您现在可以获得需要许可的更灵活的步骤定义:
[Given(@"I have a (.*) for the day shift")]
public void Something(PermitDescription p)
{ ... }
匹配到:
Given I have a permit for the day shift
Given I have a draft permit for the day shift
Given I have a suspended permit for the day shift
当然这是一种可以滥用的工具,但在某些情况下它可以提供帮助。
答案 1 :(得分:0)
添加@ gaspar-nagy的回答 它遵循C编程中的类设计模式。在一个共同的类组共享公共属性/方法的任何地方,这些属性/方法可以重构为基类。
在我们的SpecFlow测试中看起来是常见的浏览器操作在基类中:
Login()
Logout()
NavigateToUrl(string url)
UserHasPermission(string permission)
WaitForElementToAppearById(string id)
WaitForElementToAppearByClass(string class)
每个方法都可以有一个或多个Given / When / Then属性,如@ gasper-nagy所述。
另一种证明非常有价值的技术是在.features和它们各自的C#步骤文件之间共享变量是使用ScenarioContext。
例如,每当调用Login()
来启动基于浏览器的测试时,我们都会这样做:
ScenarioContext.Current.Set<IWebDriver>(driver, "driver")
然后在需要驾驶员的任何其他地方,可以通过以下方式获得:
var driver = ScenarioContext.Current.Get<IWebDriver>("driver")
这使得步骤可以重复使用,例如用于验证的用户输入测试,您可能决定传递正在验证的元素,如下所示:
ScenarioContext.Current.Set<IWebElement>(element, "validation-element")