mirror of
https://github.com/thoughtbot/capybara-webkit
synced 2023-03-27 23:22:28 -04:00
Select and unselect options
This commit is contained in:
parent
64d34e57ea
commit
fa3c6eddb4
3 changed files with 68 additions and 5 deletions
|
@ -9,19 +9,27 @@ class Capybara::Driver::Webkit
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
|
if multiple_select?
|
||||||
|
self.find("./option").select do |option|
|
||||||
|
option["selected"] == "selected"
|
||||||
|
end.map do |option|
|
||||||
|
option.value
|
||||||
|
end
|
||||||
|
else
|
||||||
invoke "value"
|
invoke "value"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def set(value)
|
def set(value)
|
||||||
invoke "set", value
|
invoke "set", value
|
||||||
end
|
end
|
||||||
|
|
||||||
def select_option
|
def select_option
|
||||||
raise NotImplementedError
|
invoke "selectOption"
|
||||||
end
|
end
|
||||||
|
|
||||||
def unselect_option
|
def unselect_option
|
||||||
raise NotImplementedError
|
invoke "unselectOption"
|
||||||
end
|
end
|
||||||
|
|
||||||
def click
|
def click
|
||||||
|
@ -63,6 +71,12 @@ class Capybara::Driver::Webkit
|
||||||
def browser
|
def browser
|
||||||
driver.browser
|
driver.browser
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def multiple_select?
|
||||||
|
self.tag_name == "select" && self["multiple"] == "multiple"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -143,8 +143,13 @@ describe Capybara::Driver::Webkit do
|
||||||
<form action="/" method="GET">
|
<form action="/" method="GET">
|
||||||
<input type="text" name="foo" value="bar"/>
|
<input type="text" name="foo" value="bar"/>
|
||||||
<select name="animal">
|
<select name="animal">
|
||||||
<option>Monkey</option>
|
<option id="select-option-monkey">Monkey</option>
|
||||||
<option selected="selected">Capybara</option>
|
<option id="select-option-capybara" selected="selected">Capybara</option>
|
||||||
|
</select>
|
||||||
|
<select name="toppings" multiple="multiple">
|
||||||
|
<option selected="selected" id="topping-apple">Apple</option>
|
||||||
|
<option selected="selected" id="topping-banana">Banana</option>
|
||||||
|
<option selected="selected" id="topping-cherry">Cherry</option>
|
||||||
</select>
|
</select>
|
||||||
<textarea id="only-textarea">what a wonderful area for text</textarea>
|
<textarea id="only-textarea">what a wonderful area for text</textarea>
|
||||||
</form>
|
</form>
|
||||||
|
@ -185,6 +190,41 @@ describe Capybara::Driver::Webkit do
|
||||||
textarea.set("newvalue")
|
textarea.set("newvalue")
|
||||||
textarea.value.should == "newvalue"
|
textarea.value.should == "newvalue"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let(:monkey_option) { subject.find("//option[@id='select-option-monkey']").first }
|
||||||
|
let(:capybara_option) { subject.find("//option[@id='select-option-capybara']").first }
|
||||||
|
let(:animal_select) { subject.find("//select[@name='animal']").first }
|
||||||
|
let(:apple_option) { subject.find("//option[@id='topping-apple']").first }
|
||||||
|
let(:banana_option) { subject.find("//option[@id='topping-banana']").first }
|
||||||
|
let(:cherry_option) { subject.find("//option[@id='topping-cherry']").first }
|
||||||
|
let(:toppings_select) { subject.find("//select[@name='toppings']").first }
|
||||||
|
|
||||||
|
it "selects an option" do
|
||||||
|
animal_select.value.should == "Capybara"
|
||||||
|
monkey_option.select_option
|
||||||
|
animal_select.value.should == "Monkey"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "unselects an option in a multi-select" do
|
||||||
|
toppings_select.value.should include("Apple", "Banana", "Cherry")
|
||||||
|
|
||||||
|
apple_option.unselect_option
|
||||||
|
toppings_select.value.should_not include("Apple")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "reselects an option in a multi-select" do
|
||||||
|
apple_option.unselect_option
|
||||||
|
banana_option.unselect_option
|
||||||
|
cherry_option.unselect_option
|
||||||
|
|
||||||
|
toppings_select.value.should == []
|
||||||
|
|
||||||
|
apple_option.select_option
|
||||||
|
banana_option.select_option
|
||||||
|
cherry_option.select_option
|
||||||
|
|
||||||
|
toppings_select.value.should include("Apple", "Banana", "Cherry")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "mouse app" do
|
context "mouse app" do
|
||||||
|
|
|
@ -66,6 +66,15 @@ Capybara = {
|
||||||
|
|
||||||
set: function(index, value) {
|
set: function(index, value) {
|
||||||
this.nodes[index].value = value;
|
this.nodes[index].value = value;
|
||||||
|
},
|
||||||
|
|
||||||
|
selectOption: function(index) {
|
||||||
|
this.nodes[index].setAttribute("selected", "selected");
|
||||||
|
},
|
||||||
|
|
||||||
|
unselectOption: function(index) {
|
||||||
|
this.nodes[index].removeAttribute("selected");
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue