保持黄瓜共同步骤的最佳做法

时间:2012-10-16 08:45:46

标签: ruby cucumber automated-tests watir watir-webdriver

我正在使用cucumber-watir-webdriver进行自动化。我有以下目录结构:

|features
 -|feature1
 --|1.feature
 --|step_definitions
 ---|1.rb
 -feature2
 --|2.feature
 --|step_definitions
 ---|2.rb

等等。我需要知道在1.rb2.rb中减少冗余的最佳做法是什么。 feature1feature2完全不同,因此我无法将两者合并到一个目录中。 还有一些部分,其中特征线是相同的,但步骤中的执行是不同的,因此如果它们在一起会产生歧义。

我需要知道1.rb2.rb中是否存在一些共同点,我应该在哪里提出保留常见步骤定义的最佳做法。

2 个答案:

答案 0 :(得分:2)

将您的功能分离到单个目录中是很好的,但最好将*_step.rb个文件放在step_definitions目录下的一个features目录中。您可以将这两个功能的共同步骤放入common_steps.rb文件中(更好的是类似login_steps.rbtest_data_creation_steps.rb而不是common_steps.rb)。

要将其转换为与问题相同的样式目录结构图,我建议如下:

|features
 -|feature1
 --|1.feature
 -feature2
 --|2.feature
 -|step_definitions
 --|1.rb
 --|2.rb
 --|common_steps.rb <-- put your common steps in here

答案 1 :(得分:1)

Cucumber仅搜索当前或后续目录中的步骤定义。 所以我们不能在两个功能目录下面有共同的步骤定义目录或文件, 我找到了一个解决方案

|features
 -|feature1
 --|1.feature
 --|step_definitions
 ---1.rb
 -feature2
 --|2.feature
 --|step_definitions
 ---2.rb
 -|common_steps.rb <-- keep common steps in here

现在加载此步骤定义添加

 require "#{File.dirname(__FILE__)}/../../common_steps.rb"
在你的1.rbs中

你也可以有更长的目录结构然后你可以在每个这样的目录中保存common_steps.rb文件,其中包含以下功能的常用步骤,你可以要求以前的common_steps.rb文件 -

 require "#{File.dirname(__FILE__)}/../common_steps.rb"

此代码。 这将使您的目录结构和step_definition文件保持清洁