Question

This code is supposed to take a string as input to check if an element of page_object is present. The script has to raise an exception in case it discovers the element, and do nothing if it doesn't.

Example Page Object:

span(:partner_flag,          class: 'content-partner-flag')

The script:

 def check_element_not_exist(page_object)
    page_object = page_object.downcase.gsub(' ', '_')
    option = send("#{page_object}")
    if option.exists?
      raise "#{page_object} was not found!"
    end
 end

In this case, I use the string partner_flag to feed the function and check the element. Watir fails in the line:

option = send("#{page_object}")

because it needs to find that element in the webpage in order to define option. Is there an alternate way of defining option, or a different way of making this non-existence check with the send functionality?

Was it helpful?

Solution

The accessor methods create a method for checking if an element exists.

For example, when you include:

span(:partner_flag, class: 'content-partner-flag')

Then the method:

partner_flag?

Is created that returns true if the element exists and false if the element does not.

You could call this method in the check_element_not_exist method:

def check_element_not_exist(page_object)
  page_object = page_object.downcase.gsub(' ', '_')
  exists = send("#{page_object}?")
  if exists
    raise "#{page_object} was found!"
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top