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
login_page.login('user1', 'user1pass')
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
......
......@@ -15,9 +15,9 @@ module QA
Page::Project::Menu.perform(&:click_members)
Page::Project::Members.perform do |members|
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
......
......@@ -11,12 +11,14 @@ module QA
project.description = 'create awesome project test'
end
expect(page).to have_content(created_project.name)
expect(page).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')
Page::Project::Show.perform do |project|
expect(project).to have_content(created_project.name)
expect(project).to have_content(
/Project \S?awesome-project\S+ was successfully created/
)
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
......
......@@ -49,23 +49,26 @@ module QA
end
def verify_repository_import
expect(page).to have_content('This test project is used for automated GitHub import by GitLab QA.')
expect(page).to have_content(imported_project.name)
Page::Project::Show.perform do |project|
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
def verify_issues_import
QA::Support::Retrier.retry_on_exception do
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
comment_text = 'This is a comment from @sliaquat'
expect(issue_page).to have_content('This is a sample first comment')
# 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_label('custom new label')
expect(issue_page).to have_label('help wanted')
......@@ -76,20 +79,21 @@ module QA
def verify_merge_requests_import
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(page).to have_content('[PR comment by @sliaquat] Nice work!')
expect(merge_request).to have_content('This improves the README file a bit.')
# Diff comments
expect(page).to have_content('[Single diff comment] Good riddance')
expect(page).to have_content('[Single diff comment] Nice addition')
# Comments
expect(merge_request).to have_content('[PR comment by @sliaquat] Nice work!')
# 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('documentation')
end
......@@ -108,7 +112,9 @@ module QA
def verify_wiki_import
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
......
......@@ -13,9 +13,11 @@ module QA
end.project.visit!
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
......
# frozen_string_literal: true
module QA
include HaveFileMatcher
RSpec.describe 'Create' 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
......
......@@ -35,8 +35,11 @@ module QA
# Check that the target project has the commit from the source
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
......
......@@ -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 }
RSpec.configure do |config|
config.include ::Matchers
QA::Specs::Helpers::Quarantine.configure_rspec
config.before do |example|
......
# frozen_string_literal: true
module HaveFileMatcher
RSpec::Matchers.define :have_file do |file|
match do |page_object|
page_object.has_file?(file)
end
module Matchers
module HaveFile
RSpec::Matchers.define :have_file do |file|
match do |page_object|
page_object.has_file?(file)
end
match_when_negated do |page_object|
page_object.has_no_file?(file)
match_when_negated do |page_object|
page_object.has_no_file?(file)
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