Module: Wist

Defined in:
lib/wist.rb

Defined Under Namespace

Modules: Helpers

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.wait_time_methodObject

Returns the value of attribute wait_time_method.



16
17
18
# File 'lib/wist.rb', line 16

def wait_time_method
  @wait_time_method
end

Class Method Details

.included(klass) ⇒ Object



7
8
9
# File 'lib/wist.rb', line 7

def self.included(klass)
  klass.include CommonAssert
end

Instance Method Details

#alert_accept(expected_msg = false) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/wist.rb', line 75

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


202
203
204
# File 'lib/wist.rb', line 202

def assert_el_has_link(selector, link)
  common_assert_equal(get_js("$('#{selector}').attr('href')"), link)
end

#assert_text(text, el_or_selector) ⇒ Object



152
153
154
# File 'lib/wist.rb', line 152

def assert_text(text, el_or_selector)
  common_assert_equal(process_el_or_selector(el_or_selector).text, text)
end

#assert_text_include(text, el) ⇒ Object



156
157
158
# File 'lib/wist.rb', line 156

def assert_text_include(text, el)
  common_assert_equal(el.text.include?(text), true)
end

#clickObject



45
46
47
# File 'lib/wist.rb', line 45

def click(...)
  find(...).click
end

#click_by_text(selector, text, text_include: true, downcase: true) ⇒ Object



164
165
166
167
168
169
# File 'lib/wist.rb', line 164

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_instantlyObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/wist.rb', line 186

def do_instantly
  lambda do
    default_max_wait_time = get_capybara_wait_time
    @old_wait_time = default_max_wait_time if default_max_wait_time > 0
  end.()
  Capybara.public_send("#{Wist.wait_time_method}=", 0)

  begin
    yield
  rescue => e
    raise(e)
  ensure
    Capybara.public_send("#{Wist.wait_time_method}=", @old_wait_time)
  end
end

#driverObject



60
61
62
# File 'lib/wist.rb', line 60

def driver
  page.driver.browser
end

#get_capybara_wait_timeObject



182
183
184
# File 'lib/wist.rb', line 182

def get_capybara_wait_time
  Capybara.public_send(Wist.wait_time_method)
end

#get_js(code) ⇒ Object



84
85
86
# File 'lib/wist.rb', line 84

def get_js(code)
  page.evaluate_script code
end

#get_val(selector) ⇒ Object



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

def get_val(selector)
  find(selector).value
end

#has_class?(el, class_name) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/wist.rb', line 101

def has_class?(el, class_name)
  klass = el[:class]
  return false if Helpers.blank?(klass)
  klass.split(' ').include?(class_name)
end

#has_css_instant?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/wist.rb', line 178

def has_css_instant?(...)
  do_instantly { has_css?(...) }
end

#interact_with_confirm(accept = true) ⇒ Object



71
72
73
# File 'lib/wist.rb', line 71

def interact_with_confirm(accept = true)
  page.execute_script("window.confirm = function() { return #{accept.to_json} }")
end

#jquery_selector(selector) ⇒ Object



88
89
90
# File 'lib/wist.rb', line 88

def jquery_selector(selector)
  "$('#{selector}')"
end

#parent(el) ⇒ Object



160
161
162
# File 'lib/wist.rb', line 160

def parent(el)
  el.first(:xpath, './/..')
end

#process_el_or_selector(el_or_selector) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/wist.rb', line 107

def process_el_or_selector(el_or_selector)
  if el_or_selector.is_a?(String)
    find(el_or_selector, visible: true)
  else
    el_or_selector
  end
end

#refreshObject



128
129
130
# File 'lib/wist.rb', line 128

def refresh
  visit current_url
end

#scroll_to(selector) ⇒ Object



141
142
143
# File 'lib/wist.rb', line 141

def scroll_to(selector)
  page.execute_script("#{jquery_selector('html, body')}.scrollTop(#{jquery_selector(selector)}.offset().top)")
end

#set_input_and_press_enter(selector, val) ⇒ Object



96
97
98
99
# File 'lib/wist.rb', line 96

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



49
50
51
52
# File 'lib/wist.rb', line 49

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



64
65
66
67
68
69
# File 'lib/wist.rb', line 64

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



54
55
56
57
58
# File 'lib/wist.rb', line 54

def verify_tweet_button(text)
  share_button_src = nil
  wait_until { share_button_src = find('.twitter-share-button')[:src] }
  common_assert_equal(CGI.parse(URI.parse(share_button_src.gsub('#', '?')).query)['text'][0], text)
end

#wait_for_ajaxObject



132
133
134
# File 'lib/wist.rb', line 132

def wait_for_ajax
  wait_until { get_js '$.isReady && ($.active == 0)' }
end

#wait_for_new_url(element_to_click = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wist.rb', line 115

def wait_for_new_url(element_to_click = nil)
  old_url = current_url

  if element_to_click.nil?
    yield
  else
    process_el_or_selector(element_to_click).click
  end

  sleep 1
  wait_until { current_url != old_url }
end

#wait_til_element_visible(selector) ⇒ Object



136
137
138
139
# File 'lib/wist.rb', line 136

def wait_til_element_visible(selector)
  has_css?(selector, visible: true)
  find(selector)
end

#wait_until(timeout: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wist.rb', line 33

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