Question

I'm trying to move some of the Capybara selector inside a module helper.

module Helper
  def element
    page.all(:css, '.element_class')
  end

  def sub_element
    find('.sub_element_class')
  end
end

And my test is

scenario 'get the sub element'
  visit 'path'
  element.sub_element.click_button 'Button'

  expect(page).to have_content('something')
end

and I get a NoMethodError:

NoMethodError: undefined method `sub_element' for Capybara::Node::Element:0x006fb54911ef88

but, if I use directly the method 'find' instead of my own method 'sub_element', everything works as expected.

element.find('.sub_element_class').click_button 'Button'

and I can't find in the documentation if it possible to use module's methods on Node Elements.

Was it helpful?

Solution

You need to return self to chain this methods.

module Helper
  def element
    page.all(:css, '.element_class')
    self # Add this
  end

  def sub_element
    find('.sub_element_class')
  end
end

More info about method chaining.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top