Commit 43377979 authored by Eric Eastwood's avatar Eric Eastwood
parent 96d8c7ff
---
title: Get true failure from evalulate_script by checking for element beforehand
merge_request: 14898
author:
type: fixed
...@@ -35,6 +35,8 @@ Here are some things to keep in mind regarding test performance: ...@@ -35,6 +35,8 @@ Here are some things to keep in mind regarding test performance:
[Gotchas](../gotchas.md#dont-assert-against-the-absolute-value-of-a-sequence-generated-attribute)). [Gotchas](../gotchas.md#dont-assert-against-the-absolute-value-of-a-sequence-generated-attribute)).
- Don't supply the `:each` argument to hooks since it's the default. - Don't supply the `:each` argument to hooks since it's the default.
- On `before` and `after` hooks, prefer it scoped to `:context` over `:all` - On `before` and `after` hooks, prefer it scoped to `:context` over `:all`
- When using `evaluate_script("$('.js-foo').testSomething()")` (or `execute_script`) which acts on a given element,
use a Capyabara matcher beforehand (e.g. `find('.js-foo')`) to ensure the element actually exists.
[four-phase-test]: https://robots.thoughtbot.com/four-phase-test [four-phase-test]: https://robots.thoughtbot.com/four-phase-test
......
...@@ -28,12 +28,6 @@ class Spinach::Features::ProjectIssuesFilterLabels < Spinach::FeatureSteps ...@@ -28,12 +28,6 @@ class Spinach::Features::ProjectIssuesFilterLabels < Spinach::FeatureSteps
end end
end end
step 'I click link "bug"' do
page.find('.js-label-select', visible: true).click
sleep 0.5
execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()")
end
step 'I click "dropdown close button"' do step 'I click "dropdown close button"' do
page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click
sleep 2 sleep 2
......
...@@ -46,10 +46,6 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps ...@@ -46,10 +46,6 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps
expect(page).to have_content new_gitignore_content expect(page).to have_content new_gitignore_content
end end
step 'I should see its content with new lines preserved at end of file' do
expect(evaluate_script('ace.edit("editor").getValue()')).to eq "Sample\n\n\n"
end
step 'I click link "Raw"' do step 'I click link "Raw"' do
click_link 'Open raw' click_link 'Open raw'
end end
...@@ -70,20 +66,11 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps ...@@ -70,20 +66,11 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps
click_link 'Fork' click_link 'Fork'
end end
step 'I can edit code' do
set_new_content
expect(evaluate_script('ace.edit("editor").getValue()')).to eq new_gitignore_content
end
step 'I edit code' do step 'I edit code' do
expect(page).to have_selector('.file-editor') expect(page).to have_selector('.file-editor')
set_new_content set_new_content
end end
step 'I edit code with new lines at end of file' do
execute_script('ace.edit("editor").setValue("Sample\n\n\n")')
end
step 'I fill the new file name' do step 'I fill the new file name' do
fill_in :file_name, with: new_file_name fill_in :file_name, with: new_file_name
end end
...@@ -395,6 +382,7 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps ...@@ -395,6 +382,7 @@ class Spinach::Features::ProjectSourceBrowseFiles < Spinach::FeatureSteps
private private
def set_new_content def set_new_content
find('#editor')
execute_script("ace.edit('editor').setValue('#{new_gitignore_content}')") execute_script("ace.edit('editor').setValue('#{new_gitignore_content}')")
end end
......
...@@ -171,12 +171,14 @@ describe 'Issue Boards', :js do ...@@ -171,12 +171,14 @@ describe 'Issue Boards', :js do
expect(page).to have_selector('.card', count: 20) expect(page).to have_selector('.card', count: 20)
expect(page).to have_content('Showing 20 of 58 issues') expect(page).to have_content('Showing 20 of 58 issues')
find('.board .board-list')
evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight") evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight")
wait_for_requests wait_for_requests
expect(page).to have_selector('.card', count: 40) expect(page).to have_selector('.card', count: 40)
expect(page).to have_content('Showing 40 of 58 issues') expect(page).to have_content('Showing 40 of 58 issues')
find('.board .board-list')
evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight") evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight")
wait_for_requests wait_for_requests
...@@ -449,11 +451,13 @@ describe 'Issue Boards', :js do ...@@ -449,11 +451,13 @@ describe 'Issue Boards', :js do
expect(page).to have_selector('.card', count: 20) expect(page).to have_selector('.card', count: 20)
expect(page).to have_content('Showing 20 of 51 issues') expect(page).to have_content('Showing 20 of 51 issues')
find('.board .board-list')
evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight") evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight")
expect(page).to have_selector('.card', count: 40) expect(page).to have_selector('.card', count: 40)
expect(page).to have_content('Showing 40 of 51 issues') expect(page).to have_content('Showing 40 of 51 issues')
find('.board .board-list')
evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight") evaluate_script("document.querySelectorAll('.board .board-list')[1].scrollTop = document.querySelectorAll('.board .board-list')[1].scrollHeight")
expect(page).to have_selector('.card', count: 51) expect(page).to have_selector('.card', count: 51)
......
...@@ -10,6 +10,7 @@ describe 'CI Lint', :js do ...@@ -10,6 +10,7 @@ describe 'CI Lint', :js do
visit ci_lint_path visit ci_lint_path
# Ace editor updates a hidden textarea and it happens asynchronously # Ace editor updates a hidden textarea and it happens asynchronously
# `sleep 0.1` is actually needed here because of this # `sleep 0.1` is actually needed here because of this
find('#ci-editor')
execute_script("ace.edit('ci-editor').setValue(" + yaml_content.to_json + ");") execute_script("ace.edit('ci-editor').setValue(" + yaml_content.to_json + ");")
sleep 0.1 sleep 0.1
click_on 'Validate' click_on 'Validate'
......
...@@ -87,8 +87,10 @@ RSpec.describe 'Dashboard Issues' do ...@@ -87,8 +87,10 @@ RSpec.describe 'Dashboard Issues' do
project_path = "/#{project.path_with_namespace}" project_path = "/#{project.path_with_namespace}"
project_json = { name: project.name_with_namespace, url: project_path }.to_json project_json = { name: project.name_with_namespace, url: project_path }.to_json
# similate selection, and prevent overlap by dropdown menu # simulate selection, and prevent overlap by dropdown menu
first('.project-item-select', visible: false)
execute_script("$('.project-item-select').val('#{project_json}').trigger('change');") execute_script("$('.project-item-select').val('#{project_json}').trigger('change');")
find('#select2-drop-mask', visible: false)
execute_script("$('#select2-drop-mask').remove();") execute_script("$('#select2-drop-mask').remove();")
find('.new-project-item-link').trigger('click') find('.new-project-item-link').trigger('click')
......
...@@ -16,6 +16,7 @@ feature 'Issue markdown toolbar', :js do ...@@ -16,6 +16,7 @@ feature 'Issue markdown toolbar', :js do
find('#note-body').native.send_key(:enter) find('#note-body').native.send_key(:enter)
find('#note-body').native.send_keys('bold') find('#note-body').native.send_keys('bold')
find('.js-main-target-form #note-body')
page.evaluate_script('document.querySelectorAll(".js-main-target-form #note-body")[0].setSelectionRange(4, 9)') page.evaluate_script('document.querySelectorAll(".js-main-target-form #note-body")[0].setSelectionRange(4, 9)')
first('.toolbar-btn').click first('.toolbar-btn').click
...@@ -28,6 +29,7 @@ feature 'Issue markdown toolbar', :js do ...@@ -28,6 +29,7 @@ feature 'Issue markdown toolbar', :js do
find('#note-body').native.send_key(:enter) find('#note-body').native.send_key(:enter)
find('#note-body').native.send_keys('underline') find('#note-body').native.send_keys('underline')
find('.js-main-target-form #note-body')
page.evaluate_script('document.querySelectorAll(".js-main-target-form #note-body")[0].setSelectionRange(4, 50)') page.evaluate_script('document.querySelectorAll(".js-main-target-form #note-body")[0].setSelectionRange(4, 50)')
find('.toolbar-btn:nth-child(2)').click find('.toolbar-btn:nth-child(2)').click
......
...@@ -60,12 +60,14 @@ feature 'Merge request conflict resolution', :js do ...@@ -60,12 +60,14 @@ feature 'Merge request conflict resolution', :js do
within find('.files-wrapper .diff-file', text: 'files/ruby/popen.rb') do within find('.files-wrapper .diff-file', text: 'files/ruby/popen.rb') do
click_button 'Edit inline' click_button 'Edit inline'
wait_for_requests wait_for_requests
find('.files-wrapper .diff-file pre')
execute_script('ace.edit($(".files-wrapper .diff-file pre")[0]).setValue("One morning");') execute_script('ace.edit($(".files-wrapper .diff-file pre")[0]).setValue("One morning");')
end end
within find('.files-wrapper .diff-file', text: 'files/ruby/regex.rb') do within find('.files-wrapper .diff-file', text: 'files/ruby/regex.rb') do
click_button 'Edit inline' click_button 'Edit inline'
wait_for_requests wait_for_requests
find('.files-wrapper .diff-file pre')
execute_script('ace.edit($(".files-wrapper .diff-file pre")[1]).setValue("Gregor Samsa woke from troubled dreams");') execute_script('ace.edit($(".files-wrapper .diff-file pre")[1]).setValue("Gregor Samsa woke from troubled dreams");')
end end
...@@ -139,6 +141,7 @@ feature 'Merge request conflict resolution', :js do ...@@ -139,6 +141,7 @@ feature 'Merge request conflict resolution', :js do
it 'conflicts are resolved in Edit inline mode' do it 'conflicts are resolved in Edit inline mode' do
within find('.files-wrapper .diff-file', text: 'files/markdown/ruby-style-guide.md') do within find('.files-wrapper .diff-file', text: 'files/markdown/ruby-style-guide.md') do
wait_for_requests wait_for_requests
find('.files-wrapper .diff-file pre')
execute_script('ace.edit($(".files-wrapper .diff-file pre")[0]).setValue("Gregor Samsa woke from troubled dreams");') execute_script('ace.edit($(".files-wrapper .diff-file pre")[0]).setValue("Gregor Samsa woke from troubled dreams");')
end end
......
...@@ -66,6 +66,7 @@ feature 'Edit Merge Request' do ...@@ -66,6 +66,7 @@ feature 'Edit Merge Request' do
end end
def get_textarea_height def get_textarea_height
find('#merge_request_description')
page.evaluate_script('document.getElementById("merge_request_description").offsetHeight') page.evaluate_script('document.getElementById("merge_request_description").offsetHeight')
end end
end end
......
...@@ -52,10 +52,12 @@ feature 'Mini Pipeline Graph', :js do ...@@ -52,10 +52,12 @@ feature 'Mini Pipeline Graph', :js do
end end
it 'should expand when hovered' do it 'should expand when hovered' do
find('.mini-pipeline-graph-dropdown-toggle')
before_width = evaluate_script("$('.mini-pipeline-graph-dropdown-toggle:visible').outerWidth();") before_width = evaluate_script("$('.mini-pipeline-graph-dropdown-toggle:visible').outerWidth();")
toggle.hover toggle.hover
find('.mini-pipeline-graph-dropdown-toggle')
after_width = evaluate_script("$('.mini-pipeline-graph-dropdown-toggle:visible').outerWidth();") after_width = evaluate_script("$('.mini-pipeline-graph-dropdown-toggle:visible').outerWidth();")
expect(before_width).to be < after_width expect(before_width).to be < after_width
......
...@@ -20,6 +20,7 @@ feature 'Editing file blob', :js do ...@@ -20,6 +20,7 @@ feature 'Editing file blob', :js do
def edit_and_commit def edit_and_commit
wait_for_requests wait_for_requests
find('.js-edit-blob').click find('.js-edit-blob').click
find('#editor')
execute_script('ace.edit("editor").setValue("class NextFeature\nend\n")') execute_script('ace.edit("editor").setValue("class NextFeature\nend\n")')
click_button 'Commit changes' click_button 'Commit changes'
end end
......
...@@ -60,6 +60,7 @@ describe 'User creates files' do ...@@ -60,6 +60,7 @@ describe 'User creates files' do
end end
it 'creates and commit a new file', :js do it 'creates and commit a new file', :js do
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:file_name, with: 'not_a_file.md') fill_in(:file_name, with: 'not_a_file.md')
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
...@@ -75,6 +76,7 @@ describe 'User creates files' do ...@@ -75,6 +76,7 @@ describe 'User creates files' do
end end
it 'creates and commit a new file with new lines at the end of file', :js do it 'creates and commit a new file with new lines at the end of file', :js do
find('#editor')
execute_script('ace.edit("editor").setValue("Sample\n\n\n")') execute_script('ace.edit("editor").setValue("Sample\n\n\n")')
fill_in(:file_name, with: 'not_a_file.md') fill_in(:file_name, with: 'not_a_file.md')
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
...@@ -86,6 +88,7 @@ describe 'User creates files' do ...@@ -86,6 +88,7 @@ describe 'User creates files' do
find('.js-edit-blob').click find('.js-edit-blob').click
find('#editor')
expect(evaluate_script('ace.edit("editor").getValue()')).to eq("Sample\n\n\n") expect(evaluate_script('ace.edit("editor").getValue()')).to eq("Sample\n\n\n")
end end
...@@ -94,6 +97,7 @@ describe 'User creates files' do ...@@ -94,6 +97,7 @@ describe 'User creates files' do
expect(page).to have_selector('.file-editor') expect(page).to have_selector('.file-editor')
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
click_button('Commit changes') click_button('Commit changes')
...@@ -108,6 +112,7 @@ describe 'User creates files' do ...@@ -108,6 +112,7 @@ describe 'User creates files' do
it 'creates and commit a new file specifying a new branch', :js do it 'creates and commit a new file specifying a new branch', :js do
expect(page).to have_selector('.file-editor') expect(page).to have_selector('.file-editor')
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:file_name, with: 'not_a_file.md') fill_in(:file_name, with: 'not_a_file.md')
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
...@@ -136,6 +141,7 @@ describe 'User creates files' do ...@@ -136,6 +141,7 @@ describe 'User creates files' do
expect(page).to have_selector('.file-editor') expect(page).to have_selector('.file-editor')
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:file_name, with: 'not_a_file.md') fill_in(:file_name, with: 'not_a_file.md')
......
...@@ -23,6 +23,7 @@ describe 'User edits files' do ...@@ -23,6 +23,7 @@ describe 'User edits files' do
find('.js-edit-blob').click find('.js-edit-blob').click
find('.file-editor', match: :first) find('.file-editor', match: :first)
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
expect(evaluate_script('ace.edit("editor").getValue()')).to eq('*.rbca') expect(evaluate_script('ace.edit("editor").getValue()')).to eq('*.rbca')
...@@ -40,6 +41,7 @@ describe 'User edits files' do ...@@ -40,6 +41,7 @@ describe 'User edits files' do
find('.js-edit-blob').click find('.js-edit-blob').click
find('.file-editor', match: :first) find('.file-editor', match: :first)
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
click_button('Commit changes') click_button('Commit changes')
...@@ -57,6 +59,7 @@ describe 'User edits files' do ...@@ -57,6 +59,7 @@ describe 'User edits files' do
find('.file-editor', match: :first) find('.file-editor', match: :first)
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
fill_in(:branch_name, with: 'new_branch_name', visible: true) fill_in(:branch_name, with: 'new_branch_name', visible: true)
...@@ -74,6 +77,7 @@ describe 'User edits files' do ...@@ -74,6 +77,7 @@ describe 'User edits files' do
find('.js-edit-blob').click find('.js-edit-blob').click
find('.file-editor', match: :first) find('.file-editor', match: :first)
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
click_link('Preview changes') click_link('Preview changes')
...@@ -103,6 +107,7 @@ describe 'User edits files' do ...@@ -103,6 +107,7 @@ describe 'User edits files' do
find('.file-editor', match: :first) find('.file-editor', match: :first)
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
expect(evaluate_script('ace.edit("editor").getValue()')).to eq('*.rbca') expect(evaluate_script('ace.edit("editor").getValue()')).to eq('*.rbca')
...@@ -119,6 +124,7 @@ describe 'User edits files' do ...@@ -119,6 +124,7 @@ describe 'User edits files' do
find('.file-editor', match: :first) find('.file-editor', match: :first)
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:commit_message, with: 'New commit message', visible: true) fill_in(:commit_message, with: 'New commit message', visible: true)
click_button('Commit changes') click_button('Commit changes')
...@@ -145,6 +151,7 @@ describe 'User edits files' do ...@@ -145,6 +151,7 @@ describe 'User edits files' do
expect(page).not_to have_link('Fork') expect(page).not_to have_link('Fork')
expect(page).not_to have_button('Cancel') expect(page).not_to have_button('Cancel')
find('#editor')
execute_script("ace.edit('editor').setValue('*.rbca')") execute_script("ace.edit('editor').setValue('*.rbca')")
fill_in(:commit_message, with: 'Another commit', visible: true) fill_in(:commit_message, with: 'Another commit', visible: true)
click_button('Commit changes') click_button('Commit changes')
......
...@@ -16,6 +16,7 @@ module Select2Helper ...@@ -16,6 +16,7 @@ module Select2Helper
selector = options.fetch(:from) selector = options.fetch(:from)
first(selector, visible: false)
if options[:multiple] if options[:multiple]
execute_script("$('#{selector}').select2('val', ['#{value}']).trigger('change');") execute_script("$('#{selector}').select2('val', ['#{value}']).trigger('change');")
else else
......
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