class Gridium::Page

Public Class Methods

assert_selector(by, locator) click to toggle source
# File lib/page.rb, line 12
def self.assert_selector(by, locator)
  asserted_element = Element.new("Asserted Element", by, locator)
  if asserted_element.eql? nil
    fail("Could not find element on page with locator #{locator} using #{by}")
  else
    Log.info("[GRIDIUM::Page] Asserted Element present with locator #{locator} using #{by}")
  end
end
evaluate_script(script) click to toggle source
# File lib/page.rb, line 130
def self.evaluate_script(script)
  Driver.evaluate_script script
end
execute_script(script) click to toggle source
# File lib/page.rb, line 126
def self.execute_script(script)
  Driver.execute_script_driver(script)
end
has_button?(button_text, opts = {}) click to toggle source
# File lib/page.rb, line 74
def self.has_button?(button_text, opts = {})
  timeout = opts[:timeout] || 5
  wait = Selenium::WebDriver::Wait.new(:timeout => timeout)

  begin
    elem = Element.new("#{button_text} button", :xpath, "//button[contains(., \"#{button_text}\")]", timeout: timeout)

    if opts[:disabled]
      wait.until {elem.disabled?}
    else
      wait.until {elem.enabled?}
    end
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_button? is false because this exception was rescued: #{exception}")
    return false
  end
end
has_css?(css, opts = {}) click to toggle source
# File lib/page.rb, line 21
def self.has_css?(css, opts = {})
  timeout = opts[:timeout] || 5
  wait = Selenium::WebDriver::Wait.new(:timeout => timeout)
  begin
    if opts[:visible]
      wait.until {Driver.driver.find_element(:css, css).displayed?}
    else
      wait.until {Driver.driver.find_element(:css, css).enabled?}
    end
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_css? is false because this exception was rescued: #{exception}")
    return false
  end
end
has_flash?(text, opts = {}) click to toggle source
# File lib/page.rb, line 96
def self.has_flash?(text, opts = {})
  timeout = opts[:timeout] || 5
  wait = Selenium::WebDriver::Wait.new(:timeout => timeout)
  begin
    if opts[:visible]
      element = wait.until { Element.new("Finding text '#{text}'", :xpath, "//*[text()=\"#{text}\"]", timeout: timeout).displayed? }
    else
      element = wait.until { Driver.html.include? text }
    end
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] exception was rescued: #{exception}")
    Log.warn("[GRIDIUM::Page] Could not find the text '#{text}'!")
  end

  if element
    return true
  else
    return false
  end
end
has_text?(text, opts = {}) click to toggle source
# File lib/page.rb, line 92
def self.has_text?(text, opts = {})
  has_flash?(text, opts)
end
has_xpath?(xpath, opts = {}) click to toggle source
# File lib/page.rb, line 36
def self.has_xpath?(xpath, opts = {})
  timeout = opts[:timeout] || 5
  wait = Selenium::WebDriver::Wait.new(:timeout => timeout)
  begin
    if opts[:visible]
      wait.until {Driver.driver.find_element(:xpath, xpath).displayed?}
    else
      wait.until {Driver.driver.find_element(:xpath, xpath).enabled?}
    end
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_xpath? is false because this exception was rescued: #{exception}")
    return false
  end
end
jquery_click(selector) click to toggle source

JQuery click @param [String] CSS selector

Occasionaly selenium is unable to click on elements in the DOM which have some interesting React goodies around the element.

# File lib/page.rb, line 153
def self.jquery_click(selector)
  Driver.evaluate_script("$(\"#{selector}\").click().change();")
end
jquery_loaded?() click to toggle source
# File lib/page.rb, line 142
def self.jquery_loaded?
  self.evaluate_script("jQuery.active").zero?
end
scroll_to_bottom() click to toggle source
# File lib/page.rb, line 117
def self.scroll_to_bottom
  Driver.execute_script_driver('window.scrollTo(0,100000)')
end
scroll_to_top() click to toggle source
# File lib/page.rb, line 121
def self.scroll_to_top
  #TODO Verify this
  Driver.execute_script_driver('window.scrollTo(100000,0)')
end
switch_to_default() click to toggle source
# File lib/page.rb, line 8
def self.switch_to_default
  Driver.driver.switch_to.default_content
end
switch_to_frame(frame) click to toggle source
# File lib/page.rb, line 4
def self.switch_to_frame(frame)
  Driver.driver.switch_to.frame(frame)
end
wait_for_ajax() click to toggle source
# File lib/page.rb, line 134
def self.wait_for_ajax
  Timeout.timeout(Gridium.config.page_load_timeout) do
    loop until jquery_loaded?
  end
rescue Timeout::Error => e
  Log.warn("[GRIDIUM::Page] Timed-out waiting for ajax")
end

Public Instance Methods

all(by, locator) click to toggle source
# File lib/page.rb, line 157
def all(by, locator)
  root = Element.new("root", :tag_name, "html")
  root.find_elements(by, locator)
end
check(id) click to toggle source
# File lib/page.rb, line 216
def check(id) #checks a checkbox
  Element.new("Checkbox", :id, id).click
end
click_button(button_name, button_index: nil) click to toggle source

Click the button on the page @param [String] link_text - Text of the link to click @param [Integer] link_index - With multiple links on the page with the same name, click on the specified link index (Defaults to first link found)

# File lib/page.rb, line 201
def click_button(button_name, button_index: nil)
  #The button maybe a link that looks like a button - we want to handle both
  if button_index
    button = Element.new("Clicking #{button_name} button (#{button_index})", :xpath, "(//button[contains(., \"#{button_name}\")])[#{button_index}]")
  else
    button = Element.new("Clicking #{button_name} button", :xpath, "//button[contains(., \"#{button_name}\")]")
  end
  begin
    button.click
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] Button not found and this exception was rescued: #{exception} Attempting Link - speed up test by using click_link method if this works...")
    click_link button_name, link_index: button_index
  end
end
click_on(text) click to toggle source
# File lib/page.rb, line 183
def click_on(text)
  Element.new("Clicking #{text}", :xpath, "//*[text()=\"#{text}\"]").click
end
find(by, locator, opts = {}) click to toggle source
# File lib/page.rb, line 162
def find(by, locator, opts = {})
  Element.new("Page Find Element", by, locator, opts)
end
first(by, locator) click to toggle source
# File lib/page.rb, line 166
def first(by, locator)
  all(by, locator).first
end
hover(text, x: 0, y: 0, index: 1)
Alias for: mouse_over
mouse_over(text, x: 0, y: 0, index: 1) click to toggle source

mouse/hover over request 'text' at options coordinates and optional index @param [String] text @param [Integer] x - optional x coordinate @param [Integer] y - optional y coordinate @param [Integer] index - optional index, if multiple instances of 'text' are found

# File lib/page.rb, line 177
def mouse_over(text, x: 0, y: 0, index: 1)
  Element.new("Clicking #{text}", :xpath, "(//*[text()=\"#{text}\"])[#{index}]").mouse_over(x: x, y: y)
end
Also aliased as: hover