rubyseleniumtestingsite-prism

Is there a way to generate an Array or Hash of Siteprism objects currently on a page


I'm still new(ish), to POM, but I've found the syntax and general structure quite strong, so now I'm looking to advanced techniques.

I have a dynamic page, and for each of the sections I am running the following code/psuedo code

if has_SECTVAR1?
   $LOG.info("Stuff")
end

if has_SECTVAR2?
   $LOG.info("Stuff")
end

What I want to do is something like this.

ALLSECTIONARRAYS.each do |var|
  if has_var?
    $LOG.info("Stuff")
  end
end

Any thoughts?


Solution

  • You can get an array of element names using #mapped_items. The more interesting part is checking if those exist on the page by calling #has_element?.

    The abstract version of what you want to do is call a method on an object given its name as a string. To do this, use #send:

    MyObject.send("method_name", *args)
    

    Or in your case:

    MyPage.send("has_element?")
    

    Finally, to iterate over all elements:

    MyPage.mapped_items.each do |item|
      if MyPage.send("has_#{item}?")
        $LOG.info("Stuff")
      end
    end