Search code examples
rubyruby-on-rails-3watirpage-object-gem

What is the page object accessor for a details element


I am trying to click on an element using Watir, but I am unsure what the page object accessor is called. I have tested nearly all of them.

This is the HTML that is displayed when I have clicked on the element:

<details open>
    <summary>Search your employers</summary>

If I haven't clicked it yet it, the HTML is:

<details>
    <summary>Search your employers</summary>

I know how to make the script click on the page element, but I don't know how to define this page object accessor. I'm guessing that I would identify it by calling something like:

[pageobject](:test, :name => 'Search your employers')

Solution

  • What you are looking for is actually called an accessor. The page object is the class that represents the page. The accessor is the method that helps create methods for interacting with the elements on the page.

    As the element is a details element, you want to use the corresponding accessor, which is also called details. It some times helps to look at the source code as the accessors for basic elements do not appear in the documentation.

    From what you have given, your page object and accessor could be defined as:

    class MyPage
      include PageObject
    
      details(:test, text: 'Search your employers')
    end
    

    However, this assumes that the details element's text is exactly "Search your employers". This is unlikely to be true as the point of the details element is to have additional elements that are shown/hidden.

    If the starting text is unique enough, you could do:

    details(:test, text: /^Search your employers/)
    

    While more work, I prefer to be more specific and look for the details element that contains the summary element with the specified text. This can either be done using a block:

    details(:test) { summary_element(text: 'Search your employers').parent }
    

    Or by using XPath:

    details(:test, xpath: '//details[./summary[text() = "Search your employers"]]')