Add System test support in session test helper
Motivation / Background
The new SessionTestHelper from #53708 is accessible in system tests but does not work since there is no direct access to cookies.
Users will find this confusing, so this PR updates the method to also work for system tests.
Detail
This uses Lewis Buckley's approach for setting cookies directly in the browser. https://world.hey.com/lewis/faster-rails-system-tests-f01df53b
Additional information
There might be a cleaner way of doing this than introducing a conditional if anyone has improvements to suggest there.
The visit to a URL (in this case new_session_url from the generator) is required so the browser is on the correct domain. Chrome defaults to opening a tab at data:, which doesn't allow setting the cookie on the other domain. We can skip this if the browser is already loaded.
Checklist
Before submitting the PR make sure the following are checked:
- [x] This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
- [x] Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex:
[Fix #issue-number] - [ ] Tests are added or updated if you fix a bug or add a feature.
- [ ] CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
I was curious how Devise handles this.
Devise calls login_as, which is a Warden helper.
Warden adds a callback that logs the user in on the next request:
https://github.com/wardencommunity/warden/blob/67f5ba6baaa7564ec79afef02cf3a4d0f7d312e5/lib/warden/test/helpers.rb#L18-L23
This is a nice way to avoid the extra visit call (and avoid calling the driver and cookie_jar), but it's probably difficult to implement this in Rails.
With system tests being now removed by default, I think we should avoid adding this helper. Thanks