Permalink
Browse files

Merge pull request #93 from p0deje/selenium_select_clear

Support for SelectList#clear on Selenium regarding #90
  • Loading branch information...
2 parents 599569c + 71a8230 commit cfec1704b7d80921af63d9c7794d600311b39218 @cheezy committed May 27, 2012
@@ -14,10 +14,15 @@
<textarea rows="2" cols="20" id="text_area_id" class="text_area_class" name="text_area_name"></textarea>
<select name="sel_list_name" id="sel_list_id" class="sel_list_class">
- <option value="option1">Test 1</option>
- <option value="option2">Test 2</option>
+ <option value="option1">Test 1</option>
+ <option value="option2">Test 2</option>
</select>
+ <select multiple="multiple" id="sel_list_multiple">
+ <option value="option1" selected="true">Test 1</option>
+ <option value="option2">Test 2</option>
+ <option value="option3" selected="true">Test 3</option>
+ </select>
<a href="success.html" id="link_id" name="link_name" class="link_class">Google Search</a>
@@ -33,7 +38,7 @@
<span id="span_id" name="span_name" class="span_class" title="span_title">
My alert
</span>
-
+
<label id="label_id" name="label_name" class="label_class">
page-object is the best!
</label>
@@ -91,7 +96,7 @@ <h5 id="h5_id" class="h5_class" name="h5_name">h5's are cool</h5>
<h6 id="h6_id" class="h6_class" name="h6_name">h6's are cool</h6>
<a href="success.html" target="_blank">New Window</a>
-
+
<input type="text" id="onfocus_text_field" onfocus="document.getElementById('onfocus_test').innerHTML = 'changed by onfocus event'"/>
<div id="onfocus_test"></div>
@@ -57,3 +57,6 @@ Feature: Select List
When I select "Test 2" from the select list
Then the select list should know that "Test 2" is selected
+ Scenario: Clearing multiple select list
+ When I clear multiple select list
+ Then multiple select list should have no selected options
@@ -47,3 +47,11 @@
element = @page.send "sel_list_#{@how}_element".to_sym
element.value.should == value
end
+
+When /^I clear multiple select list$/ do
+ @page.sel_list_multiple_element.clear
+end
+
+Then /^multiple select list should have no selected options$/ do
+ @page.sel_list_multiple_element.selected_options.should be_empty
+end
View
@@ -3,7 +3,7 @@ class Page
expected_title "Static Elements Page"
expected_element :hello0
-
+
link(:hello0, {:text => "Hello", :index => 0})
link(:hello1, {:text => "Hello", :index => 1})
link(:hello2, {:text => "Hello", :index => 2})
@@ -65,6 +65,7 @@ class Page
select_list(:sel_list_text, :text => "Test 1")
select_list(:sel_list_class_index, :class => "sel_list_class", :index => 0)
select_list(:sel_list_name_index, :name => "sel_list_name", :index => 0)
+ select_list(:sel_list_multiple, :id => "sel_list_multiple")
checkbox(:cb_id, :id => 'cb_id')
checkbox(:cb_name, :name => 'cb_name')
@@ -190,7 +191,7 @@ class Page
ordered_list(:ol_xpath, :xpath => '//ol')
ordered_list(:ol_class_index, :class => "ol_class", :index => 0)
ordered_list(:ol_name_index, :name => "ol_name", :index => 0)
-
+
h1(:h1_id, :id => 'h1_id')
h1(:h1_class, :class => 'h1_class')
h1(:h1_name, :name => 'h1_name')
@@ -238,7 +239,7 @@ class Page
h6(:h6_xpath, :xpath => '//h6')
h6(:h6_class_index, :class => 'h6_class', :index => 0)
h6(:h6_name_index, :name => 'h6_name', :index => 0)
-
+
paragraph(:p_id, :id => 'p_id')
paragraph(:p_class, :class => 'p_class')
paragraph(:p_name, :name => 'p_name')
@@ -268,7 +269,7 @@ class Page
label(:label_xpath, :xpath => '//label')
label(:label_class_index, :class => "label_class", :index => 0)
label(:label_name_index, :name => "label_name", :index => 0)
-
+
link(:open_window, :text => 'New Window')
link(:child, :id => 'child')
@@ -12,7 +12,7 @@ module SelectList
def [](idx)
options[idx]
end
-
+
#
# Select a value from the list
#
@@ -56,6 +56,13 @@ def selected?(value)
selected.any? { |e| e.text == value }
end
+ #
+ # Deselect all selected options.
+ #
+ def clear
+ find_options.select { |e| e.selected? }.each { |o| o.click }
+ end
+
private
def find_options
@@ -71,7 +71,7 @@
context "for selenium" do
let(:selenium_sel_list) { PageObject::Elements::SelectList.new(sel_list, :platform => :selenium_webdriver) }
-
+
it "should return an option when indexed" do
sel_list.should_receive(:find_elements).with(:xpath, ".//child::option").and_return(opts)
selenium_sel_list[1].should be_instance_of PageObject::Elements::Option
@@ -89,7 +89,7 @@
option.should_receive(:click)
selenium_sel_list.select 'something'
end
-
+
it "should return an array of selected options" do
sel_list.should_receive(:find_elements).with(:xpath, ".//child::option").and_return(opts)
opts[0].should_receive(:selected?).and_return(true)
@@ -105,13 +105,22 @@
opts[0].should_receive(:text).and_return('blah')
selenium_sel_list.should include 'blah'
end
-
+
it "should know if an option is selected" do
sel_list.should_receive(:find_elements).and_return(opts)
opts[0].should_receive(:selected?).twice.and_return(true)
opts[0].should_receive(:text).and_return('blah')
selenium_sel_list.selected?('blah').should be_true
end
+
+ it "should be able to clear selected options" do
+ sel_list.should_receive(:find_elements).and_return(opts)
+ opts.each do |opt|
+ opt.should_receive(:selected?).and_return(true)
+ opt.should_receive(:click)
+ end
+ selenium_sel_list.clear
+ end
end
end
end

0 comments on commit cfec170

Please sign in to comment.