2019-04-11 08:02:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-14 02:08:29 -04:00
|
|
|
# rubocop:disable QA/ElementWithPattern
|
2020-09-24 23:09:30 -04:00
|
|
|
RSpec.describe QA::Page::Base do
|
2017-12-22 07:01:12 -05:00
|
|
|
describe 'page helpers' do
|
|
|
|
it 'exposes helpful page helpers' do
|
2020-01-15 07:08:34 -05:00
|
|
|
expect(subject).to respond_to :refresh, :wait_until, :scroll_to
|
2017-12-22 07:01:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-22 09:40:46 -05:00
|
|
|
describe '.view', 'DSL for defining view partials' do
|
2017-12-22 07:01:12 -05:00
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
view 'path/to/some/view.html.haml' do
|
2022-04-14 02:08:29 -04:00
|
|
|
element :something, 'string pattern'
|
|
|
|
element :something_else, /regexp pattern/
|
2017-12-22 07:01:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
view 'path/to/some/_partial.html.haml' do
|
2022-04-14 02:08:29 -04:00
|
|
|
element :another_element, 'string pattern'
|
2017-12-22 07:01:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'makes it possible to define page views' do
|
|
|
|
expect(subject.views.size).to eq 2
|
2020-03-27 08:07:43 -04:00
|
|
|
expect(subject.views).to all(be_an_instance_of(QA::Page::View))
|
2017-12-22 07:01:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'populates views objects with data about elements' do
|
2018-02-16 06:00:13 -05:00
|
|
|
expect(subject.elements.size).to eq 3
|
2020-03-27 08:07:43 -04:00
|
|
|
expect(subject.elements).to all(be_an_instance_of(QA::Page::Element))
|
2018-02-16 06:00:13 -05:00
|
|
|
expect(subject.elements.map(&:name))
|
|
|
|
.to eq [:something, :something_else, :another_element]
|
2017-12-22 07:01:12 -05:00
|
|
|
end
|
|
|
|
end
|
2017-12-22 09:40:46 -05:00
|
|
|
|
|
|
|
describe '.errors' do
|
|
|
|
let(:view) { double('view') }
|
|
|
|
|
2018-01-09 06:06:58 -05:00
|
|
|
context 'when page has views and elements defined' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:views)
|
|
|
|
.and_return([view])
|
2017-12-22 09:40:46 -05:00
|
|
|
|
2018-01-09 06:06:58 -05:00
|
|
|
allow(view).to receive(:errors).and_return(['some error'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'iterates views composite and returns errors' do
|
|
|
|
expect(described_class.errors).to eq ['some error']
|
|
|
|
end
|
2017-12-22 09:40:46 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 06:06:58 -05:00
|
|
|
context 'when page has no views and elements defined' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:views).and_return([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends an error about missing views / elements block' do
|
|
|
|
expect(described_class.errors)
|
2018-01-09 06:34:47 -05:00
|
|
|
.to include 'Page class does not have views / elements defined!'
|
2018-01-09 06:06:58 -05:00
|
|
|
end
|
2017-12-22 09:40:46 -05:00
|
|
|
end
|
|
|
|
end
|
2019-02-11 04:04:59 -05:00
|
|
|
|
2020-01-17 13:08:41 -05:00
|
|
|
describe '#wait_until' do
|
2019-02-11 04:04:59 -05:00
|
|
|
subject { Class.new(described_class).new }
|
|
|
|
|
|
|
|
context 'when the condition is true' do
|
|
|
|
it 'does not refresh' do
|
|
|
|
expect(subject).not_to receive(:refresh)
|
|
|
|
|
2020-01-15 07:08:34 -05:00
|
|
|
subject.wait_until(max_duration: 0.01, raise_on_failure: false) { true }
|
2019-02-11 04:04:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
2020-01-15 07:08:34 -05:00
|
|
|
expect(subject.wait_until(max_duration: 0.1, raise_on_failure: false) { true }).to be_truthy
|
2019-02-11 04:04:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the condition is false' do
|
|
|
|
it 'refreshes' do
|
|
|
|
expect(subject).to receive(:refresh).at_least(:once)
|
|
|
|
|
2020-01-15 07:08:34 -05:00
|
|
|
subject.wait_until(max_duration: 0.01, raise_on_failure: false) { false }
|
2019-02-11 04:04:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
allow(subject).to receive(:refresh)
|
|
|
|
|
2020-01-15 07:08:34 -05:00
|
|
|
expect(subject.wait_until(max_duration: 0.01, raise_on_failure: false) { false }).to be_falsey
|
2019-02-11 04:04:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-09 19:07:56 -05:00
|
|
|
|
|
|
|
describe '#all_elements' do
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:all)
|
2022-04-14 02:08:29 -04:00
|
|
|
allow(subject).to receive(:wait_for_requests)
|
2020-01-09 19:07:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if count or minimum are not specified' do
|
|
|
|
expect { subject.all_elements(:foo) }.to raise_error ArgumentError
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an error if :minimum, :maximum, :count, or :between is specified' do
|
|
|
|
[:minimum, :maximum, :count, :between].each do |param|
|
|
|
|
expect { subject.all_elements(:foo, param => 1) }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-09 05:10:17 -04:00
|
|
|
|
2022-04-14 02:08:29 -04:00
|
|
|
describe 'elements' do
|
2020-04-09 05:10:17 -04:00
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
view 'path/to/some/view.html.haml' do
|
|
|
|
element :something, required: true
|
|
|
|
element :something_else
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#elements' do
|
|
|
|
it 'returns all elements' do
|
|
|
|
expect(subject.elements.size).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#required_elements' do
|
|
|
|
it 'returns only required elements' do
|
|
|
|
expect(subject.required_elements.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#visible?', 'Page is currently visible' do
|
|
|
|
let(:page) { subject.new }
|
|
|
|
|
2022-04-14 02:08:29 -04:00
|
|
|
before do
|
|
|
|
allow(page).to receive(:wait_for_requests)
|
|
|
|
end
|
|
|
|
|
2020-04-09 05:10:17 -04:00
|
|
|
context 'with elements' do
|
2022-04-14 02:08:29 -04:00
|
|
|
before do
|
|
|
|
allow(page).to receive(:has_no_element?).and_return(has_no_element)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with element on the page' do
|
|
|
|
let(:has_no_element) { false }
|
2020-04-09 05:10:17 -04:00
|
|
|
|
|
|
|
it 'is visible' do
|
|
|
|
expect(page).to be_visible
|
|
|
|
end
|
|
|
|
|
2022-04-14 02:08:29 -04:00
|
|
|
it 'does not raise error if page has elements' do
|
|
|
|
expect { page.visible? }.not_to raise_error
|
2020-04-09 05:10:17 -04:00
|
|
|
end
|
2022-04-14 02:08:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with element not on the page' do
|
|
|
|
let(:has_no_element) { true }
|
2020-04-09 05:10:17 -04:00
|
|
|
|
|
|
|
it 'is not visible' do
|
|
|
|
expect(page).not_to be_visible
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-14 02:08:29 -04:00
|
|
|
context 'with no elements' do
|
2020-04-09 05:10:17 -04:00
|
|
|
subject do
|
|
|
|
Class.new(described_class) do
|
|
|
|
view 'path/to/some/view.html.haml' do
|
|
|
|
element :something
|
|
|
|
element :something_else
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:page) { subject.new }
|
|
|
|
|
|
|
|
it 'raises error if page has no required elements' do
|
|
|
|
expect { page.visible? }.to raise_error(described_class::NoRequiredElementsError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-22 07:01:12 -05:00
|
|
|
end
|
2022-04-14 02:08:29 -04:00
|
|
|
# rubocop:enable QA/ElementWithPattern
|