Reputation: 199
In Capybara, How can I reuse existing browser to continue run step next. Instead of star a new session and open a new windown and next run a ton of step before.
For example : First:
session = Capybara::Session.new(:headless_chrome, Capybara.app)
session.open(url)
session.click(...)
sesion.quit()
Second Time:
session = ...#retry the first session state
sesion.do_something()
Upvotes: 1
Views: 565
Reputation: 49950
Capybara is designed as a testing tool, where you want every test to be completely independent of other tests. Because of that it has no support for storing/reloading a session, and uses a fresh browser instance (data-dir, etc) for each session. To prevent creation of a completely new browser setup every time you create a new session you could pass your own --user-data-dir=<some_directory>
option when registering your Capybara driver, but it's not Capybara supported usage (and would really not be a good idea if you're actually doing testing rather than web scraping). Another option if all you really want is the cookies retained would be something like https://github.com/kyamaguchi/capybara-sessionkeeper (again bad idea if you're testing since it couples tests to each other).
Upvotes: 2