Skip to content

Commit

Permalink
fix(rspec): fix flaky specs
Browse files Browse the repository at this point in the history
- wait for animations to complete with wait_for_animation
- optimize wait for forbidden page to load (remove sleep 1)
- flaky forum disbursement due to 12h/24h datetime format inconsistency
- skip dismissing toast unless necessary (when texts are the same in same scenario)
- optimize page load with redirect on login
  • Loading branch information
cysjonathan committed Nov 28, 2024
1 parent a8b9f88 commit 70932f2
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 83 deletions.
4 changes: 2 additions & 2 deletions spec/features/course/admin/admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@

click_button 'Save changes'

expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(course.reload.advance_start_at_duration).to be_within(1.hour).of(days.days)

fill_in advance_start_at_duration_field, with: ''
click_button 'Save changes'

expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(course.reload.advance_start_at_duration).to eq 0
end

Expand Down
8 changes: 4 additions & 4 deletions spec/features/course/admin/codaveri_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
itsp_engine_radio_button = find('label', text: 'ITSP Engine')
itsp_engine_radio_button.click
click_button 'Save changes'
expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(course.reload.codaveri_itsp_enabled?).to eq(true)

default_engine_radio_button = find('label', text: 'Default Engine')
default_engine_radio_button.click
click_button 'Save changes'
expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(course.reload.codaveri_itsp_enabled?).to eq(false)
end

Expand All @@ -33,12 +33,12 @@

find('label', text: 'Publish feedback directly to student').click
click_button 'Save changes'
expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(course.reload.codaveri_feedback_workflow).to eq('publish')

find('label', text: 'Generate no feedback').click
click_button 'Save changes'
expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(course.reload.codaveri_feedback_workflow).to eq('none')
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/features/course/admin/component_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

control = find('label', text: sample_component.display_name)
control.click
expect_toastify('Your changes have been saved. Refresh to see the new changes.')
expect_toastify('Your changes have been saved. Refresh to see the new changes.', dismiss: true)

expect(control).to have_field(type: 'checkbox', checked: false, visible: false)

control.click
expect_toastify('Your changes have been saved. Refresh to see the new changes.')
expect_toastify('Your changes have been saved. Refresh to see the new changes.', dismiss: true)

expect(control).to have_field(type: 'checkbox', checked: true, visible: false)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/features/course/admin/forum_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@
find_field('allowAnonymousPost', visible: false).set(true)

click_button 'Save changes'
expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(page).to have_field('allowAnonymousPost', checked: true, visible: false)
expect(course.reload.settings(:course_forums_component).allow_anonymous_post).to be_truthy

find_field('allowAnonymousPost', visible: false).set(false)
click_button 'Save changes'
expect_toastify('Your changes have been saved.')
expect_toastify('Your changes have been saved.', dismiss: true)
expect(page).to have_field('allowAnonymousPost', checked: false, visible: false)
expect(course.reload.settings(:course_forums_component).allow_anonymous_post).to be_falsy
end
Expand Down
18 changes: 11 additions & 7 deletions spec/features/course/assessment/skill_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
visit course_assessments_skills_path(course)
click_button 'Skill'

# ensure form is present
find('form#skill-form')
skill_attributes = attributes_for(:course_assessment_skill)
fill_in 'title', with: skill_attributes[:title]

Expand All @@ -33,13 +35,16 @@
visit course_assessments_skills_path(course)
find('.skill_branch#skill_branch_-1').click
find(content_tag_selector(skill)).click
wait_for_animation
find("button.skill-edit-#{skill.id}").click

# ensure form is present
find('form#skill-form')
new_skill_title = "#{skill.title} there!"
fill_in 'title', with: new_skill_title
# Does not work because it is not visible
find('#select').click
find("#select-#{skill_branch.id}").click
find('form#skill-form #select').click
wait_for_animation
find("#menu-skillBranchId li#select-#{skill_branch.id}").click

find('.btn-submit').click

Expand All @@ -51,16 +56,15 @@
expect(skill.skill_branch).to eq(skill_branch)
end

