Module: Wist
- Defined in:
- lib/wist.rb
Defined Under Namespace
Modules: Helpers
Instance Method Summary collapse
- #alert_accept(expected_msg = false) ⇒ Object
- #assert_text(text, el) ⇒ Object
- #assert_text_include(text, el) ⇒ Object
- #click(selector) ⇒ Object
- #click_by_text(selector, text, text_include: true, downcase: true) ⇒ Object
- #driver ⇒ Object
- #get_js(code) ⇒ Object
- #get_val(selector) ⇒ Object
- #has_class?(el, class_name) ⇒ Boolean
- #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 ⇒ 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) ⇒ Object
- #wait_til_element_visible(selector) ⇒ Object
- #wait_until(timeout: Capybara.default_wait_time) ⇒ Object
Instance Method Details
#alert_accept(expected_msg = false) ⇒ Object
62 63 64 65 66 |
# File 'lib/wist.rb', line 62 def alert_accept(expected_msg = false) page.execute_script "window.alert = function(msg) { window.lastAlertMsg = msg; }" yield assert_equal(expected_msg, get_js('window.lastAlertMsg')) if expected_msg end |
#assert_text(text, el) ⇒ Object
136 137 138 |
# File 'lib/wist.rb', line 136 def assert_text(text, el) assert_equal(text, el.text) end |
#assert_text_include(text, el) ⇒ Object
140 141 142 |
# File 'lib/wist.rb', line 140 def assert_text_include(text, el) assert_equal(true, el.text.include?(text)) end |
#click(selector) ⇒ Object
28 29 30 |
# File 'lib/wist.rb', line 28 def click(selector) find(selector).click end |
#click_by_text(selector, text, text_include: true, downcase: true) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/wist.rb', line 148 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 |
#driver ⇒ Object
50 51 52 |
# File 'lib/wist.rb', line 50 def driver page.driver.browser end |
#get_js(code) ⇒ Object
68 69 70 |
# File 'lib/wist.rb', line 68 def get_js(code) page.evaluate_script code end |
#get_val(selector) ⇒ Object
76 77 78 |
# File 'lib/wist.rb', line 76 def get_val(selector) find(selector).value end |
#has_class?(el, class_name) ⇒ Boolean
85 86 87 88 89 |
# File 'lib/wist.rb', line 85 def has_class?(el, class_name) klass = el[:class] return false if Helpers.blank?(klass) klass.split(' ').include?(class_name) end |
#jquery_selector(selector) ⇒ Object
72 73 74 |
# File 'lib/wist.rb', line 72 def jquery_selector(selector) "$('#{selector}')" end |
#parent(el) ⇒ Object
144 145 146 |
# File 'lib/wist.rb', line 144 def parent(el) el.first(:xpath, './/..') end |
#refresh ⇒ Object
98 99 100 |
# File 'lib/wist.rb', line 98 def refresh visit current_url end |
#scroll_to(selector) ⇒ Object
111 112 113 |
# File 'lib/wist.rb', line 111 def scroll_to(selector) page.execute_script("#{jquery_selector('html, body')}.scrollTop(#{jquery_selector(selector)}.offset().top)") end |
#set_cookie(k, v) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/wist.rb', line 32 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
80 81 82 83 |
# File 'lib/wist.rb', line 80 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
39 40 41 42 |
# File 'lib/wist.rb', line 39 def set_value_and_trigger_evts(selector, val) find(selector).set(val) page.execute_script("$('#{selector}').focusout().change().trigger('input')") end |
#switch_to_window_and_execute ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/wist.rb', line 54 def switch_to_window_and_execute wait_until { driver.window_handles.size == 2 } within_window(driver.window_handles.last) do yield driver.close end end |
#verify_tweet_button(text) ⇒ Object
44 45 46 47 48 |
# File 'lib/wist.rb', line 44 def (text) = nil wait_until { = find('.twitter-share-button')[:src] } assert_equal text, CGI.parse(URI.parse(.gsub('#', '?')).query)['text'][0] end |
#visit_with_retries(url, retries: 5, wait_time: 10) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/wist.rb', line 122 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
102 103 104 |
# File 'lib/wist.rb', line 102 def wait_for_ajax wait_until { get_js '$.isReady && ($.active == 0)' } end |
#wait_for_new_url(element_to_click) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/wist.rb', line 91 def wait_for_new_url(element_to_click) old_url = current_url element_to_click.click sleep 1 wait_until { current_url != old_url } end |
#wait_til_element_visible(selector) ⇒ Object
106 107 108 109 |
# File 'lib/wist.rb', line 106 def wait_til_element_visible(selector) has_css?(selector, visible: true) find(selector) end |
#wait_until(timeout: Capybara.default_wait_time) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/wist.rb', line 18 def wait_until(timeout: Capybara.default_wait_time) Selenium::WebDriver::Wait.new(timeout: timeout).until do begin yield rescue false end end end |