3

I need some help with a regular expression in a Cucumber step definition file. Many of my steps are of the type:

Given I am on the search page

I use this general pattern for most of my step definitions, and use the default Webrat regex to pick it up that looks like this:

Given /^(?:|I )am on (.+)$/ do |page_name|
     visit path_to(page_name)
end

The problem is that I need to handle a page titled 'results' differently, and I do not know how to modify the above regex to exclude lines that say 'Given I am on the results page'.

2
  • More specific regex (for the result page) should win therefore you don't need to change the generic regex. Commented Oct 19, 2010 at 19:12
  • That was actually one of the first things I tried, but I still get an error about both regex's matching. Commented Oct 19, 2010 at 22:09

1 Answer 1

1

You can use a negative look ahead to resolve this one:

Given /^(?:|I )am on (?!the results)(.+)$/ do |page_name|
    p page_name
end

Given /^I am on the results page$/ do
   p 'We matched the results page'
end
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I ended up not using the regular expression, and instead writing individual regexes for each case, but I just tried this and it definitely works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.