# Flaky test
xscenario 'I can delete a skill' do
scenario 'I can delete a skill' do
skill = create(:course_assessment_skill, course: course)
visit course_assessments_skills_path(course)
find('.skill_branch#skill_branch_-1').click
find(content_tag_selector(skill)).click
wait_for_page
wait_for_animation
expect do
find("button.skill-delete-#{skill.id}").click
accept_confirm_dialog
accept_confirm_dialog('button.prompt-primary-btn')
end.to change { course.assessment_skills.count }.by(-1)

expect(current_path).to eq(course_assessments_skills_path(course))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
expect(page).to have_selector('span', text: 'Past Answers', count: past_answer_count)

all('span', text: 'Past Answers').each(&:click)
wait_for_animation
# Label selector matches both the expanded 'Past Answers' section and the labels we clicked
expect(page).to have_selector('label', text: 'Past Answers', count: past_answer_count * 2)
end
Expand All @@ -47,6 +48,7 @@
expect(page).to have_selector('span', text: 'Past Answers', count: past_answer_count)

all('span', text: 'Past Answers').each(&:click)
wait_for_animation
expect(page).to have_selector('label', text: 'Past Answers', count: past_answer_count * 2)
end
end
Expand Down
20 changes: 7 additions & 13 deletions spec/features/course/assessment_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,44 @@

with_tenant(:instance) do
let(:course) { create(:course) }
before { login_as(user, scope: :user) }
before { login_as(user, scope: :user, redirect_url: course_assessments_path(course)) }

context 'As a Course Manager' do
let(:user) { create(:course_manager, course: course).user }

# This test is disabled as CircleCI is unable to detect the following:
# first("input[name='start_at']").click.set(assessment.start_at.strftime('%d-%m-%Y'))
xscenario 'I can create an assessment' do
scenario 'I can create an assessment' do
assessment_tab = create(:course_assessment_tab,
category: course.assessment_categories.first)
assessment = build_stubbed(:assessment)

visit course_assessments_path(course, category: assessment_tab.category,
tab: assessment_tab)
find('div.new-btn button').click
find('button[aria-label="New Assessment"]').click

expect(page).to have_selector('h2', text: 'New Assessment')

fill_in 'title', with: assessment.title
fill_in 'base_exp', with: assessment.base_exp
# Need to click on the field first or `set` won't work.
first("input[name='start_at']").click.set(assessment.start_at.strftime('%d-%m-%Y'))
start_at = assessment.start_at.strftime('%d-%m-%Y %H:%M')
fill_in_mui_datetime('start_at', start_at)
find('button.btn-submit').click

expect(page).not_to have_selector('h2', text: 'New Assessment')
assessment_created = course.assessments.last
expect(assessment_created.tab).to eq(assessment_tab)
expect(assessment_created.title).to eq(assessment.title)
expect(assessment_created.base_exp).to eq(assessment.base_exp)
expect(page).to have_content_tag_for(assessment_created)
expect(assessment_created).not_to be_autograded
expect(page).to have_text(assessment_created.title)
end

scenario 'I can delete an assessment' do
skip 'Flaky tests'
assessment = create(:assessment, course: course)
category_id, tab_id = assessment.tab.category_id, assessment.tab_id
visit course_assessment_path(course, assessment)

expect do
wait_for_page
click_button 'Delete Assessment'
find('svg[data-testid="DeleteIcon"]').click
Capybara.enable_aria_label = false
click_button 'Delete Assessment'
expect_toastify('Assessment successfully deleted.')
Expand All @@ -62,8 +58,6 @@
let(:user) { create(:course_student, course: course).user }

scenario 'I can view the Assessment Sidebar item' do
visit course_path(course)

assessment_sidebar = 'activerecord.attributes.course/assessment/category/title.default'
expect(find_sidebar).to have_text(assessment_sidebar)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
expect(page).to have_field(type: 'text', with: '100')
end

start_date = (4.weeks.ago + 1.minute).strftime('%d-%m-%Y %I:%M')
end_date = 2.weeks.ago.strftime('%d-%m-%Y %I:%M')
start_date = (4.weeks.ago + 1.minute).strftime('%d-%m-%Y %H:%M')
end_date = 2.weeks.ago.strftime('%d-%m-%Y %H:%M')

