Skip to content

Commit

Permalink
Added #list_item_elements to ElementLocator
Browse files Browse the repository at this point in the history
  • Loading branch information
cheezy committed Jan 10, 2012
1 parent 01d2a27 commit 54bd501
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ChangeLog
Expand Up @@ -13,7 +13,8 @@
* Added #table_elements to ElementLocators
* Added #cell_elements to ElementLocators
* Added #image_elements to ElementLocators
* Added *form_elements to ElementLocators
* Added #form_elements to ElementLocators
* Added #list_item_elements to ElementLocators
* Added #navigate_to to PageFactory to navigate to a page through previous pages
* Added #continue_navigation_to to PageFactory which begins at @current_page
* Added routes to PageFactory to collect routes through the site
Expand Down
22 changes: 22 additions & 0 deletions features/html/multi_elements.html
Expand Up @@ -82,5 +82,27 @@
<form method="get" class="form" action="form2"></form>
<form method="get" class="form" action="form3"></form>

<ul class="ul">
<li class="li">Item One</li>
<li class="li">Item Two</li>
<li class="li">Item Three</li>
</ul>
<ul class="ul">
<li>Item Four</li>
</ul>
<ul class="ul">
<li>Item Five</li>
</ul>

<ol class="ol">
<li>Number One</li>
</ol>
<ol class="ol">
<li>Number Two</li>
</ol>
<ol class="ol">
<li>Number Three</li>
</ol>

</body>
</html>
6 changes: 6 additions & 0 deletions features/multi_elements.feature
Expand Up @@ -101,3 +101,9 @@ Feature: Multi Elements
And the action for form 2 should be "form2"
And the action for form 3 should be "form3"

Scenario: Selecting list items
When I select the list items with class "li"
Then I should have 3 list items
And the text for list item 1 should be "Item One"
And the text for list item 2 should be "Item Two"
And the text for list item 3 should be "Item Three"
12 changes: 12 additions & 0 deletions features/step_definitions/multi_elements_steps.rb
Expand Up @@ -175,3 +175,15 @@ class MultiElementsPage
Then /^the action for form (\d+) should be "([^\"]*)"$/ do |form_number, action|
@elements[form_number.to_i-1].attribute(:action).should match action
end

When /^I select the list items with class "([^\"]*)"$/ do |class_name|
@elements = @page.list_item_elements(:class => class_name)
end

Then /^I should have (\d+) list items$/ do |num_list_items|
@elements.size.should == num_list_items.to_i
end

Then /^the text for list item (\d+) should be "([^\"]*)"$/ do |list_item_num, text|
@elements[list_item_num.to_i - 1].text.should == text
end
15 changes: 15 additions & 0 deletions lib/page-object/element_locators.rb
Expand Up @@ -478,6 +478,21 @@ def list_item_element(identifier)
platform.list_item_for(identifier.clone)
end

#
# Finds all list items that match the identifier
#
# @param [Hash] identifier how we find a list item. You can use a multiple paramaters
# by combining of any of the following except xpath. The valid keys are:
# * :class => Watir and Selenium
# * :id => Watir and Selenium
# * :index => Watir and Selenium
# * :name => Watir and Selenium
# * :xpath => Watir and Selenium
#
def list_item_elements(identifier)
platform.list_items_for(identifier.clone)
end

#
# Finds an unordered list
#
Expand Down
7 changes: 7 additions & 0 deletions lib/page-object/platforms/selenium_webdriver/page_object.rb
Expand Up @@ -573,6 +573,13 @@ def list_item_for(identifier)
find_selenium_element(identifier, Elements::ListItem, 'li')
end

#
# platform method to retrieve all list items
#
def list_items_for(identifier)
find_selenium_elements(identifier, Elements::ListItem, 'li')
end

#
# platform method to retrieve an unordered list element
# See PageObject::Accessors#unordered_list
Expand Down
7 changes: 7 additions & 0 deletions lib/page-object/platforms/watir_webdriver/page_object.rb
Expand Up @@ -540,6 +540,13 @@ def list_item_for(identifier)
find_watir_element("li(identifier)", Elements::ListItem, identifier, 'li')
end

#
# platform method to retrieve an array of list items
#
def list_items_for(identifier)
find_watir_elements("lis(identifier)", Elements::ListItem, identifier, 'li')
end

#
# platform method to retrieve an unordered list element
# See PageObject::Accessors#unordered_list
Expand Down
12 changes: 12 additions & 0 deletions spec/page-object/element_locators_spec.rb
Expand Up @@ -184,6 +184,12 @@ class ElementLocatorsTestPageObject
element = watir_page_object.list_item_element(:id => 'blah')
element.should be_instance_of PageObject::Elements::ListItem
end

it "should find all list items" do
watir_browser.should_receive(:lis).with(:id => 'blah').and_return([watir_browser])
elements = watir_page_object.list_item_elements(:id => 'blah')
elements[0].should be_instance_of PageObject::Elements::ListItem
end

it "should find an unordered list" do
watir_browser.should_receive(:ul).with(:id => 'blah').and_return(watir_browser)
Expand Down Expand Up @@ -424,6 +430,12 @@ class ElementLocatorsTestPageObject
element = selenium_page_object.list_item_element(:id => 'blah')
element.should be_instance_of PageObject::Elements::ListItem
end

it "should find all list items" do
selenium_browser.should_receive(:find_elements).with(:id, 'blah').and_return([selenium_browser])
element = selenium_page_object.list_item_elements(:id => 'blah')
element[0].should be_instance_of PageObject::Elements::ListItem
end

it "should find an unordered list" do
selenium_browser.should_receive(:find_element).with(:id, 'blah').and_return(selenium_browser)
Expand Down

0 comments on commit 54bd501

Please sign in to comment.