0

I am trying to use capybara (ruby bindings) to select an item from a dropdown that does not have an id or unique class, but I seem to be unable to do so.

The select box in question looks like this:

<select data-bind="options: environments, optionsText: 'name', value: selectedEnvironment,   optionsCaption: 'Choose...'" class="form-control">

Trying to do a:

select("option",:from "#panel > div.panel-body > form > div:nth-child(1) > select")

does not work, but I am able to find the select field using a

page.find("#panel > div.panel-body > form > div:nth-child(1) > select")

The documentation says that the select method expects an id, name or label of the select box, but surely there must be a way of selecting something from a dropdown that is more specific than this. Is there another method I can use other than select() or do I have to go back to using pure selenium?

Metareven
  • 822
  • 2
  • 7
  • 26
  • If you do this in context of testing some application and you don't know name or label of item to select you probably should rethink your tests. If that's not a test suite why not to use selenium. In the end Capybara's readme declares: "Capybara helps you test web applications..." – lompy Apr 29 '14 at 11:42
  • I am doing this in the context of testing some application, but at the same time, this is not the app that I am testing. In order to fake a log in to the correct environment I need to use a tool created by a third party. As of right now I have created an automated version of doing this on an old version of this tool, but I now want to update it to run on the newer version so that we won't need to host 2 versions of this tool. In the old version these select fields had IDs, but now they don't. – Metareven Apr 29 '14 at 11:47
  • If you want to test this particular log in step from browser point of view then stick with selenium. Otherwise I wold try to skip this step and put my application in state of successful log in without any communication with third party setting rack session or anything else you are using to store results of a log in. – lompy Apr 29 '14 at 12:09
  • I appreciate your feedback, but do you know if I can use both capybara and pure selenium in the same tests without any kind of conflict or side-effect? Also, could you be a bit more specific as to why this should discourage me using capybara and exactly what you mean by "rethink your tests"? – Metareven Apr 29 '14 at 12:32
  • You gonna have to manage to dig into Capybara's instance of Selenium driver. I would start my research from [here](https://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/driver.rb) Never done that so can't tell about any conflicts and side-effects. – lompy Apr 29 '14 at 13:09
  • About "rethink your tests". Test should be as simple as possible and as readable as possible. If you don't have time to create docs your test should cover for docs. According to that point of view using complicated css selectors inside top level request tests is discouraged. But you could totally use it if you put it inside helper method with comprehensive name and comment on what's going on inside and why. – lompy Apr 29 '14 at 13:15

1 Answers1

2

If you want more control over which option you select, you will need to:

  1. Find the option element (using find, all, etc)
  2. Use the select_option method

For example, if you wanted to just select the first option:

option = page.first("#panel > div.panel-body > form > div:nth-child(1) > select option")
option.select_option

Or if you wanted to select the last:

options = page.all("#panel > div.panel-body > form > div:nth-child(1) > select option")
options[-1].select_option
Justin Ko
  • 46,526
  • 5
  • 91
  • 101