debian-mirror-gitlab/spec/support/helpers/merge_request_diff_helpers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.6 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module MergeRequestDiffHelpers
2022-04-04 11:22:00 +05:30
PageEndReached = Class.new(StandardError)
2018-03-17 18:26:18 +05:30
def click_diff_line(line_holder, diff_side = nil)
line = get_line_components(line_holder, diff_side)
2022-04-04 11:22:00 +05:30
scroll_to_elements_bottom(line_holder)
2021-09-30 23:02:18 +05:30
line_holder.hover
line[:num].find('.js-add-diff-note-button').click
2018-03-17 18:26:18 +05:30
end
def get_line_components(line_holder, diff_side = nil)
if diff_side.nil?
get_inline_line_components(line_holder)
else
get_parallel_line_components(line_holder, diff_side)
end
end
def get_inline_line_components(line_holder)
{ content: line_holder.find('.line_content', match: :first), num: line_holder.find('.diff-line-num', match: :first) }
end
def get_parallel_line_components(line_holder, diff_side = nil)
side_index = diff_side == 'left' ? 0 : 1
# Wait for `.line_content`
line_holder.find('.line_content', match: :first)
# Wait for `.diff-line-num`
line_holder.find('.diff-line-num', match: :first)
{ content: line_holder.all('.line_content')[side_index], num: line_holder.all('.diff-line-num')[side_index] }
end
2022-04-04 11:22:00 +05:30
def has_reached_page_end
evaluate_script("(window.innerHeight + window.scrollY) >= document.body.offsetHeight")
end
def scroll_to_elements_bottom(element)
evaluate_script("(function(el) {
window.scrollBy(0, el.getBoundingClientRect().bottom - window.innerHeight);
})(arguments[0]);", element.native)
end
# we're not using Capybara's .obscured here because it also checks if the element is clickable
def within_viewport?(element)
evaluate_script("(function(el) {
var rect = el.getBoundingClientRect();
return (
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
);
})(arguments[0]);", element.native)
end
def find_within_viewport(selector, **options)
begin
element = find(selector, **options, wait: 2)
rescue Capybara::ElementNotFound
return
end
return element if within_viewport?(element)
nil
end
def find_by_scrolling(selector, **options)
element = find_within_viewport(selector, **options)
return element if element
page.execute_script "window.scrollTo(0,0)"
until element
if has_reached_page_end
raise PageEndReached, "Failed to find any elements matching a selector '#{selector}' by scrolling. Page end reached."
end
page.execute_script "window.scrollBy(0,window.innerHeight/1.5)"
element = find_within_viewport(selector, **options)
end
element
end
2018-03-17 18:26:18 +05:30
end