Class: Gridium::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assert_selector(by, locator) ⇒ Object



12
13
14
15
16
17
18
19
# 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("Asserted Element present with locator #{locator} using #{by}")
  end
end

.evaluate_script(script) ⇒ Object



84
85
86
# File 'lib/page.rb', line 84

def self.evaluate_script(script)
  Driver.evaluate_script script
end

.execute_script(script) ⇒ Object



80
81
82
# File 'lib/page.rb', line 80

def self.execute_script(script)
  Driver.execute_script_driver(script)
end

.has_css?(css, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/page.rb', line 21

def self.has_css?(css, options={})
  wait = Selenium::WebDriver::Wait.new(:timeout => 5)
  begin
    wait.until {Driver.driver.find_element(:css, css).enabled?}
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_css? is false because this exception was rescued: #{exception}")
    return false
  end
end

.has_flash?(text) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/page.rb', line 55

def self.has_flash?(text)
  wait = Selenium::WebDriver::Wait.new(:timeout => 5) #5 seconds every 500ms
  begin
    element = wait.until {Driver.html.include? text}
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_flash? exception was rescued: #{exception}")
    Log.warn("Could not find the flash message!")
  end

  if element
    return true
  else
    return false
  end
end

.has_link?(linktext) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/page.rb', line 41

def self.has_link?(linktext)
  wait = Selenium::WebDriver::Wait.new(:timeout => 5)
  begin
    wait.until {Driver.driver.find_element(:link_text, linktext).enabled?}
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_link? is false because this exception was rescued: #{exception}")
    return false
  end
end

.has_text?(text) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/page.rb', line 51

def self.has_text?(text)
  has_flash?(text)
end

.has_xpath?(xpath, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/page.rb', line 31

def self.has_xpath?(xpath, options={})
  wait = Selenium::WebDriver::Wait.new(:timeout => 5)
  begin
    wait.until {Driver.driver.find_element(:xpath, xpath).enabled?}
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_xpath? is false because this exception was rescued: #{exception}")
    return false
  end
end

.jquery_loaded?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/page.rb', line 94

def self.jquery_loaded?
  self.evaluate_script("jQuery.active").zero?
end

.scroll_to_bottomObject



71
72
73
# File 'lib/page.rb', line 71

def self.scroll_to_bottom
  Driver.execute_script_driver('window.scrollTo(0,100000)')
end

.scroll_to_topObject



75
76
77
78
# File 'lib/page.rb', line 75

def self.scroll_to_top
  #TODO Verify this
  Driver.execute_script_driver('window.scrollTo(100000,0)')
end

.switch_to_defaultObject



8
9
10
# File 'lib/page.rb', line 8

def self.switch_to_default
  Driver.driver.switch_to.default_content
end

.switch_to_frame(frame) ⇒ Object



4
5
6
# File 'lib/page.rb', line 4

def self.switch_to_frame(frame)
  Driver.driver.switch_to.frame(frame)
end

.wait_for_ajaxObject



88
89
90
91
92
# File 'lib/page.rb', line 88

def self.wait_for_ajax
  Timeout.timeout(Gridium.config.page_load_timeout) do
    loop until jquery_loaded?
  end
end

Instance Method Details

#all(by, locator) ⇒ Object



98
99
100
# File 'lib/page.rb', line 98

def all(by, locator)
  Driver.driver.find_elements(by, locator)
end

#check(id) ⇒ Object

checks a checkbox



143
144
145
# File 'lib/page.rb', line 143

def check(id) #checks a checkbox
  Driver.driver.find_element(:id, id).click
end

#click_button(button_name, button_index: nil) ⇒ Object

Click the button on the page

Parameters:

  • link_text (String)
    • Text of the link to click

  • link_index (Integer)
    • With multiple links on the page with the same name, click on the specified link index (Defaults to first link found)



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/page.rb', line 128

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 the link on the page

Parameters:

  • link_text (String)
    • Text of the link to click

  • link_index (Integer) (defaults to: nil)

    (optional) - With multiple links on the page with the same name, click on the specified link index



117
118
119
120
121
122
123
# File 'lib/page.rb', line 117

def click_link(link_text, link_index: nil)
  if link_index
    Element.new("Clicking #{link_text} Link (#{link_index})", :xpath, "(//a[contains(., '#{link_text}')])[#{link_index}]").click
  else
    Element.new("Clicking #{link_text} Link", :xpath, "//a[contains(., '#{link_text}')]").click
  end
end

#click_on(text) ⇒ Object



110
111
112
# File 'lib/page.rb', line 110

def click_on(text)
  Element.new("Clicking #{text}", :xpath, "//*[text()='#{text}')]").click
end

#find(by, locator) ⇒ Object



102
103
104
# File 'lib/page.rb', line 102

def find(by, locator)
  Element.new("Page Find Element", by, locator)
end

#first(by, locator) ⇒ Object



106
107
108
# File 'lib/page.rb', line 106

def first(by, locator)
  Driver.driver.find_elements(by, locator).first
end