【问题标题】:Get cucumber datatable in hooks file using Ruby/Watir使用 Ruby/Watir 在 hooks 文件中获取黄瓜数据表
【发布时间】:2017-07-01 11:41:10
【问题描述】:

第一次来,所以我会尽量做到最易读。我在一个功能文件中有一个测试,它使用数据表对一些数据进行排序,如下所示:

Current cucumber test example

目前我正在使用scenario.test_steps.map(&:name) 将所有步骤(这是必要的,因为与应用程序生命周期软件管理器集成),这就是我得到的:

Cucumber steps got in the hooks file

我的问题是:是否可以在 Before do |scenario| 钩子文件中获取数据表信息?

提前感谢任何提供帮助的人!

【问题讨论】:

    标签: ruby automation cucumber integration watir


    【解决方案1】:

    当迭代scenario.test_steps 时,每个测试步骤都有一个关联的Cucumber::Core::Ast::Step。这包含步骤特定信息,例如步骤名称、数据表等。关联的Ast::Step 将是测试步骤的source 的最后一个元素:

    test_step.source
    #=> [
    #=>   #<Cucumber::Core::Ast::Feature "Feature: Something" (features/something.feature:1)>,
    #=>   #<Cucumber::Core::Ast::Scenario "Scenario: Only a test" (features/something.feature:3)>, 
    #=>   #<Cucumber::Core::Ast::Step "Given : the fields" (features/something.feature:4)>
    #=> ]
    

    要访问Ast::Step 多行参数,请检查multiline_arg。如果已指定数据表,将返回Ast::DataTable。否则,将返回 Ast::EmptyMultilineArgument。您可以通过调用data_table?来检查返回值是否为数据表。

    例如,下面将遍历每个测试步骤并输出数据表(如果已定义):

    Before do |scenario|    
      scenario.test_steps.each do |test_step|
        multiline_arg = test_step.source.last.multiline_arg
        puts multiline_arg.raw if multiline_arg.data_table?
      end
    end
    

    【讨论】:

    • 感谢@Justin Ko 的回复。我完全按照您所说的进行复制和粘贴,但是没有用,所以我使用begin rescue 块做了一些更改,它起作用了!
    猜你喜欢
    • 2012-10-20
    • 2023-04-03
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多