Commit 9bccea6e authored by Rémy Coutable's avatar Rémy Coutable

Add LiveDebugger#live_debug to debug Capybara in feature tests.

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 88bd5fa2
...@@ -58,6 +58,36 @@ writing one](testing_levels.md#consider-not-writing-a-system-test)! ...@@ -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 - It's ok to look for DOM elements but don't abuse it since it makes the tests
more brittle 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 ### `let` variables
GitLab's RSpec suite has made extensive use of `let` variables to reduce GitLab's RSpec suite has made extensive use of `let` variables to reduce
...@@ -267,25 +297,6 @@ RSpec.configure do |config| ...@@ -267,25 +297,6 @@ RSpec.configure do |config|
end 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) [Return to Testing documentation](index.md)
...@@ -49,6 +49,7 @@ RSpec.configure do |config| ...@@ -49,6 +49,7 @@ RSpec.configure do |config|
config.include LoginHelpers, type: :feature config.include LoginHelpers, type: :feature
config.include SearchHelpers, type: :feature config.include SearchHelpers, type: :feature
config.include WaitForRequests, :js config.include WaitForRequests, :js
config.include LiveDebugger, :js
config.include StubConfiguration config.include StubConfiguration
config.include EmailHelpers, :mailer, type: :mailer config.include EmailHelpers, :mailer, type: :mailer
config.include TestEnv config.include TestEnv
......
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
...@@ -3,6 +3,14 @@ require_relative 'devise_helpers' ...@@ -3,6 +3,14 @@ require_relative 'devise_helpers'
module LoginHelpers module LoginHelpers
include DeviseHelpers 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 # 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) # user_or_role - User object, or a role to create (e.g., :admin, :user)
...@@ -28,7 +36,7 @@ module LoginHelpers ...@@ -28,7 +36,7 @@ module LoginHelpers
gitlab_sign_in_with(user, **kwargs) gitlab_sign_in_with(user, **kwargs)
user @current_user = user
end end
def gitlab_sign_in_via(provider, user, uid) def gitlab_sign_in_via(provider, user, uid)
......
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