0

Is it possible to get the identifier of a PageObject Element by having only the Element? (The cheezy pageobject gem)

Is there any attribute of the Element that keeps the name of the element.

For instance, in this case:

text_field(:name, :id => 'name')

if I have only the element, how can I find its name out? :)

Thanks for the help.


Edit:

Ok, so here's what I am trying to do:

I have a pageObject called AttributePage, which has some elements defined:

class AttributePage < PropertyPage
  include PageObject
  include DataMagic

  page_url NavigationHelpers::get_path 'catalog-attributes'


  # Header
  textfield(:attribute_sets, :id =>'ext-1') # 1.
  textfield(:global_attributes, :id => 'ext-2') # 2.

end

and a PageObject PropertyPage that looks like this:

class PropertyPage
  include PageObject

  def get_search_filter_elements
    element_methods = self.class.instance_methods(false).find_all{ |m| m =~ /_element$/ }
    elements = element_methods.collect{ |m| self.send(m) }
    return_array =[]
    for x in 0...element_methods.size
      return_array.append([element_methods[x].to_s.gsub('_element', ''), elements[x]])
    end
    return return_array
  end

This way, when I call

on_page(AttributePage).get_search_filter_elements

I get a list of PageObject::Element. What I wanted to do was to call the AttributePage.fill_elements that would look like this:

def fill_elements
  elements = get_search_filter_elements
  elements.each do |element|
     # Now this doesn't work
     element.text = read_from_some_file_for_element_name element.name
  end

is there any way I can do this?

6
  • 2
    name means what ? Are you talking about tag.. Not clear .. Nov 3, 2014 at 18:11
  • Your question is mixing unrelated components here. "Element" in the generic sense always refers an element in your html. Element Object in the PO gem is a generic wrapper for 'WatirWebDriver::Element' or 'SeleniumWebDriver::Element' You need to clarify this. It seems as though you might be referring to the PageObject::Element, then what are you asking? What is it's method name in this example you have provided? Answer: The symbol name you provided as arg[0]: ':name' in text_field(). To access it, you would call name_element() within the context of your page object. Nov 3, 2014 at 22:16
  • Thanks for your help, Jared. What I'm looking for is the "name", the id of the PO element. I have access only to the Element Object. And having only the Selenium::Webdriver::Element, how can I do that? Is there an "element.name" method or something like that? Thanks.
    – Tiago
    Nov 4, 2014 at 10:36
  • Isn't the get_search_filter_elements method already returning the name, but the fill_elements method is using the returned result incorrectly?
    – Justin Ko
    Nov 12, 2014 at 21:22
  • I would submit that this is the hard way and a bit of wheel reinvention is going on with your approach, but that isn't your question. The answer(s) are where @JustinKo just stated and in my answer below. Nov 12, 2014 at 23:21

1 Answer 1

0

Let me first create a more complete page object (PO) class for reference

Assuming you have some html on a page with a text field that has an id of 'name'

<input type="text" id="name" value="zzz" name="yyy"/>

class MyPage
    include PageObject
    text_field(:name, :id => 'name')
end

myPage = MyPage.new
p myPage.name_element #Output => PageObject::Elements::TextArea
p myPage.name?        #Output => 'true'
p myPage.name         #Output => 'zzz'
myPage.name = 'bbb' #Sets value of text_field to 'bbb'
p myPage.name         #Output => 'bbb'

These four methods are all dynamically created as a result of defining the page element 'text_field(:name, ...)'

Your question is ambiguous, but I think what you are wanting to know is, now that you have this element mapped in the page object, how would you go about getting it's name, meaning it's html name? Well that will only work if the html element in question has a 'name' attribute. If it does, like my example above, then you would access it's name using the name_element object, like so:

p myPage.name_element.name #Output => 'yyy'
1
  • I already edited the question so you can understand it better :)
    – Tiago
    Nov 12, 2014 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.