From 77d636cc164eebeb119943f62b7ca1b837f878a8 Mon Sep 17 00:00:00 2001 From: Fatih Acet Date: Thu, 12 Oct 2017 21:29:32 +0000 Subject: [PATCH 1/7] Add debugging section to testing_guide/best_practices.md. --- doc/development/testing_guide/best_practices.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index 613423dbd9a..b3ce2133161 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -267,6 +267,20 @@ RSpec.configure do |config| end ``` +### Debugging + +If you need to debug Capybara tests, using the following lines, +you can get the current URL of Capybara server, logged in user email. Then you +can add some arbitrary sleep to halt the test and go check out the page. + +Default user password is `12345678`. + +``` +puts current_url +puts user.email +sleep(200) +``` + --- [Return to Testing documentation](index.md) From 88bd5fa274d0b9d9ccd8be26f83d422517880fe3 Mon Sep 17 00:00:00 2001 From: Clement Ho Date: Thu, 12 Oct 2017 21:38:52 +0000 Subject: [PATCH 2/7] Update best_practices.md --- doc/development/testing_guide/best_practices.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index b3ce2133161..386fe2a35eb 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -267,15 +267,20 @@ RSpec.configure do |config| end ``` -### Debugging +### Debugging Capybara -If you need to debug Capybara tests, using the following lines, -you can get the current URL of Capybara server, logged in user email. Then you -can add some arbitrary sleep to halt the test and go check out the page. +Sometimes you may need to debug Capybara tests by observing browser behavior. -Default user password is `12345678`. +You can stall capybara and view the website on the browser by adding a long sleep command in your spec and then opening your browser +to the capybara url. -``` +You can get the capybara url by doing `puts current_url`. +You can also get the user's email by doing `puts user.email`. + +The default password for the user is `12345678`. + +Example: +```ruby puts current_url puts user.email sleep(200) From 9bccea6e3438e26479e0f38e7833286daa10ed5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Mon, 16 Oct 2017 10:37:53 +0200 Subject: [PATCH 3/7] Add LiveDebugger#live_debug to debug Capybara in feature tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- .../testing_guide/best_practices.md | 49 ++++++++++++------- spec/spec_helper.rb | 1 + spec/support/live_debugger.rb | 21 ++++++++ spec/support/login_helpers.rb | 10 +++- 4 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 spec/support/live_debugger.rb diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index 386fe2a35eb..d07a368abfd 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -58,6 +58,36 @@ writing one](testing_levels.md#consider-not-writing-a-system-test)! - It's ok to look for DOM elements but don't abuse it since it makes the tests more brittle +#### Debugging Capybara + +Sometimes you may need to debug Capybara tests by observing browser behavior. + +You can stall Capybara and view the website on the browser by using the +`live_debug` method in your spec. The current page will be automatically opened +in your default browser. +You may need to sign-in first (the current user's credentials are displayed in +the terminal). + +To resume the test run, you only need to press `c`. + +For example: + +```ruby +$ bin/rspec spec/features/auto_deploy_spec.rb:34 +Running via Spring preloader in process 8999 +Run options: include {:locations=>{"./spec/features/auto_deploy_spec.rb"=>[34]}} + +Current example is paused for live debugging +The current user credentials are: user2 / 12345678 +Press 'c' to continue the execution of the example +Please press 'c' to continue the execution of the example! ;) <- I pressed `d` here +Back to the example! +. + +Finished in 34.51 seconds (files took 0.76702 seconds to load) +1 example, 0 failures +``` + ### `let` variables GitLab's RSpec suite has made extensive use of `let` variables to reduce @@ -267,25 +297,6 @@ RSpec.configure do |config| end ``` -### Debugging Capybara - -Sometimes you may need to debug Capybara tests by observing browser behavior. - -You can stall capybara and view the website on the browser by adding a long sleep command in your spec and then opening your browser -to the capybara url. - -You can get the capybara url by doing `puts current_url`. -You can also get the user's email by doing `puts user.email`. - -The default password for the user is `12345678`. - -Example: -```ruby -puts current_url -puts user.email -sleep(200) -``` - --- [Return to Testing documentation](index.md) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 48cacba6a8a..0dc417b3cb6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -49,6 +49,7 @@ RSpec.configure do |config| config.include LoginHelpers, type: :feature config.include SearchHelpers, type: :feature config.include WaitForRequests, :js + config.include LiveDebugger, :js config.include StubConfiguration config.include EmailHelpers, :mailer, type: :mailer config.include TestEnv diff --git a/spec/support/live_debugger.rb b/spec/support/live_debugger.rb new file mode 100644 index 00000000000..b15aba01e68 --- /dev/null +++ b/spec/support/live_debugger.rb @@ -0,0 +1,21 @@ +require 'io/console' + +module LiveDebugger + def live_debug + `open #{current_url}` + + puts "\nCurrent example is paused for live debugging" + puts "The current user credentials are: #{@current_user.username} / 12345678" if @current_user + puts "Press 'c' to continue the execution of the example" + + loop do + if $stdin.getch == 'c' + break + else + puts "Please press 'c' to continue the execution of the example! ;)" + end + end + + puts "Back to the example!" + end +end diff --git a/spec/support/login_helpers.rb b/spec/support/login_helpers.rb index 3e117530151..2ca05bb1778 100644 --- a/spec/support/login_helpers.rb +++ b/spec/support/login_helpers.rb @@ -3,6 +3,14 @@ require_relative 'devise_helpers' module LoginHelpers include DeviseHelpers + # Overriding Devise::Test::IntegrationHelpers#sign_in to store @current_user + # since we may need it in LiveDebugger#live_debug. + def sign_in(resource, scope: nil) + super + + @current_user = resource + end + # Internal: Log in as a specific user or a new user of a specific role # # user_or_role - User object, or a role to create (e.g., :admin, :user) @@ -28,7 +36,7 @@ module LoginHelpers gitlab_sign_in_with(user, **kwargs) - user + @current_user = user end def gitlab_sign_in_via(provider, user, uid) From a3368a988d9ed90dfe67b34017782d45b5897574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 17 Oct 2017 17:42:21 +0200 Subject: [PATCH 4/7] Improve the LiveDebugger exit handler and documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- .../testing_guide/best_practices.md | 4 ++-- spec/support/live_debugger.rb | 22 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index d07a368abfd..12e6ec169e0 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -62,10 +62,10 @@ writing one](testing_levels.md#consider-not-writing-a-system-test)! Sometimes you may need to debug Capybara tests by observing browser behavior. -You can stall Capybara and view the website on the browser by using the +You can pause Capybara and view the website on the browser by using the `live_debug` method in your spec. The current page will be automatically opened in your default browser. -You may need to sign-in first (the current user's credentials are displayed in +You may need to sign in first (the current user's credentials are displayed in the terminal). To resume the test run, you only need to press `c`. diff --git a/spec/support/live_debugger.rb b/spec/support/live_debugger.rb index b15aba01e68..9c0570bcbf7 100644 --- a/spec/support/live_debugger.rb +++ b/spec/support/live_debugger.rb @@ -2,20 +2,24 @@ require 'io/console' module LiveDebugger def live_debug + puts "\nCurrent example is paused for live debugging." + puts "Opening #{current_url} in your default browser..." + puts "The current user credentials are: #{@current_user.username} / 12345678" if @current_user + puts "Press '^C' to continue the execution of the example" + `open #{current_url}` - puts "\nCurrent example is paused for live debugging" - puts "The current user credentials are: #{@current_user.username} / 12345678" if @current_user - puts "Press 'c' to continue the execution of the example" - - loop do - if $stdin.getch == 'c' - break - else - puts "Please press 'c' to continue the execution of the example! ;)" + catch :unpause_test do + trap('INT') { throw :unpause_test } + loop do + sleep(1) end end + # If the command is 'DEFAULT', the Ruby's default handler will be invoked. + # http://docs.rubydocs.org/rails-4-2-8-ruby-2-3-4/Ruby%202.3.4/classes/Kernel.html#method-i-trap + trap('INT') { 'DEFAULT' } + puts "Back to the example!" end end From 73e3d6b8a0594ff081103ce1bbb7cb8370503975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 17 Oct 2017 17:42:43 +0200 Subject: [PATCH 5/7] Ensure no exception is raised if the Gitaly process is already gone in TestEnv.cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/test_env.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/support/test_env.rb b/spec/support/test_env.rb index a27bfdee3d2..fff120fcb88 100644 --- a/spec/support/test_env.rb +++ b/spec/support/test_env.rb @@ -182,6 +182,8 @@ module TestEnv return unless @gitaly_pid Process.kill('KILL', @gitaly_pid) + rescue Errno::ESRCH + # The process can already be gone if the test run was INTerrupted. end def setup_factory_repo From 1c17ddba662872bf6ad9a9fe04137023476df50c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 26 Oct 2017 18:03:33 +0200 Subject: [PATCH 6/7] Simplify the live debugger resume mechanism: press any key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- doc/development/testing_guide/best_practices.md | 5 ++--- spec/support/live_debugger.rb | 13 ++----------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index 12e6ec169e0..bbdeee05579 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -68,7 +68,7 @@ in your default browser. You may need to sign in first (the current user's credentials are displayed in the terminal). -To resume the test run, you only need to press `c`. +To resume the test run, press any key. For example: @@ -79,8 +79,7 @@ Run options: include {:locations=>{"./spec/features/auto_deploy_spec.rb"=>[34]}} Current example is paused for live debugging The current user credentials are: user2 / 12345678 -Press 'c' to continue the execution of the example -Please press 'c' to continue the execution of the example! ;) <- I pressed `d` here +Press any key to resume the execution of the example! Back to the example! . diff --git a/spec/support/live_debugger.rb b/spec/support/live_debugger.rb index 9c0570bcbf7..8359a81059b 100644 --- a/spec/support/live_debugger.rb +++ b/spec/support/live_debugger.rb @@ -5,20 +5,11 @@ module LiveDebugger puts "\nCurrent example is paused for live debugging." puts "Opening #{current_url} in your default browser..." puts "The current user credentials are: #{@current_user.username} / 12345678" if @current_user - puts "Press '^C' to continue the execution of the example" + puts "Press any key to resume the execution of the example!!" `open #{current_url}` - catch :unpause_test do - trap('INT') { throw :unpause_test } - loop do - sleep(1) - end - end - - # If the command is 'DEFAULT', the Ruby's default handler will be invoked. - # http://docs.rubydocs.org/rails-4-2-8-ruby-2-3-4/Ruby%202.3.4/classes/Kernel.html#method-i-trap - trap('INT') { 'DEFAULT' } + loop until $stdin.getch puts "Back to the example!" end From 0364e074479ac8204365fa6b6109011042431575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 2 Nov 2017 12:02:51 +0100 Subject: [PATCH 7/7] Address Douwe's feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- doc/development/testing_guide/best_practices.md | 2 +- spec/support/live_debugger.rb | 5 +++-- spec/support/login_helpers.rb | 8 ++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index bbdeee05579..a165a17f5db 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -72,7 +72,7 @@ To resume the test run, press any key. For example: -```ruby +``` $ bin/rspec spec/features/auto_deploy_spec.rb:34 Running via Spring preloader in process 8999 Run options: include {:locations=>{"./spec/features/auto_deploy_spec.rb"=>[34]}} diff --git a/spec/support/live_debugger.rb b/spec/support/live_debugger.rb index 8359a81059b..911eb48a8ca 100644 --- a/spec/support/live_debugger.rb +++ b/spec/support/live_debugger.rb @@ -2,9 +2,10 @@ require 'io/console' module LiveDebugger def live_debug - puts "\nCurrent example is paused for live debugging." + puts + puts "Current example is paused for live debugging." puts "Opening #{current_url} in your default browser..." - puts "The current user credentials are: #{@current_user.username} / 12345678" if @current_user + puts "The current user credentials are: #{@current_user.username} / #{@current_user.password}" if @current_user puts "Press any key to resume the execution of the example!!" `open #{current_url}` diff --git a/spec/support/login_helpers.rb b/spec/support/login_helpers.rb index 2ca05bb1778..de46d2ebf29 100644 --- a/spec/support/login_helpers.rb +++ b/spec/support/login_helpers.rb @@ -11,6 +11,13 @@ module LoginHelpers @current_user = resource end + # Overriding Devise::Test::IntegrationHelpers#sign_out to clear @current_user. + def sign_out(resource_or_scope) + super + + @current_user = nil + end + # Internal: Log in as a specific user or a new user of a specific role # # user_or_role - User object, or a role to create (e.g., :admin, :user) @@ -49,6 +56,7 @@ module LoginHelpers def gitlab_sign_out find(".header-user-dropdown-toggle").click click_link "Sign out" + @current_user = nil expect(page).to have_button('Sign in') end