Module: WebHelper
- Defined in:
- lib/test_utils/browser_tests/web_helper.rb
Instance Method Summary collapse
-
#browser_refresh ⇒ Object
Refresh browser.
-
#click_when_visible(element, timeout) ⇒ Object
Clicks when an element is visible Waits for visibility (wait_until_visible) and then clicks on element Note: Use on watir elements.
-
#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.
-
#confirm_alert ⇒ Object
Confirms alert.
-
#delete_session_storage(key) ⇒ Object
Delete session storage item by key.
-
#get_all_session_storage ⇒ Object
Returns an array with all the session storage items.
- #get_current_url ⇒ Object
-
#get_local_storage_item(key) ⇒ Object
Get local storage item by key.
-
#get_session_storage_item(key) ⇒ Object
Get session storage item by key.
-
#get_session_storage_key(index) ⇒ Object
Get session storage key by index.
- #goto(uri) ⇒ Object
- #open_url(url) ⇒ Object
-
#resize_browser(width, height) ⇒ Object
Resize browser.
-
#scroll_horizontal(x) ⇒ Object
Scroll on X axis Positive values scroll right.
-
#scroll_to_element(element) ⇒ Object
Scrolls the page to focus on an element.
-
#scroll_vertical(y) ⇒ Object
Scroll on Y axis Positive values scroll down.
-
#scroll_x_y(x, y) ⇒ Object
Scrolls X and/or Y axis.
-
#switch_browser_window(index) ⇒ Object
Switch to another browser window.
-
#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.
-
#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.
-
#wait_until_visible_po(element, timeout: 15) ⇒ Object
Wait until an element is visible Waits for visibility Note: Use on page object elements.
Instance Method Details
#browser_refresh ⇒ Object
Refresh browser
74 75 76 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 74 def browser_refresh @browser.refresh end |
#click_when_visible(element, timeout) ⇒ Object
Clicks when an element is visible Waits for visibility (wait_until_visible) and then clicks on element Note: Use on watir elements
97 98 99 100 101 102 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 97 def click_when_visible(element, timeout) puts element puts timeout wait_until_visible(element) 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
129 130 131 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 129 def click_when_visible_po(element, timeout: 15) element.when_visible(timeout).click end |
#confirm_alert ⇒ Object
Confirms alert
69 70 71 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 69 def confirm_alert @browser.alert.ok if @browser.alert.exists? end |
#delete_session_storage(key) ⇒ Object
Delete session storage item by key
164 165 166 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 164 def delete_session_storage(key) @browser.execute_script("return sessionStorage.removeItem('#{key}')") end |
#get_all_session_storage ⇒ Object
Returns an array with all the session storage items
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 149 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_current_url ⇒ Object
21 22 23 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 21 def get_current_url return @driver.current_url end |
#get_local_storage_item(key) ⇒ Object
Get local storage item by key
134 135 136 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 134 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
139 140 141 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 139 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
144 145 146 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 144 def get_session_storage_key(index) @browser.execute_script("return sessionStorage.key(#{index})") end |
#goto(uri) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 25 def goto(uri) uri = "https://#{uri}" unless uri =~ URI.regexp @driver.navigate.to uri run_checkers url 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
63 64 65 66 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 63 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.
52 53 54 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 52 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
41 42 43 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 41 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.
58 59 60 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 58 def scroll_vertical(y) @browser.execute_script("window.scrollBy(0,#{y})") end |
#scroll_x_y(x, y) ⇒ Object
Scrolls X and/or Y axis
46 47 48 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 46 def scroll_x_y(x, y) @browser.execute_script("window.scrollBy(#{x},#{y})") end |
#switch_browser_window(index) ⇒ Object
Switch to another browser window
35 36 37 38 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 35 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
118 119 120 121 122 123 124 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 118 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
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 81 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
107 108 109 110 111 112 113 |
# File 'lib/test_utils/browser_tests/web_helper.rb', line 107 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 |