【问题标题】:Apply Condition in Cucumber Feature Scenario在 Cucumber 特征场景中应用条件
【发布时间】:2017-07-17 09:14:44
【问题描述】:

我将如何使用带有 selenium 的 Cucumber java 来处理以下类型的场景:

  Scenario: Raise Invoice by User according to Raise Invoice Type.
  When I select the Raise Invoice Type as "RaiseInvoiceType"
  IF RaiseInvoiceType == 'ABC'
     Method ABC()
  else if RaiseInvoiceType == 'XYZ'
     Method XYZ()

“RaiseInvoiceType”是一个变量,取决于单选按钮或下拉菜单。 带条件的黄瓜特征文件和步骤定义类方法如何实现?

【问题讨论】:

  • 向我们展示步骤定义

标签: selenium cucumber qa


【解决方案1】:

背景

Cucumber 功能文件都是为了弥合业务和开发团队之间的对话鸿沟,因此,代码和条件语句不应出现在其中。

解决方案

问题的解决方案是如何编写步骤定义。

以 Cucumber 的 Ruby 实现为例:

When('I select the Raise Invoice Type as "$invoice_type"') do | invoice_type |
  if invoice_type == 'ABC'
     method_abc
  else 
     if invoice_type == 'XYZ'
        method_xyz
     else
        raise 'Unknown invoice type'
     end
  end
end

这将代码和条件语句从功能文件中带出,本质上是应用程序/系统的行为的动态文档

进一步改进

但我什至会更改步骤的措辞:

Scenario Outline: Raise Invoice by User according to Raise Invoice Type.
  When I raise the invoice type "<invoice_type>"
  Then some expected behaviour

Examples:
  | invoice_type |
  | ABC          |
  | XYZ          |

这使我们远离了实现(例如,可能是下拉菜单、单选框或文本框),而更多地转向了系统的现有行为 - 这个场景强调的功能是您应该能够提出一个发票,而不是您应该在选择框中有一个可供选择的选项列表。

【讨论】:

    【解决方案2】:

    这里重要的是两种发票类型之间的区别。每种类型对您的业务都很重要,因此我会为每种类型创建一个步骤,例如

    When I raise an ABC invoiceWhen I raise an XYZ invoice

    在实现步骤定义时,我可能会考虑使用相同的辅助方法来减少代码,例如

    When I raise an ABC invoice' do
      raise_invoice type: 'abc'
    end
    
    When I raise an XYZ invoice' do
      raise_invoice type: 'xyz'
    end
    

    然后有一个辅助方法来处理您如何开具发票。

    def raise_invoice(type: )
      click_radio('invoice', type)
    end
    

    这为您提供了非常简单的步骤定义,在您的步骤定义中没有条件或其他复杂情况,并且提供了一种处理浏览器交互的简单方法。

    请注意以上所有代码都是伪代码/ruby

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多