Module: WebHelper

Defined in:
lib/test_utils/browser_tests/web_helper.rb

Instance Method Summary collapse

Instance Method Details

#browser_refreshObject

Refresh browser



61
62
63
# File 'lib/test_utils/browser_tests/web_helper.rb', line 61

def browser_refresh
  @browser.refresh
end

#click_when_visible(element, timeout = 15) ⇒ Object

Clicks when an element is visible Waits for visibility (wait_until_visible) and then clicks on element Note: Use on watir elements



84
85
86
87
# File 'lib/test_utils/browser_tests/web_helper.rb', line 84

def click_when_visible(element, timeout = 15)
  wait_until_visible(element, timeout)
  element.click
end

#click_when_visible_po(element, timeout = 15) ⇒ Object

Clicks when an element is visible Waits for visibility (wait_until_visible_po) and then clicks on element Note: Use on page object elements



114
115
116
# File 'lib/test_utils/browser_tests/web_helper.rb', line 114

def click_when_visible_po(element, timeout = 15)
  element.when_visible(timeout).click
end

#confirm_alertObject

Confirms alert



56
57
58
# File 'lib/test_utils/browser_tests/web_helper.rb', line 56

def confirm_alert
  @browser.alert.ok if @browser.alert.exists?
end

#delete_session_storage(key) ⇒ Object

Delete session storage item by key



149
150
151
# File 'lib/test_utils/browser_tests/web_helper.rb', line 149

def delete_session_storage(key)
  @browser.execute_script("return sessionStorage.removeItem('#{key}')")
end

#get_all_session_storageObject

Returns an array with all the session storage items



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/test_utils/browser_tests/web_helper.rb', line 134

def get_all_session_storage
  begin
    length = @browser.execute_script('return sessionStorage.length')
    session_storage = Hash.new
    length.times do |index|
      key = get_session_storage_key(index)
      session_storage[key] = get_session_storage_item(key)
    end
    session_storage
  rescue Selenium::WebDriver::Error::NoScriptResultError
    {}
  end
end

#get_local_storage_item(key) ⇒ Object

Get local storage item by key



119
120
121
# File 'lib/test_utils/browser_tests/web_helper.rb', line 119

def get_local_storage_item(key)
  @browser.execute_script("return localStorage.getItem('#{key}')")
end

#get_session_storage_item(key) ⇒ Object

Get session storage item by key



124
125
126
# File 'lib/test_utils/browser_tests/web_helper.rb', line 124

def get_session_storage_item(key)
  @browser.execute_script("return sessionStorage.getItem('#{key}')")
end

#get_session_storage_key(index) ⇒ Object

Get session storage key by index



129
130
131
# File 'lib/test_utils/browser_tests/web_helper.rb', line 129

def get_session_storage_key(index)
  @browser.execute_script("return sessionStorage.key(#{index})")
end

#open_url(url) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/test_utils/browser_tests/web_helper.rb', line 2

def open_url(url)
  max_retries = 1
  times_retried = 0
  begin
    @browser.goto url
  rescue Net::ReadTimeout => error
    if times_retried < max_retries
      p "### ERROR: Net::ReadTimeout (@browser.goto url page timeout) (url: #{url}) ###"
      p '### RETRYING ... ###'
      times_retried += 1
      retry
    else
      p "### ERROR: Net::ReadTimeout (@browser.goto url page timeout) (url: #{url}) ###"
      p error
      fail "Page failed to load (timeout) after #{(times_retried + 1)} attempts. ###"
    end
  end
end

#resize_browser(width, height) ⇒ Object

Resize browser



50
51
52
53
# File 'lib/test_utils/browser_tests/web_helper.rb', line 50

def resize_browser(width, height)
  @browser.window.resize_to(width, height)
  sleep 2
end

#scroll_horizontal(x) ⇒ Object

Scroll on X axis Positive values scroll right. Negative values scroll left.



39
40
41
# File 'lib/test_utils/browser_tests/web_helper.rb', line 39

def scroll_horizontal(x)
  @browser.execute_script("window.scrollBy(#{x},0)")
end

#scroll_to_element(element) ⇒ Object

Scrolls the page to focus on an element



28
29
30
# File 'lib/test_utils/browser_tests/web_helper.rb', line 28

def scroll_to_element(element)
  @browser.execute_script('arguments[0].scrollIntoView();', element)
end

#scroll_vertical(y) ⇒ Object

Scroll on Y axis Positive values scroll down. Negative values scroll up.



45
46
47
# File 'lib/test_utils/browser_tests/web_helper.rb', line 45

def scroll_vertical(y)
  @browser.execute_script("window.scrollBy(0,#{y})")
end

#scroll_x_y(x, y) ⇒ Object

Scrolls X and/or Y axis



33
34
35
# File 'lib/test_utils/browser_tests/web_helper.rb', line 33

def scroll_x_y(x, y)
  @browser.execute_script("window.scrollBy(#{x},#{y})")
end

#switch_browser_window(index) ⇒ Object

Switch to another browser window



22
23
24
25
# File 'lib/test_utils/browser_tests/web_helper.rb', line 22

def switch_browser_window(index)
  sleep 3
  @browser.window(:index => index).use if @browser.windows.length > 1
end

#wait_until_not_visible_po(element, timeout = 15) ⇒ Object

Wait until an element is NOT visible Waits for !visibility Note: Use on page object elements



103
104
105
106
107
108
109
# File 'lib/test_utils/browser_tests/web_helper.rb', line 103

def wait_until_not_visible_po(element, timeout = 15)
  begin
    Watir::Wait.until(timeout) {!element.visible?}
  rescue Watir::Wait::TimeoutError
    fail "timeout on Watir::Wait.until(t) {!element.visible?} method for element '#{element.inspect}' after #{timeout} seconds."
  end
end

#wait_until_visible(element, timeout = 15) ⇒ Object

Wait until an element is visible Waits for presence and then waits for visibility Note: Use on watir elements



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/test_utils/browser_tests/web_helper.rb', line 68

def wait_until_visible(element, timeout = 15)
  begin
    element.wait_until_present(timeout)
  rescue Watir::Wait::TimeoutError
    fail "timeout on wait_until_present method for element '#{element.inspect}' after #{timeout} seconds."
  end
  begin
    Watir::Wait.until(timeout) {element.visible?}
  rescue Watir::Wait::TimeoutError
    fail "timeout on Watir::Wait.until(t) {element.visible?} method for element '#{element.inspect}' after #{timeout} seconds."
  end
end

#wait_until_visible_po(element, timeout = 15) ⇒ Object

Wait until an element is visible Waits for visibility Note: Use on page object elements



92
93
94
95
96
97
98
# File 'lib/test_utils/browser_tests/web_helper.rb', line 92

def wait_until_visible_po(element, timeout = 15)
  begin
    Watir::Wait.until(timeout) {element.visible?}
  rescue Watir::Wait::TimeoutError
    fail "timeout on Watir::Wait.until(t) {element.visible?} method for element '#{element.inspect}' after #{timeout} seconds."
  end
end