Module: Wist
- Defined in:
- lib/wist.rb
Defined Under Namespace
Modules: Helpers
Class Attribute Summary collapse
-
.rspec ⇒ Object
Returns the value of attribute rspec.
Instance Method Summary collapse
- #alert_accept(expected_msg = false) ⇒ Object
- #assert_el_has_link(selector, link) ⇒ Object
- #assert_text(text, el) ⇒ Object
- #assert_text_include(text, el) ⇒ Object
- #click(*args) ⇒ Object
- #click_by_text(selector, text, text_include: true, downcase: true) ⇒ Object
- #do_instantly ⇒ Object
- #driver ⇒ Object
- #get_js(code) ⇒ Object
- #get_val(selector) ⇒ Object
- #has_class?(el, class_name) ⇒ Boolean
- #has_css_instant?(*args) ⇒ Boolean
- #interact_with_confirm(accept = true) ⇒ Object
- #jquery_selector(selector) ⇒ Object
- #parent(el) ⇒ Object
- #refresh ⇒ Object
- #scroll_to(selector) ⇒ Object
- #set_cookie(k, v) ⇒ Object
- #set_input_and_press_enter(selector, val) ⇒ Object
- #set_value_and_trigger_evts(selector, val) ⇒ Object
- #switch_to_window_and_execute(new_window_lambda) ⇒ Object
- #verify_tweet_button(text) ⇒ Object
- #visit_with_retries(url, retries: 5, wait_time: 10) ⇒ Object
- #wait_for_ajax ⇒ Object
- #wait_for_new_url(element_to_click = nil) ⇒ Object
- #wait_til_element_visible(selector) ⇒ Object
- #wait_until(timeout: false) ⇒ Object
- #wist_assert(value, expected) ⇒ Object
Class Attribute Details
.rspec ⇒ Object
Returns the value of attribute rspec.
11 12 13 |
# File 'lib/wist.rb', line 11 def rspec @rspec end |
Instance Method Details
#alert_accept(expected_msg = false) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/wist.rb', line 80 def alert_accept(expected_msg = false) page.execute_script "window.alert = function(msg) { window.lastAlertMsg = msg; }" yield wait_until do get_js('window.lastAlertMsg') == expected_msg end if expected_msg end |
#assert_el_has_link(selector, link) ⇒ Object
210 211 212 |
# File 'lib/wist.rb', line 210 def assert_el_has_link(selector, link) wist_assert(get_js("$('#{selector}').attr('href')"), link) end |
#assert_text(text, el) ⇒ Object
164 165 166 |
# File 'lib/wist.rb', line 164 def assert_text(text, el) wist_assert(el.text, text) end |
#assert_text_include(text, el) ⇒ Object
168 169 170 |
# File 'lib/wist.rb', line 168 def assert_text_include(text, el) wist_assert(el.text.include?(text), true) end |
#click(*args) ⇒ Object
43 44 45 |
# File 'lib/wist.rb', line 43 def click(*args) find(*args).click end |
#click_by_text(selector, text, text_include: true, downcase: true) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/wist.rb', line 176 def click_by_text(selector, text, text_include: true, downcase: true) all_with_wait(selector, visible: true).find do |el| el_text = downcase ? el.text.downcase : el.text text_include ? el_text.include?(text) : el_text == text end.click end |
#do_instantly ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/wist.rb', line 194 def do_instantly lambda do default_wait_time = Capybara.default_wait_time @old_wait_time = default_wait_time if default_wait_time > 0 end.() Capybara.default_wait_time = 0 begin yield rescue => e raise(e) ensure Capybara.default_wait_time = @old_wait_time end end |
#driver ⇒ Object
65 66 67 |
# File 'lib/wist.rb', line 65 def driver page.driver.browser end |
#get_js(code) ⇒ Object
89 90 91 |
# File 'lib/wist.rb', line 89 def get_js(code) page.evaluate_script code end |
#get_val(selector) ⇒ Object
97 98 99 |
# File 'lib/wist.rb', line 97 def get_val(selector) find(selector).value end |
#has_class?(el, class_name) ⇒ Boolean
106 107 108 109 110 |
# File 'lib/wist.rb', line 106 def has_class?(el, class_name) klass = el[:class] return false if Helpers.blank?(klass) klass.split(' ').include?(class_name) end |
#has_css_instant?(*args) ⇒ Boolean
190 191 192 |
# File 'lib/wist.rb', line 190 def has_css_instant?(*args) do_instantly { has_css?(*args) } end |
#interact_with_confirm(accept = true) ⇒ Object
76 77 78 |
# File 'lib/wist.rb', line 76 def interact_with_confirm(accept = true) page.execute_script("window.confirm = function() { return #{accept.to_json} }") end |
#jquery_selector(selector) ⇒ Object
93 94 95 |
# File 'lib/wist.rb', line 93 def jquery_selector(selector) "$('#{selector}')" end |
#parent(el) ⇒ Object
172 173 174 |
# File 'lib/wist.rb', line 172 def parent(el) el.first(:xpath, './/..') end |
#refresh ⇒ Object
126 127 128 |
# File 'lib/wist.rb', line 126 def refresh visit current_url end |
#scroll_to(selector) ⇒ Object
139 140 141 |
# File 'lib/wist.rb', line 139 def scroll_to(selector) page.execute_script("#{jquery_selector('html, body')}.scrollTop(#{jquery_selector(selector)}.offset().top)") end |
#set_cookie(k, v) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/wist.rb', line 47 def (k, v) page.execute_script <<-eos (function (e,t,n){var r=new Date;r.setDate(r.getDate()+n);var i=escape(t)+(n==null?"":"; expires="+r.toUTCString());document.cookie=e+"="+i} (#{k.to_json}, #{v.to_json})); eos end |
#set_input_and_press_enter(selector, val) ⇒ Object
101 102 103 104 |
# File 'lib/wist.rb', line 101 def set_input_and_press_enter(selector, val) wait_til_element_visible(selector) page.execute_script("#{jquery_selector(selector)}.val('#{val}').trigger({type: 'keydown', which: 13}).trigger({ type: 'keypress', which: 13})") end |
#set_value_and_trigger_evts(selector, val) ⇒ Object
54 55 56 57 |
# File 'lib/wist.rb', line 54 def set_value_and_trigger_evts(selector, val) find(selector).set(val) page.execute_script("$('#{selector}').focusout().blur().change().trigger('input')") end |
#switch_to_window_and_execute(new_window_lambda) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/wist.rb', line 69 def switch_to_window_and_execute(new_window_lambda) within_window(window_opened_by(&new_window_lambda)) do yield driver.close end end |
#verify_tweet_button(text) ⇒ Object
59 60 61 62 63 |
# File 'lib/wist.rb', line 59 def (text) = nil wait_until { = find('.twitter-share-button')[:src] } wist_assert(CGI.parse(URI.parse(.gsub('#', '?')).query)['text'][0], text) end |
#visit_with_retries(url, retries: 5, wait_time: 10) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/wist.rb', line 150 def visit_with_retries(url, retries: 5, wait_time: 10) raise 'only works with poltergeist' unless Capybara.javascript_driver == :poltergeist status = nil retries.times do |i| status = visit(url)['status'] break if status == 'success' puts "visiting #{url} failed, attempting retry #{i + 1}/#{retries} in #{wait_time} second(s)" sleep wait_time end raise "visit #{url} failed" unless status == 'success' end |
#wait_for_ajax ⇒ Object
130 131 132 |
# File 'lib/wist.rb', line 130 def wait_for_ajax wait_until { get_js '$.isReady && ($.active == 0)' } end |
#wait_for_new_url(element_to_click = nil) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/wist.rb', line 112 def wait_for_new_url(element_to_click = nil) old_url = current_url if element_to_click.nil? yield else element_to_click = find(element_to_click, visible: true) if element_to_click.is_a?(String) element_to_click.click end sleep 1 wait_until { current_url != old_url } end |
#wait_til_element_visible(selector) ⇒ Object
134 135 136 137 |
# File 'lib/wist.rb', line 134 def wait_til_element_visible(selector) has_css?(selector, visible: true) find(selector) end |
#wait_until(timeout: false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wist.rb', line 31 def wait_until(timeout: false) do_instantly do Selenium::WebDriver::Wait.new(timeout: timeout || @old_wait_time).until do begin yield rescue false end end end end |