thoughtbot/capybara-webkit — Issue #268: `select_option` does not deselect sibling `<option>`s
Author: jasonmp85
Created: 2012-02-11 21:38:18+00:00
State: open

Apologies in advance for not having the time to write a patch or repro case, but I wanted to report this issue to get it off my plate. I may be able to come back in the week with a repro/patch, but for now I'm a bit swamped.

Here's the issue:

* I've got a form being rendered dynamically client-side with a JavaScript template
* It's got a `` element with no `multiple` attribute present (i.e. single select)
* One of its `
`s is selected at render time to reflect the present value on a model object

The generated markup looks like (using `puts page.html`):

```html

Normal

Flagged

Disabled

```

Now let's say a test makes a selection using `select 'Disabled', from: 'status'`. Here's what the markup looks like after that:

```html

Normal

Flagged

Disabled

```

See the problem? This interferes with form submission, resetting the form, and all number of actions. I inspected [the code][] for `select_option` in capybara proper:

```ruby
def select_option
if select_node['multiple'] != 'multiple'
select_node.find(".//option[@selected]").each { |node| node.native.remove_attribute("selected") }
end
native["selected"] = 'selected'
end
```

However, in capybara-webkit, we see:

```ruby
def select_option
check_visibility(self)
invoke "selectOption"
end
```

This is where I'm in a bit over my head, since I'm not familiar with the architecture of capybara-webkit; however, I'm going to guess that this ends up using the `selectOption` method in `capybara.js`:

```js
selectOption: function(index) {
this.nodes[index].selected = true;
this.nodes[index].setAttribute("selected", "selected");
this.trigger(index, "change");
},
```

My proposal is that capybara-webkit's `Node#select_option` be modified to perform a similar check to that seen in capybara proper.

[the code]: https://github.com/jnicklas/capybara/blob/060fc3a72d0cd18e1ad3376017123c0ceb29aaa4/lib/capybara/rack_test/node.rb#L38