Skip to content

Commit 22ca875

Browse files
viktorianerlewispb
andauthored
Add docs about how to use remote browser in test (rails#44311)
* Add docs about how to use remote browser in test * Update guides/source/testing.md Co-authored-by: Lewis Buckley <[email protected]> Co-authored-by: Lewis Buckley <[email protected]>
1 parent f071897 commit 22ca875

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

guides/source/testing.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,55 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
861861
end
862862
```
863863

864+
If you want to use a remote browser, e.g.
865+
[Headless Chrome in Docker](https://github.com/SeleniumHQ/docker-selenium),
866+
you have to add remote `url` through `options`.
867+
868+
```ruby
869+
require "test_helper"
870+
871+
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
872+
options = ENV["SELENIUM_REMOTE_URL"].present? ? { url: ENV["SELENIUM_REMOTE_URL"] } : {}
873+
driven_by :selenium, using: :headless_chrome, options: options
874+
end
875+
```
876+
877+
In such a case, the gem `webdrivers` is no longer required. You could remove it
878+
completely or add `require:` option in `Gemfile`.
879+
880+
```ruby
881+
# ...
882+
group :test do
883+
gem "webdrivers", require: !ENV["SELENIUM_REMOTE_URL"] || ENV["SELENIUM_REMOTE_URL"].empty?
884+
end
885+
```
886+
887+
Now you should get a connection to remote browser.
888+
889+
```bash
890+
$ SELENIUM_REMOTE_URL=http://localhost:4444/wd/hub bin/rails test:system
891+
```
892+
893+
If your application in test is running remote too, e.g. Docker container,
894+
Capybara needs more input about how to
895+
[call remote servers](https://github.com/teamcapybara/capybara#calling-remote-servers).
896+
897+
```ruby
898+
require "test_helper"
899+
900+
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
901+
def setup
902+
Capybara.server_host = "0.0.0.0" # bind to all interfaces
903+
Capybara.app_host = "http://#{IPSocket.getaddress(Socket.gethostname)}" if ENV["SELENIUM_REMOTE_URL"].present?
904+
super
905+
end
906+
# ...
907+
end
908+
```
909+
910+
Now you should get a connection to remote browser and server, regardless if it
911+
is running in Docker container or CI.
912+
864913
If your Capybara configuration requires more setup than provided by Rails, this
865914
additional configuration could be added into the `application_system_test_case.rb`
866915
file.

0 commit comments

Comments
 (0)