2 Replies - 2113 Views - Last Post: 06 November 2019 - 01:33 PM Rate Topic: -----

#1 peterbonney   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-November 19

Capybara/Cucumber/Regex

Posted 06 November 2019 - 06:39 AM

Hi,
Firstly, thanks for the add :^:/>

Secondly - I'm self-taught in Ruby/Selenium/Capybara/Regex so still trying to get my head around it all!

I'm hoping someone might be able to help me with my automated test.
I'm using Capybara/Cucumber the website under test has a CMS, when a user adds a component in the CMS the component is assigned an auto-generated class eg:
"StyledTabsSection-dk9iyv-4 gkLKye"
the format is the same, so I have a regex that splits the class into 3 groups -
^(Styled)(\w*)(-\S*\s\S*)$


What I'm not sure about is how I write the ruby/Capybara DSL to expect the class to contain the 2nd group in my regex

I've found a code snippet online that I think will be a good start:

* m = "John 31".match /\w+ (\d+)/
* m[1]


I'm guessing I can do something like:

module = displayed_module.match "^(Styled)(\w*)(-\S*\s\S*)$"
module[2]


Then("I can see the {displayed_module} module on the page") do |displayed_module|
page.find_by_class(module[2]).tag_name
end


But how do I write expect page.class to contain(module[2])?

Thanks in advance

Have a nice day :bigsmile:/>

Is This A Good Question/Topic? 0
  • +

Replies To: Capybara/Cucumber/Regex

#2 peterbonney   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-November 19

Re: Capybara/Cucumber/Regex

Posted 06 November 2019 - 09:30 AM

Update:

I've made a bit of headway, and I'm now getting some kind of sensible error.

Here's my code:

Then("I can see the {string} module on the page") do |string|
  page.all('div', class: "Styled" + m[2] + m[3])
  end


Which is throwing this error:
undefined method `[]' for nil:NilClass (NoMethodError)
      ./features/step_definitions/frontend/modules.rb:14:in `"I can see the {string} module on the page"'
      features/frontend/Modules/module.feature:6:in `Then I can see the 'FeaturedRecipe' module on the page'


I'm hoping it's a simple syntax error that'll fix it!! :/

I don't see an edit option, I pasted the wrong code!

Then("I can see the {string} module on the page") do |string|
  m=string.match "^(Styled)(\w*)(-\S*\s\S*)$"
  page.all('div', class: "Styled" + m[2] + m[3])
  end

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Capybara/Cucumber/Regex

Posted 06 November 2019 - 01:33 PM

If m is nil, that means your regex didn't match. So let's look at why that might be the case:

Unknown escape sequences in string literals are treated just as if the backslash wasn't there. \w and \S aren't valid escape sequences in string literals, so they're treated as just "w" and "S" respectively. Similarly \s in a string literal simply becomes a space, not a pattern matching multiple types of white space.

Generally, if you want to use regex escape characters in a string literal, you'll need to escape the backslashes. But the much better solution would be to just use a regex literal instead of a string literal (/^(Styled)(-\S*\s\S*)$/).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1