[英]Can a Cucumber feature pass a constant to a step definition?
I have a library of XPATHs for a site whose XPATHs regularly change. 我有一个XPATH库,用于XPATH定期更改的站点。 I've written it because instead of going through every feature file and changing the XPATH it sends, I can simply change the value of the variables I have within my .rb library.
我写的是因为我不是通过每个功能文件并更改它发送的XPATH,而是可以简单地更改我的.rb库中的变量值。
Is it possible to pass these constants to step definitions through the .feature file? 是否可以通过.feature文件将这些常量传递给步骤定义?
Example .feature feature file: 示例.feature功能文件:
Scenario: I want to test a button
When I go to url "blah"
And I click on the XPATH: XPATH_CONSTANT_VARIABLE
Example .rb step definition: 示例.rb步骤定义:
When /^I click on the XPATH: {I DON'T KNOW WHAT TO PUT HERE}$/ do |path|
@driver.find_element(:xpath, path).click
end
Example XPATH .rb library: 示例XPATH .rb库:
XPATH_CONSTANT_VARIABLE = "//*[@id="blahblah"]/div[1]/div/div[2]/div/div[1]/div/div[5]/div/div/div/div[2]"
Your scenarios are very imperative. 您的方案非常紧迫。 I advice you to make them more declarative and don't use (or refer) to XPathes in scenarios.
我建议你让它们更具说明性,不要在场景中使用(或引用)XPathes。 Read:
读:
If you really want to leave your scenarios as they are, you can use: 如果您真的希望保留原有场景,可以使用:
When /^I click on the XPATH: \w+$/ do |constant|
xpath = Kernel.const_get constant
@driver.find_element(:xpath, xpath).click
end
But putting all constants to global space as you did seems ugly to me. 但是把所有常量放到全球空间就像你一样对我来说似乎很难看。 It may be better to put them to YAML file.
将它们放到YAML文件中可能更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.