Search code examples
rubywatirpage-object-gem

Cheezy Page-Object Gem Dynamic Locator for Generic Element?


Using Cheezy's page-object gem I've come across the ability to have dynamic element locators. (Noted at this github issue: https://github.com/cheezy/page-object/issues/203).

So for example I can do span_element(id: 'some id'), div_element(class: 'some_class'), etc. However what can I do if I need to locate a generic element? For example I could be working on a page that has angular so the elements are not traditional (like instead of a traditional html select control with options, it is a custom angular dropdown). I've tried element_element(class: 'class_name') and just element(class: 'class_name') but ruby says method missing


Solution

  • The generic dynamic element locator is defined in PageObject::ElementLocators#element as:

    def element(tag, identifier={:index => 0})
      platform.element_for(tag, identifier.clone)
    end
    

    The first argument is the element's tag name. If you don't know the tag name, you can specify "element" for any tag. For example:

    class MyPage
      include PageObject
    
      def do_stuff
        element('element', class: 'class_name').text
      end
    end