【问题标题】:How write table inside of cucumber step with table如何用表格在黄瓜步骤内写表格
【发布时间】:2014-02-10 14:19:29
【问题描述】:

我正在为 QA 工程师编写场景,现在遇到了步骤封装等问题。

这是我的场景:

When I open connection And device are ready to receive files I send to device file with params: | name | ololo | | type | txt | | size | 123 |

每个步骤对于使用我的步骤的人来说都很重要。 我需要自动化这些步骤并重复 100 次。 所以我决定创建一个新的步骤,运行 100 次。

  1. 第一个变体是创建包含其他步骤的步骤,例如:
Then I open connection, check device are ready and send file with params 100 times: | name | ololo | | type | txt | | size | 123 |

但是这个版本不合适,因为:

  • 会使用它的人不会明白里面执行了哪些步骤
  • 有时像这样的步骤名称很长

    1. 第二个变体是在参数表中与其他步骤一起创建步骤:
I execute following steps 100 times: | When I open connection | | And device are ready to receive files | | I send to device file |

对于会使用我的步骤和场景的人来说,这将很容易理解。

但我也有一些带参数的步骤,

我需要创建类似两层表的东西:

I execute following steps 100 times: | When I open connection | | And device are ready to receive files | | I send to device file with params: | | | name | ololo | | | | type | txt | | | | size | 123 | |

在我的情况下,这是最好的变体。 但是由于黄瓜无法正确解析它(它作为黄瓜代码不正确)。

如何修复步骤的最后一个示例? (用粗体标记)

黄瓜有什么乐器,对我有帮助吗?

您能否提出一些建议您的解决方案类型?

有人有类似的问题吗?

【问题讨论】:

  • 您真的要打开连接并检查设备是否就绪 100 次吗?据我了解,您的业务要求是发送带有所需参数的文件 100 次,因此您可以在打开连接并检查设备本身准备就绪后执行此操作。否则,我建议您最好使用您的第一个变体。
  • 我想出这些步骤只是为了举例。设备通过适当的协议工作。所以我有诸如“我发送 23.x 消息”、“然后我发送带有参数的 54.x 消息:(此处为表)”、“我收到一些数据”等步骤,发送其他类型的消息。所以协议步骤很多,步骤必须封装可见(很重要)...
  • 如果没有办法,我会修改 Cucumber 源...在 Cucumber::Core::Ast::DataTable 类中创建具有必要功能的新方法。

标签: ruby cucumber scenarios


【解决方案1】:

我决定更改符号“|”参数表中的“/”,在里面。 它并不完美,但它确实有效:

这是场景步骤:

I execute following steps 100 times: | I open connection | | device are ready to receive files | | I send to device file with params: | | / name / ololo / | | / type / txt / | | / size / 123 / |

这是步骤定义:

And /^I execute following steps (.*) times:$/ do |number, table| data = table.raw.map{ |raw| raw.last } number.to_i.times do params = [] step_name = '' data.each_with_index do |line,index| next_is_not_param = data[index+1].nil? || ( data[index+1] && !data[index+1].include?('/') ) if !line.include?('/') step_name = line #p step_name if next_is_not_param step step_name if next_is_not_param else params += [line.gsub('/','|')] if next_is_not_param step_table = Cucumber::Ast::Table.parse( params.join("\n"), nil, nil ) #p step_name #p step_table step step_name, step_table params = [] end end end #p '---------------------------------------------------------' end end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多