Rspec: Capybara select not working Rspec: Capybara select not working selenium selenium

Rspec: Capybara select not working


I think you almost had it with xpath, but I try to avoid it due to how brittle it can be.

I use the following code, though recently I'm only running my tests with capybara-webkit rather than selenium:

option = @brand.namefind('#campaign_brand_id').find(  'option', text: /#{option}/i).select_option

I use the text as a selector, as to me this is what the user sees and therefore is what a feature test should look for.Also, I've used a regex to match the text to make it case insensitive - this is to prevent failing tests due to fickle UI changes such as up/down casing of words. It also saves you from the awkward problem of the css text-transform messing with the displayed vs. actual case of the text.


Update

If the problem is still persisting, you will have to do some debugging. My suggestion is the pry debugger gem. Add it to your gemfile and include the line binding.pry in the scenario above the line which is failing.If I remember correctly, selenium runs only in the foreground, so you will be able to use the interactive pry console while seeing the page.

First try finding the select box (find('#campaign_brand_id')) and its options, then see what the selenium browser does while you are running the commands.

If you still don't have any luck, maybe these questions will help:

  • Are there any errors or is it failing silently
  • Is there any custom JS / CSS modifying your select boxes?
  • Try interacting with the page via execute_script (see the selenium driver docs for details)


use select without brackets:

select @brand.name, from: 'campaign[brand_id]'

or

select @brand.name, from: 'campaign_brand_id'

you can use id or name for option from

but if your form customized and real select is hidden, than you have to use another case