문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top