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("[GRIDIUM::Page] Asserted Element present with locator #{locator} using #{by}")
  end
end

.evaluate_script(script) ⇒ Object



130
131
132
# File 'lib/page.rb', line 130

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

.execute_script(script) ⇒ Object



126
127
128
# File 'lib/page.rb', line 126

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

.has_button?(button_text, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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_link?(linktext, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/page.rb', line 51

def self.has_link?(linktext, opts = {})
  timeout = opts[:timeout] || 5
  wait = Selenium::WebDriver::Wait.new(:timeout => timeout)

  begin
    elem = nil
    wait.until do
      elem = Driver.driver.find_element(:link_text, linktext)
      elem.enabled?
    end

    if opts[:href]
      href = elem.attribute 'href'
      raise "Failed to verify link href='#{opts[:href]}': #{href} != #{opts[:href]}" unless href == opts[:href]
    end

    return true
  rescue Exception => exception
    Log.debug("[GRIDIUM::Page] has_link? is false because this exception was rescued: #{exception}")
    return false
  end
end

.has_text?(text, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/page.rb', line 92

def self.has_text?(text, opts = {})
  has_flash?(text, opts)
end

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

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object

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

Parameters:

  • CSS (String)

    selector



153
154
155
# File 'lib/page.rb', line 153

def self.jquery_click(selector)
  Driver.evaluate_script("$(\"#{selector}\").click().change();")
end

.jquery_loaded?Boolean

Returns:

  • (Boolean)


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

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

.scroll_to_bottomObject



117
118
119
# File 'lib/page.rb', line 117

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

.scroll_to_topObject



121
122
123
124
# 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_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



134
135
136
137
138
139
140
# 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

Instance Method Details

#all(by, locator) ⇒ Object



157
158
159
160
# 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) ⇒ Object

checks a checkbox



216
217
218
# 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) ⇒ 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)



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 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 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



190
191
192
193
194
195
196
# File 'lib/page.rb', line 190

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



183
184
185
# File 'lib/page.rb', line 183

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

#find(by, locator, opts = {}) ⇒ Object



162
163
164
# File 'lib/page.rb', line 162

def find(by, locator, opts = {})
  Element.new("Page Find Element", by, locator, opts)
end

#first(by, locator) ⇒ Object



166
167
168
# File 'lib/page.rb', line 166

def first(by, locator)
  all(by, locator).first
end

#mouse_over(text, x: 0, y: 0, index: 1) ⇒ Object Also known as: hover

mouse/hover over request ‘text’ at options coordinates and optional index

Parameters:

  • text (String)
  • x (Integer) (defaults to: 0)
    • optional x coordinate

  • y (Integer) (defaults to: 0)
    • optional y coordinate

  • index (Integer) (defaults to: 1)
    • optional index, if multiple instances of ‘text’ are found



177
178
179
# 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