lib/page-object/platforms/selenium/page_object.rb in page-object-0.2.2 vs lib/page-object/platforms/selenium/page_object.rb in page-object-0.2.3

- old
+ new

@@ -116,13 +116,11 @@ # # platform method to get the value stored in a text field # See PageObject::Accessors#text_field # def text_field_value_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'text' - how, what = Elements::TextField.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TextField, 'input', :type => 'text') switch_to_frame(frame_identifiers) text = @browser.find_element(how, what).attribute('value') @browser.switch_to.default_content text end @@ -130,13 +128,11 @@ # # platform method to set the value for a text field # See PageObject::Accessors#text_field # def text_field_value_set(identifier, value) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'text' - how, what = Elements::TextField.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TextField, 'input', :type => 'text') switch_to_frame(frame_identifiers) @browser.find_element(how, what).clear @browser.find_element(how, what).send_keys(value) @browser.switch_to.default_content end @@ -144,13 +140,11 @@ # # platform method to retrieve a text field element # See PageObject::Accessors#text_field # def text_field_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'text' - how, what = Elements::TextField.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TextField, 'input', :type => 'text') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::TextField.new(element, :platform => :selenium) end @@ -158,13 +152,11 @@ # # platform method to get the value stored in a hidden field # See PageObject::Accessors#hidden_field # def hidden_field_value_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'hidden' - how, what = Elements::HiddenField.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::HiddenField, 'input', :type => 'hidden') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).attribute('value') @browser.switch_to.default_content value end @@ -172,13 +164,11 @@ # # platform method to retrieve a hidden field element # See PageObject::Accessors#hidden_field # def hidden_field_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'hidden' - how, what = Elements::HiddenField.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::HiddenField, 'input', :type => 'hidden') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Elements::HiddenField.new(element, :platform => :selenium) end @@ -186,26 +176,22 @@ # # platform method to set text in a textarea # See PageObject::Accessors#text_area # def text_area_value_set(identifier, value) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'textarea' - how, what = Elements::TextArea.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TextArea, 'textarea') switch_to_frame(frame_identifiers) @browser.find_element(how, what).send_keys(value) @browser.switch_to.default_content end # # platform method to get the text from a textarea # See PageObject::Accessors#text_area # def text_area_value_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'textarea' - how, what = Elements::TextArea.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TextArea, 'textarea') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).attribute('value') @browser.switch_to.default_content value end @@ -213,13 +199,11 @@ # # platform method to get the text area element # See PageObject::Accessors#text_area # def text_area_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'textarea' - how, what = Elements::TextArea.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TextArea, 'textarea') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Elements::TextArea.new(element, :platform => :selenium) end @@ -227,13 +211,11 @@ # # platform method to get the currently selected value from a select list # See PageObject::Accessors#select_list # def select_list_value_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'select' - how, what = Elements::SelectList.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::SelectList, 'select') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).attribute('value') @browser.switch_to.default_content value end @@ -241,26 +223,22 @@ # # platform method to select a value from a select list # See PageObject::Accessors#select_list # def select_list_value_set(identifier, value) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'select' - how, what = Elements::SelectList.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::SelectList, 'select') switch_to_frame(frame_identifiers) @browser.find_element(how, what).send_keys(value) @browser.switch_to.default_content end # # platform method to return the select list element # See PageObject::Accessors#select_list # def select_list_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'select' - how, what = Elements::SelectList.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::SelectList, 'select') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Elements::SelectList.new(element, :platform => :selenium) end @@ -268,26 +246,22 @@ # # platform method to click a link # See PageObject::Accessors#link # def click_link_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, "a" - how, what = Elements::Link.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Link, 'a') switch_to_frame frame_identifiers @browser.find_element(how, what).click @browser.switch_to.default_content end # # platform method to return a PageObject::Elements::Link object # see PageObject::Accessors#link # def link_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, "a" - how, what = Elements::Link.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Link, 'a') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Elements::Link.new(element, :platform => :selenium) end @@ -295,39 +269,33 @@ # # platform method to check a checkbox # See PageObject::Accessors#checkbox # def check_checkbox(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'checkbox' - how, what = Elements::CheckBox.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::CheckBox, 'input', :type => 'checkbox') switch_to_frame(frame_identifiers) @browser.find_element(how, what).click unless @browser.find_element(how, what).selected? @browser.switch_to.default_content end # # platform method to uncheck a checkbox # See PageObject::Accessors#checkbox # def uncheck_checkbox(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'checkbox' - how, what = Elements::CheckBox.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::CheckBox, 'input', :type => 'checkbox') switch_to_frame(frame_identifiers) @browser.find_element(how, what).click if @browser.find_element(how, what).selected? @browser.switch_to.default_content end # # platform method to determine if a checkbox is checked # See PageObject::Accessors#checkbox # def checkbox_checked?(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'checkbox' - how, what = Elements::CheckBox.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::CheckBox, 'input', :type => 'checkbox') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).selected? @browser.switch_to.default_content value end @@ -335,13 +303,11 @@ # # platform method to return a PageObject::Elements::CheckBox element # See PageObject::Accessors#checkbox # def checkbox_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'checkbox' - how, what = Elements::CheckBox.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::CheckBox, 'input', :type => 'checkbox') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Elements::CheckBox.new(element, :platform => :selenium) end @@ -349,39 +315,33 @@ # # platform method to select a radio button # See PageObject::Accessors#radio_button # def select_radio(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'radio' - how, what = Elements::RadioButton.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::RadioButton, 'input', :type => 'radio') switch_to_frame(frame_identifiers) @browser.find_element(how, what).click unless @browser.find_element(how, what).selected? @browser.switch_to.default_content end # # platform method to clear a radio button # See PageObject::Accessors#radio_button # def clear_radio(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'radio' - how, what = Elements::RadioButton.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::RadioButton, 'input', :type => 'radio') switch_to_frame(frame_identifiers) @browser.find_element(how, what).click if @browser.find_element(how, what).selected? @browser.switch_to.default_content end # # platform method to determine if a radio button is selected # See PageObject::Accessors#radio_button # def radio_selected?(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'radio' - how, what = Elements::RadioButton.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::RadioButton, 'input', :type => 'radio') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).selected? @browser.switch_to.default_content value end @@ -389,13 +349,11 @@ # # platform method to return a PageObject::Eements::RadioButton element # See PageObject::Accessors#radio_button # def radio_button_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'radio' - how, what = Elements::RadioButton.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::RadioButton, 'input', :type => 'radio') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::RadioButton.new(element, :platform => :selenium) end @@ -403,13 +361,11 @@ # # platform method to return the text for a div # See PageObject::Accessors#div # def div_text_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'div' - how, what = Elements::Div.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Div, 'div') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).text @browser.switch_to.default_content value end @@ -417,13 +373,11 @@ # # platform method to return a PageObject::Elements::Div element # See PageObject::Accessors#div # def div_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'div' - how, what = Elements::Div.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Div, 'div') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::Div.new(element, :platform => :selenium) end @@ -431,13 +385,11 @@ # # platform method to return the text for a span # See PageObject::Accessors#span # def span_text_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'span' - how, what = Elements::Span.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Span, 'span') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).text @browser.switch_to.default_content value end @@ -445,13 +397,11 @@ # # platform method to return a PageObject::Elements::Span element # See PageObject::Accessors#span # def span_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'span' - how, what = Elements::Span.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Span, 'span') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::Span.new(element, :platform => :selenium) end @@ -459,26 +409,22 @@ # # platform method to click a button # See PageObject::Accessors#button # def click_button_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'submit' - how, what = Elements::Button.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Button, 'input', :type => 'submit') switch_to_frame(frame_identifiers) @browser.find_element(how, what).click @browser.switch_to.default_content end # # platform method to retrieve a button element # See PageObject::Accessors#button # def button_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'input', :type => 'submit' - how, what = Elements::Button.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Button, 'input', :type => 'submit') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::Button.new(element, :platform => :selenium) end @@ -486,13 +432,11 @@ # # platform method to retrieve a table element # See PageObject::Accessors#table # def table_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'table' - how, what = Elements::Table.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Table, 'table') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::Table.new(element, :platform => :selenium) end @@ -500,13 +444,11 @@ # # platform method to retrieve the text from a table cell # See PageObject::Accessors#cell # def cell_text_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'td' - how, what = Elements::TableCell.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TableCell, 'td') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).text @browser.switch_to.default_content value end @@ -514,13 +456,11 @@ # # platform method to retrieve a table cell element # See PageObject::Accessors#cell # def cell_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'td' - how, what = Elements::TableCell.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::TableCell, 'td') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::TableCell.new(element, :platform => :selenium) end @@ -528,13 +468,11 @@ # # platform method to retrieve an image element # See PageObject::Accessors#image # def image_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'img' - how, what = Elements::Image.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Image, 'img') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::Image.new(element, :platform => :selenium) end @@ -542,13 +480,11 @@ # # platform method to retrieve a form element # See PageObject::Accessors#form # def form_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'form' - how, what = Elements::Form.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::Form, 'form') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::Form.new(element, :platform => :selenium) end @@ -556,13 +492,11 @@ # # platform method to retrieve the text from a list item # See PageObject::Accessors#list_item # def list_item_text_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'li' - how, what = Elements::ListItem.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::ListItem, 'li') switch_to_frame(frame_identifiers) value = @browser.find_element(how, what).text @browser.switch_to.default_content value end @@ -570,13 +504,11 @@ # # platform method to retrieve a list item element # See PageObject::Accessors#list_item # def list_item_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'li' - how, what = Elements::ListItem.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::ListItem, 'li') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::ListItem.new(element, :platform => :selenium) end @@ -584,13 +516,11 @@ # # platform method to retrieve an unordered list element # See PageObject::Accessors#unordered_list # def unordered_list_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'ul' - how, what = Elements::UnorderedList.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::UnorderedList, 'ul') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::UnorderedList.new(element, :platform => :selenium) end @@ -598,20 +528,25 @@ # # platform method to retrieve an ordered list element # See PageObject::Accessors#ordered_list # def ordered_list_for(identifier) - frame_identifiers = identifier.delete(:frame) - identifier = add_tagname_if_needed identifier, 'ol' - how, what = Elements::OrderedList.selenium_identifier_for identifier + how, what, frame_identifiers = parse_identifiers(identifier, Elements::OrderedList, 'ol') switch_to_frame(frame_identifiers) element = @browser.find_element(how, what) @browser.switch_to.default_content Object::PageObject::Elements::OrderedList.new(element, :platform => :selenium) end private + + def parse_identifiers(identifier, element, tag_name=nil, additional=nil) + frame_identifiers = identifier.delete(:frame) + identifier = add_tagname_if_needed identifier, tag_name, additional if tag_name + how, what = element.selenium_identifier_for identifier + return how, what, frame_identifiers + end def add_tagname_if_needed identifier, tag, additional=nil return identifier if identifier.length < 2 and supported_identifier(identifier, tag, additional) identifier[:tag_name] = tag if additional @@ -625,9 +560,10 @@ def supported_identifier(identifier, tag, additional) return false if identifier[:index] return false if identifier[:text] and tag == 'input' and additional[:type] == 'hidden' return false if identifier[:href] and tag == 'a' return false if identifier[:text] and tag == 'div' + return false if identifier[:text] and tag == 'td' true end def switch_to_frame(frame_identifiers) if not frame_identifiers.nil?