简体   繁体   中英

Can a Cucumber feature pass a constant to a step definition?

I have a library of XPATHs for a site whose XPATHs regularly change. 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.

Is it possible to pass these constants to step definitions through the .feature file?

Example .feature feature file:

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:

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_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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM