getting undefined method `click_button' for nil:NilClass (NoMethodError)

237 Views Asked by At

For selecting dropdown using capybara : getting undefined method

click_button' for nil:NilClass (NoMethodError) ./features/pages/cm_dash_board.rb:187:inchoose_role' ./features/pages/cm_dash_board.rb:147:in set_role' ./features/step_definitions/cm_dash_board_steps.rb:58:in/^

Method:
  def choose_role(role_name)
    click_button CHOOSE_ROLE
    find(ROLEDROPDOWN).all('li').select{|role| role.text == role_name}.first.click_button 
  end 
Feature file:And(/^I assign "([^"]*)" role to the "([^"]*)" user$/) do |email, role_name|
  @sw.cm_dash_board.set_role(email,role_name)
end
1

There are 1 best solutions below

0
Thomas Walpole On

That error tells you that find(ROLEDROPDOWN).all('li').select{|role| role.text == role_name} isn't matching any elements. The first find is finding something (or you'd have a different error), but then either all('li') is finding no elements or the select is filtering out all elements.

As a side node, you really shouldn't be using select for this anyway and should instead be specifying it in your all call like

find(ROLEDROPDOWN).find('li', exact_text: role_name).click_button

That may well still fail, but at least the error will tell you why it's failing