From 54bd501187e69ba039c71b7655c533100a3ed47c Mon Sep 17 00:00:00 2001 From: "Jeffrey S. Morgan" Date: Tue, 10 Jan 2012 15:41:55 -0500 Subject: [PATCH] Added #list_item_elements to ElementLocator --- ChangeLog | 3 ++- features/html/multi_elements.html | 22 +++++++++++++++++++ features/multi_elements.feature | 6 +++++ .../step_definitions/multi_elements_steps.rb | 12 ++++++++++ lib/page-object/element_locators.rb | 15 +++++++++++++ .../selenium_webdriver/page_object.rb | 7 ++++++ .../platforms/watir_webdriver/page_object.rb | 7 ++++++ spec/page-object/element_locators_spec.rb | 12 ++++++++++ 8 files changed, 83 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index dbbf53ce..c4b3a1b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 diff --git a/features/html/multi_elements.html b/features/html/multi_elements.html index 9d11453c..98d16452 100644 --- a/features/html/multi_elements.html +++ b/features/html/multi_elements.html @@ -82,5 +82,27 @@
+ + + + +
    +
  1. Number One
  2. +
+
    +
  1. Number Two
  2. +
+
    +
  1. Number Three
  2. +
+ diff --git a/features/multi_elements.feature b/features/multi_elements.feature index 01e879ac..cc958e01 100644 --- a/features/multi_elements.feature +++ b/features/multi_elements.feature @@ -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" diff --git a/features/step_definitions/multi_elements_steps.rb b/features/step_definitions/multi_elements_steps.rb index 91fbd029..f9a5e421 100644 --- a/features/step_definitions/multi_elements_steps.rb +++ b/features/step_definitions/multi_elements_steps.rb @@ -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 diff --git a/lib/page-object/element_locators.rb b/lib/page-object/element_locators.rb index 619b280f..ec6f5c38 100644 --- a/lib/page-object/element_locators.rb +++ b/lib/page-object/element_locators.rb @@ -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 # diff --git a/lib/page-object/platforms/selenium_webdriver/page_object.rb b/lib/page-object/platforms/selenium_webdriver/page_object.rb index 6ee525d6..6bd4ff8b 100755 --- a/lib/page-object/platforms/selenium_webdriver/page_object.rb +++ b/lib/page-object/platforms/selenium_webdriver/page_object.rb @@ -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 diff --git a/lib/page-object/platforms/watir_webdriver/page_object.rb b/lib/page-object/platforms/watir_webdriver/page_object.rb index 77803169..35a5899a 100644 --- a/lib/page-object/platforms/watir_webdriver/page_object.rb +++ b/lib/page-object/platforms/watir_webdriver/page_object.rb @@ -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 diff --git a/spec/page-object/element_locators_spec.rb b/spec/page-object/element_locators_spec.rb index d04756a2..6dabe67f 100644 --- a/spec/page-object/element_locators_spec.rb +++ b/spec/page-object/element_locators_spec.rb @@ -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) @@ -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)