Commit be621e42 authored by Mark Lapierre's avatar Mark Lapierre

Add and update QA text content matchers

- Adds a negatable have_text/have_content matcher to be used instead
  of Capybara's built-in ones
- Include all custom matchers during RSpec config. This should make it
  simpler to add more matchers - we just need to put them in
  qa/spec/support/matchers in the Matchers module
- Update a few tests that used `expect(page).to have_content`
parent 325aceb0
...@@ -12,7 +12,9 @@ module QA ...@@ -12,7 +12,9 @@ module QA
login_page.login('user1', 'user1pass') login_page.login('user1', 'user1pass')
end end
expect(page).to have_content('Welcome to GitLab') Page::Dashboard::Welcome.perform do |welcome|
expect(welcome).to have_content('Welcome to GitLab')
end
end end
end end
end end
......
...@@ -15,9 +15,9 @@ module QA ...@@ -15,9 +15,9 @@ module QA
Page::Project::Menu.perform(&:click_members) Page::Project::Menu.perform(&:click_members)
Page::Project::Members.perform do |members| Page::Project::Members.perform do |members|
members.add_member(user.username) members.add_member(user.username)
end
expect(page).to have_content(/@#{user.username}( Is using seat)?(\n| )?Given access/) expect(members).to have_content(/@#{user.username}( Is using seat)?(\n| )?Given access/)
end
end end
end end
end end
......
...@@ -11,12 +11,14 @@ module QA ...@@ -11,12 +11,14 @@ module QA
project.description = 'create awesome project test' project.description = 'create awesome project test'
end end
expect(page).to have_content(created_project.name) Page::Project::Show.perform do |project|
expect(page).to have_content( expect(project).to have_content(created_project.name)
/Project \S?awesome-project\S+ was successfully created/ expect(project).to have_content(
) /Project \S?awesome-project\S+ was successfully created/
expect(page).to have_content('create awesome project test') )
expect(page).to have_content('The repository for this project is empty') expect(project).to have_content('create awesome project test')
expect(project).to have_content('The repository for this project is empty')
end
end end
end end
end end
......
...@@ -49,23 +49,26 @@ module QA ...@@ -49,23 +49,26 @@ module QA
end end
def verify_repository_import def verify_repository_import
expect(page).to have_content('This test project is used for automated GitHub import by GitLab QA.') Page::Project::Show.perform do |project|
expect(page).to have_content(imported_project.name) expect(project).to have_content('This test project is used for automated GitHub import by GitLab QA.')
expect(project).to have_content(imported_project.name)
end
end end
def verify_issues_import def verify_issues_import
QA::Support::Retrier.retry_on_exception do QA::Support::Retrier.retry_on_exception do
Page::Project::Menu.perform(&:click_issues) Page::Project::Menu.perform(&:click_issues)
expect(page).to have_content('This is a sample issue')
click_link 'This is a sample issue' Page::Project::Issue::Show.perform do |issue_page|
expect(issue_page).to have_content('This is a sample issue')
expect(page).to have_content('This is a sample first comment') click_link 'This is a sample issue'
# Comments expect(issue_page).to have_content('This is a sample first comment')
comment_text = 'This is a comment from @sliaquat'
# Comments
comment_text = 'This is a comment from @sliaquat'
Page::Project::Issue::Show.perform do |issue_page|
expect(issue_page).to have_comment(comment_text) expect(issue_page).to have_comment(comment_text)
expect(issue_page).to have_label('custom new label') expect(issue_page).to have_label('custom new label')
expect(issue_page).to have_label('help wanted') expect(issue_page).to have_label('help wanted')
...@@ -76,20 +79,21 @@ module QA ...@@ -76,20 +79,21 @@ module QA
def verify_merge_requests_import def verify_merge_requests_import
Page::Project::Menu.perform(&:click_merge_requests) Page::Project::Menu.perform(&:click_merge_requests)
expect(page).to have_content('Improve readme')
click_link 'Improve readme' Page::MergeRequest::Show.perform do |merge_request|
expect(merge_request).to have_content('Improve readme')
expect(page).to have_content('This improves the README file a bit.') click_link 'Improve readme'
# Comments expect(merge_request).to have_content('This improves the README file a bit.')
expect(page).to have_content('[PR comment by @sliaquat] Nice work!')
# Diff comments # Comments
expect(page).to have_content('[Single diff comment] Good riddance') expect(merge_request).to have_content('[PR comment by @sliaquat] Nice work!')
expect(page).to have_content('[Single diff comment] Nice addition')
# Diff comments
expect(merge_request).to have_content('[Single diff comment] Good riddance')
expect(merge_request).to have_content('[Single diff comment] Nice addition')
Page::MergeRequest::Show.perform do |merge_request|
expect(merge_request).to have_label('bug') expect(merge_request).to have_label('bug')
expect(merge_request).to have_label('documentation') expect(merge_request).to have_label('documentation')
end end
...@@ -108,7 +112,9 @@ module QA ...@@ -108,7 +112,9 @@ module QA
def verify_wiki_import def verify_wiki_import
Page::Project::Menu.perform(&:click_wiki) Page::Project::Menu.perform(&:click_wiki)
expect(page).to have_content('Welcome to the test-project wiki!') Page::Project::Wiki::Show.perform do |wiki|
expect(wiki).to have_content('Welcome to the test-project wiki!')
end
end end
end end
end end
......
...@@ -13,9 +13,11 @@ module QA ...@@ -13,9 +13,11 @@ module QA
end.project.visit! end.project.visit!
Page::Project::Menu.perform(&:click_activity) Page::Project::Menu.perform(&:click_activity)
Page::Project::Activity.perform(&:click_push_events) Page::Project::Activity.perform do |activity|
activity.click_push_events
expect(page).to have_content('pushed new branch master') expect(activity).to have_content('pushed new branch master')
end
end end
end end
end end
......
# frozen_string_literal: true # frozen_string_literal: true
module QA module QA
include HaveFileMatcher
RSpec.describe 'Create' do RSpec.describe 'Create' do
describe 'Files management' do describe 'Files management' do
it 'user creates, edits and deletes a file via the Web', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/451' do it 'user creates, edits and deletes a file via the Web', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/451' do
......
...@@ -35,8 +35,11 @@ module QA ...@@ -35,8 +35,11 @@ module QA
# Check that the target project has the commit from the source # Check that the target project has the commit from the source
target_project.visit! target_project.visit!
expect(page).to have_content('README.md')
expect(page).to have_content('This is a test project') Page::Project::Show.perform do |project|
expect(project).to have_content('README.md')
expect(project).to have_content('This is a test project')
end
end end
end end
end end
......
...@@ -19,6 +19,8 @@ Dir[::File.join(__dir__, "support/shared_contexts/*.rb")].sort.each { |f| requir ...@@ -19,6 +19,8 @@ Dir[::File.join(__dir__, "support/shared_contexts/*.rb")].sort.each { |f| requir
Dir[::File.join(__dir__, "support/shared_examples/*.rb")].sort.each { |f| require f } Dir[::File.join(__dir__, "support/shared_examples/*.rb")].sort.each { |f| require f }
RSpec.configure do |config| RSpec.configure do |config|
config.include ::Matchers
QA::Specs::Helpers::Quarantine.configure_rspec QA::Specs::Helpers::Quarantine.configure_rspec
config.before do |example| config.before do |example|
......
# frozen_string_literal: true # frozen_string_literal: true
module HaveFileMatcher module Matchers
RSpec::Matchers.define :have_file do |file| module HaveFile
match do |page_object| RSpec::Matchers.define :have_file do |file|
page_object.has_file?(file) match do |page_object|
end page_object.has_file?(file)
end
match_when_negated do |page_object| match_when_negated do |page_object|
page_object.has_no_file?(file) page_object.has_no_file?(file)
end
end end
end end
end end
# frozen_string_literal: true
module Matchers
class HaveText
def initialize(expected_text, **kwargs)
@expected_text = expected_text
@kwargs = kwargs
end
def matches?(actual)
@actual = wrap(actual)
@actual.has_text?(@expected_text, **@kwargs)
end
def does_not_match?(actual)
@actual = wrap(actual)
@actual.has_no_text?(@expected_text, **@kwargs)
end
def failure_message
"expected to find text \"#{@expected_text}\" in \"#{normalized_actual_text}\""
end
def failure_message_when_negated
"expected not to find text \"#{@expected_text}\" in \"#{normalized_actual_text}\""
end
def normalized_actual_text
@actual.text.gsub(/\s+/, " ")
end
# From https://github.com/teamcapybara/capybara/blob/fe5940c6afbfe32152df936ce03ad1371ae05354/lib/capybara/rspec/matchers/base.rb#L66
def wrap(actual)
actual = actual.to_capybara_node if actual.respond_to?(:to_capybara_node)
@context_el = if actual.respond_to?(:has_selector?)
actual
else
Capybara.string(actual.to_s)
end
end
end
def have_text(text, **kwargs) # rubocop:disable Naming/PredicateName
HaveText.new(text, **kwargs)
end
alias_method :have_content, :have_text
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment