1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
# frozen_string_literal: true
require 'capybara/dsl'
module QA
module Page
class Base
prepend Support::Page::Logging
include Capybara::DSL
include Scenario::Actable
include Support::WaitForRequests
extend Validatable
extend SingleForwardable
ElementNotFound = Class.new(RuntimeError)
class NoRequiredElementsError < RuntimeError
def initialize(page_class)
@page_class = page_class
super
end
def to_s
format(<<~MSG.strip, page: @page_class)
%{page} has no required elements.
See https://docs.gitlab.com/ee/development/testing_guide/end_to_end/dynamic_element_validation.html#required-elements
MSG
end
end
def_delegators :evaluator, :view, :views
def initialize
@retry_later_backoff = QA::Support::Repeater::DEFAULT_MAX_WAIT_TIME
end
def inspect
# For prettier failure messages
# Eg.: "expected QA::Page::File::Show not to have file "QA Test - File name"
# Instead of "expected #<QA::Page::File::Show:0x000055c6511e07b8 @retry_later_backoff=60> not to have file "QA Test - File name"
self.class.to_s
end
def assert_no_element(name)
assert_no_selector(element_selector_css(name))
end
def refresh(skip_finished_loading_check: false)
page.refresh
wait_for_requests(skip_finished_loading_check: skip_finished_loading_check)
end
def wait_until(
max_duration: 60,
sleep_interval: 0.1,
reload: true,
raise_on_failure: true,
skip_finished_loading_check_on_refresh: false,
message: nil
)
Support::Waiter.wait_until(
max_duration: max_duration,
sleep_interval: sleep_interval,
raise_on_failure: raise_on_failure,
message: message
) do
yield || (reload && refresh(skip_finished_loading_check: skip_finished_loading_check_on_refresh) && false)
end
end
def retry_until(max_attempts: 3, reload: false, sleep_interval: 0, raise_on_failure: true, message: nil, &block)
Support::Retrier.retry_until(
max_attempts: max_attempts,
reload_page: (reload && self),
sleep_interval: sleep_interval,
raise_on_failure: raise_on_failure,
message: message,
&block
)
end
def retry_on_exception(max_attempts: 3, reload: false, sleep_interval: 0.5, message: nil, &block)
Support::Retrier.retry_on_exception(
max_attempts: max_attempts,
reload_page: (reload && self),
sleep_interval: sleep_interval,
message: message,
&block
)
end
def scroll_to(selector, text: nil, &block)
wait_for_requests
page.execute_script <<~JS
var elements = Array.from(document.querySelectorAll('#{selector}'));
var text = '#{text}';
if (text.length > 0) {
elements.find(e => e.textContent === text).scrollIntoView();
} else {
elements[0].scrollIntoView();
}
JS
page.within(selector, &block) if block
end
# Returns true if successfully GETs the given URL
# Useful because `page.status_code` is unsupported by our driver, and
# we don't have access to the `response` to use `have_http_status`.
def asset_exists?(url)
page.execute_script <<~JS
xhr = new XMLHttpRequest();
xhr.open('GET', '#{url}', true);
xhr.send();
JS
return false unless wait_until(sleep_interval: 0.5, max_duration: 60, reload: false) do
page.evaluate_script('xhr.readyState == XMLHttpRequest.DONE')
end
page.evaluate_script('xhr.status') == 200
end
def find_element(name, **kwargs)
skip_finished_loading_check = kwargs.delete(:skip_finished_loading_check)
wait_for_requests(skip_finished_loading_check: skip_finished_loading_check)
element_selector = element_selector_css(name, reject_capybara_query_keywords(kwargs))
find(element_selector, **only_capybara_query_keywords(kwargs))
end
def only_capybara_query_keywords(kwargs)
kwargs.select { |kwarg| Capybara::Queries::SelectorQuery::VALID_KEYS.include?(kwarg) }
end
def reject_capybara_query_keywords(kwargs)
kwargs.reject { |kwarg| Capybara::Queries::SelectorQuery::VALID_KEYS.include?(kwarg) }
end
def active_element?(name)
find_element(name, class: 'active')
end
def all_elements(name, **kwargs)
all_args = [:minimum, :maximum, :count, :between]
if kwargs.keys.none? { |key| all_args.include?(key) }
raise ArgumentError, "Please use :minimum, :maximum, :count, or :between so that all is more reliable"
end
wait_for_requests
all(element_selector_css(name), **kwargs)
end
def check_element(name, click_by_js = false, visibility = false)
if find_element(name, visible: visibility).checked?
QA::Runtime::Logger.debug("#{name} is already checked")
return
end
retry_until(sleep_interval: 1) do
click_checkbox_or_radio(name, click_by_js, visibility)
checked = find_element(name, visible: visibility).checked?
QA::Runtime::Logger.debug(checked ? "#{name} was checked" : "#{name} was not checked")
checked
end
end
def uncheck_element(name, click_by_js = false, visibility = false)
unless find_element(name, visible: visibility).checked?
QA::Runtime::Logger.debug("#{name} is already unchecked")
return
end
retry_until(sleep_interval: 1) do
click_checkbox_or_radio(name, click_by_js, visibility)
unchecked = !find_element(name, visible: visibility).checked?
QA::Runtime::Logger.debug(unchecked ? "#{name} was unchecked" : "#{name} was not unchecked")
unchecked
end
end
# Method for selecting radios
def choose_element(name, click_by_js = false, visibility = false)
if find_element(name, visible: visibility).checked?
QA::Runtime::Logger.debug("#{name} is already selected")
return
end
retry_until(sleep_interval: 1) do
click_checkbox_or_radio(name, click_by_js, visibility)
selected = find_element(name, visible: visibility).checked?
QA::Runtime::Logger.debug(selected ? "#{name} was selected" : "#{name} was not selected")
selected
end
wait_for_requests
end
# Use this to simulate moving the pointer to an element's coordinate
# and sending a click event.
# This is a helpful workaround when there is a transparent element overlapping
# the target element and so, normal `click_element` on target would raise
# Selenium::WebDriver::Error::ElementClickInterceptedError
def click_element_coordinates(name, **kwargs)
page.driver.browser.action.move_to(find_element(name, **kwargs).native).click.perform
end
# replace with (..., page = self.class)
def click_element(name, page = nil, **kwargs)
skip_finished_loading_check = kwargs.delete(:skip_finished_loading_check)
wait_for_requests(skip_finished_loading_check: skip_finished_loading_check)
wait = kwargs.delete(:wait) || Capybara.default_max_wait_time
text = kwargs.delete(:text)
find(element_selector_css(name, kwargs), text: text, wait: wait).click
page.validate_elements_present! if page
end
# Uses capybara to locate and click an element instead of `click_element`.
# This can be used when it's not possible to add a QA selector but we still want to log the click
#
# @param [String] method the capybara method to use
# @param [String] locator the selector used to find the element
def click_via_capybara(method, locator)
page.public_send(method, locator)
end
def fill_element(name, content)
# `click_element_coordinates` is used to ensure the element is focused.
# Without it, flakiness can occur on pages with GitLab keyboard shortcuts enabled,
# where certain keys trigger actions when typed elsewhere on the page.
click_element_coordinates(name)
find_element(name).set(content)
end
def select_element(name, value)
element = find_element(name)
return if element.text == value
element.select value
end
def has_active_element?(name, **kwargs)
has_element?(name, class: 'active', **kwargs)
end
def has_element?(name, **kwargs)
disabled = kwargs.delete(:disabled)
original_kwargs = kwargs.dup
wait = kwargs.delete(:wait) || Capybara.default_max_wait_time
text = kwargs.delete(:text)
klass = kwargs.delete(:class)
visible = kwargs.delete(:visible)
visible = visible.nil? && true
try_find_element = lambda do |wait|
if disabled.nil?
has_css?(element_selector_css(name, kwargs), text: text, wait: wait, class: klass, visible: visible)
else
find_element(name, **original_kwargs).disabled? == disabled
end
rescue Capybara::ElementNotFound
false
end
# Check for the element before waiting for requests, just in case unrelated requests are in progress.
# This is to avoid waiting unnecessarily after the element we're interested in has already appeared.
return true if try_find_element.call(wait)
# If the element didn't appear, wait for requests and then check again
wait_for_requests(skip_finished_loading_check: !!kwargs.delete(:skip_finished_loading_check))
# We only wait one second now because we previously waited the full expected duration,
# plus however long it took for requests to complete. One second should be enough
# for the UI to update after requests complete.
try_find_element.call(1)
end
def has_no_element?(name, **kwargs)
wait_for_requests
wait = kwargs.delete(:wait) || Capybara.default_max_wait_time
text = kwargs.delete(:text)
has_no_css?(element_selector_css(name, kwargs), wait: wait, text: text)
end
def has_text?(text, wait: Capybara.default_max_wait_time)
wait_for_requests
page.has_text?(text, wait: wait)
end
def has_no_text?(text, wait: Capybara.default_max_wait_time)
wait_for_requests
page.has_no_text?(text, wait: wait)
end
def has_normalized_ws_text?(text, wait: Capybara.default_max_wait_time)
has_text?(text.gsub(/\s+/, " "), wait: wait)
end
def finished_loading_block?
wait_for_requests
has_no_css?('.gl-spinner', wait: Capybara.default_max_wait_time)
end
def has_loaded_all_images?
# I don't know of a foolproof way to wait for all images to load
# This loop gives time for the img tags to be rendered and for
# images to start loading.
previous_total_images = 0
wait_until(sleep_interval: 1) do
current_total_images = all("img").size
result = previous_total_images == current_total_images
previous_total_images = current_total_images
result
end
# Retry until all images found can be fetched via HTTP, and
# check that the image has a non-zero natural width (a broken
# img tag could have a width, but wouldn't have a natural width)
# Unfortunately, this doesn't account for SVGs. They're rendered
# as HTML, so there doesn't seem to be a way to check that they
# display properly via Selenium. However, if the SVG couldn't be
# rendered (e.g., because the file doesn't exist), the whole page
# won't display properly, so we should catch that with the test
# this method is called from.
# The user's avatar is an img, which could be a gravatar image,
# so we skip that by only checking for images hosted internally
retry_until(sleep_interval: 1) do
all("img").all? do |image|
next true unless URI(image['src']).host == URI(page.current_url).host
asset_exists?(image['src']) && image['naturalWidth'].to_i > 0
end
end
end
def wait_for_animated_element(name)
# It would be ideal if we could detect when the animation is complete
# but in some cases there's nothing we can easily access via capybara
# so instead we wait for the element, and then we wait a little longer
raise ElementNotFound, %(Couldn't find element named "#{name}") unless has_element?(name)
sleep 1
end
def within_element(name, **kwargs)
wait_for_requests(skip_finished_loading_check: kwargs.delete(:skip_finished_loading_check))
text = kwargs.delete(:text)
page.within(element_selector_css(name, kwargs), text: text) do
yield
end
end
def within_element_by_index(name, index)
page.within all_elements(name, minimum: index + 1)[index] do
yield
end
end
def scroll_to_element(name, *kwargs)
text = kwargs.delete(:text)
scroll_to(element_selector_css(name, kwargs), text: text)
end
def element_selector_css(name, *attributes)
return name.selector_css if name.is_a? Page::Element
Page::Element.new(name, *attributes).selector_css
end
def click_link_with_text(text)
wait_for_requests
click_link text
end
def visit_link_in_element(name)
visit find_element(name)['href']
end
def wait_if_retry_later
return if @retry_later_backoff > QA::Support::Repeater::DEFAULT_MAX_WAIT_TIME * 5
if has_css?('body', text: 'Retry later', wait: 0)
QA::Runtime::Logger.warn("`Retry later` error occurred. Sleeping for #{@retry_later_backoff} seconds...")
sleep @retry_later_backoff
refresh
@retry_later_backoff += QA::Support::Repeater::DEFAULT_MAX_WAIT_TIME
wait_if_retry_later
end
end
def current_host
URI(page.current_url).host
end
def self.path
raise NotImplementedError
end
def self.evaluator
@evaluator ||= Page::Base::DSL.new
end
def self.errors
return ["Page class does not have views / elements defined!"] if views.empty?
views.flat_map(&:errors)
end
def self.elements
views.flat_map(&:elements)
end
def self.required_elements
elements.select(&:required?)
end
def send_keys_to_element(name, keys)
find_element(name).send_keys(keys)
end
def visible?
raise NoRequiredElementsError, self.class if self.class.required_elements.empty?
self.class.required_elements.each do |required_element|
return false if has_no_element? required_element
end
true
end
class DSL
attr_reader :views
def initialize
@views = []
end
def view(path, &block)
Page::View.evaluate(&block).tap do |view|
@views.push(Page::View.new(path, view.elements))
end
end
end
private
def click_checkbox_or_radio(name, click_by_js, visibility)
box = find_element(name, visible: visibility)
# Some checkboxes and radio buttons are hidden by their labels and cannot be clicked directly
click_by_js ? page.execute_script("arguments[0].click();", box) : box.click
end
def feature_flag_controlled_element(feature_flag, element_when_flag_enabled, element_when_flag_disabled, visibility = true)
# Feature flags can change the UI elements shown, but we need admin access to get feature flag values, which
# prevents us running the tests on production. Instead we detect the UI element that should be shown when the
# feature flag is enabled and otherwise use the element that should be displayed when the feature flag is
# disabled.
# Check both options once quickly so that the test doesn't wait unnecessarily if the UI has loaded
# We wait for requests first and wait one second for the element because it can take a moment for a Vue app to
# load and render the UI
wait_for_requests
return element_when_flag_enabled if has_element?(element_when_flag_enabled, wait: 1, visible: visibility)
return element_when_flag_disabled if has_element?(element_when_flag_disabled, wait: 1, visibile: visibility)
# Check both options again, this time waiting for the default duration
return element_when_flag_enabled if has_element?(element_when_flag_enabled, visible: visibility)
return element_when_flag_disabled if has_element?(element_when_flag_disabled, visible: visibility)
raise ElementNotFound,
"Could not find the expected element as #{element_when_flag_enabled} or #{element_when_flag_disabled}." \
"The relevant feature flag is #{feature_flag}"
end
end
end
end
QA::Page::Base.prepend_mod_with('Page::Base', namespace: QA)
|