fill_in_mui_datetime('Start Date', start_date)
fill_in_mui_datetime('End Date', end_date)
Expand Down
8 changes: 3 additions & 5 deletions spec/features/system/admin/components_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
let(:components) { instance.disableable_components }

before do
login_as(admin, scope: :user)
login_as(admin, scope: :user, redirect_url: admin_instance_components_path)
end

scenario 'Admin visits the page' do
visit admin_instance_components_path
settings = Instance::Settings::Components.new(instance)
enabled_components = settings.enabled_component_ids

Expand All @@ -31,18 +30,17 @@
end

scenario 'Enable/disable a component' do
visit admin_instance_components_path
settings = Instance::Settings::Components.new(instance)
enabled_components = settings.enabled_component_ids
component_to_modify = enabled_components.sample
element_to_modify = find("tr#component_#{component_to_modify}")

element_to_modify.find('input', visible: false).click
expect_toastify('Instance component setting was updated successfully.')
expect_toastify('Instance component setting was updated successfully.', dismiss: true)
expect(element_to_modify).to have_field(type: 'checkbox', checked: false, visible: false)

element_to_modify.find('input', visible: false).click
expect_toastify('Instance component setting was updated successfully.')
expect_toastify('Instance component setting was updated successfully.', dismiss: true)
expect(element_to_modify).to have_field(type: 'checkbox', checked: true, visible: false)
end
end
Expand Down
20 changes: 6 additions & 14 deletions spec/features/system/admin/instance/user_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ def search_for_users(query, click: true)
find('button[aria-label="Search"]').click if click
find('input[type="text"]').set('').native.send_keys(query)
end
wait_for_field_debouncing
end

context 'As a Instance Administrator' do
before { login_as(instance_admin, scope: :user) }
before { login_as(instance_admin, scope: :user, redirect_url: admin_instance_users_path) }

scenario 'I can view all users in the instance' do
visit admin_instance_users_path

search_for_users(prefix)
instance_users.each do |instance_user|
expect(page).to have_text(instance_user.user.name)
Expand All @@ -37,8 +36,6 @@ def search_for_users(query, click: true)
end

scenario 'I can filter users by role and view only administrators' do
visit admin_instance_users_path

within find('p', text: 'Total Users', exact_text: false) do
find_all('a').first.click
end
Expand All @@ -53,8 +50,6 @@ def search_for_users(query, click: true)
end

scenario "I can change a user's role" do
visit admin_instance_users_path

user_to_change = instance_users.sample
search_for_users(user_to_change.user.name)

Expand All @@ -68,8 +63,6 @@ def search_for_users(query, click: true)
end

scenario 'I can delete a user' do
visit admin_instance_users_path

search_for_users(prefix)
user_to_delete = instance_users.sample
find("button.user-delete-#{user_to_delete.id}").click
Expand All @@ -78,25 +71,24 @@ def search_for_users(query, click: true)
end

# Generate new users to search so it doesn't conflict with above scenarios
scenario 'I can search users' do
scenario 'I can search users by name' do
search_prefix = "testadm-search-#{rand(36**12).to_s(36)}-usr-"
instance_users_to_search = (1..2).map do |i|
create(:instance_user, user_name: "#{search_prefix}#{i}")
end

visit admin_instance_users_path

# Search by username
search_for_users(search_prefix)

instance_users_to_search.each do |instance_user|
expect(page).to have_text(instance_user.user.name)
end
expect(page).to have_selector('.instance_user', count: 2)
end

# Search by email
scenario 'I can search users by email' do
random_instance_user = InstanceUser.order('RANDOM()').first
search_for_users(random_instance_user.user.email, click: false)
search_for_users(random_instance_user.user.email)

expect(page).to have_text(random_instance_user.user.name)
expect(page).to have_selector('.instance_user', count: 1)
Expand Down
Loading

0 comments on commit 70932f2

Please sign in to comment.