How do I use my own cookies in capybara?
<p>I'm trying to (ab)use the capybara web testing framework to automate some tasks on github that are not accessible via the github API and which require me to be logged in and click on buttons to send AJAX requests.</p>

<p>Since capybara/selenium is a testing framework it helpfully creates a temporary session which has no cookies in it.  I'd like to either stop it from doing that, or else I'd like to know how to load my cookie store into the browser session that it creates.</p>

<p>All I'm trying to do is this:</p>

<pre><code>#!/usr/bin/env ruby

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://github.com"
</code></pre>

<p>Or this:</p>

<pre><code>#!/usr/bin/env ruby

require 'capybara'

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser =&gt; :chrome)
end

session = Capybara::Session.new(:selenium)
session.visit "https://www.github.com"
</code></pre>

<p>In both cases I get the github.com landing page you'd see as a logged-out user or incognito mode in the browser.  I'd like to get my logged-in landing page like I just fired up a web browser myself and navigated to that URL.</p>

<p>Since I have 2FA setup on github that makes automating the login process from the github landing page somewhat annoying, so I'd like to avoid automating logging into github.  The tasks that I want to automate do not require re-authenticating via 2FA.</p>

<p>ANSWER:</p>

<p>For MacOSX+Ruby+Selenium this works:</p>

<pre><code>#!/usr/bin/env ruby

require 'selenium-webdriver'

caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" =&gt; {"debuggerAddress" =&gt; "127.0.0.1:20480"}, detach: false)
driver = Selenium::WebDriver.for :chrome, :desired_capabilities =&gt; caps
driver.navigate.to "https://github.com"
</code></pre>

<p>Then fire up chrome with this:</p>

<pre><code>% /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/Users/lamont/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480
</code></pre>

<p>Obviously the paths will need to be adjusted because they're OSX-centric and have my homedir in them.</p>

<p>There is also a bug in the selenium-webdriver gem for ruby where it inserts a 'detach' option which gets into a fight with 'debuggerAddress':</p>

<pre><code>/Users/lamont/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok': unknown error: cannot parse capability: chromeOptions (Selenium::WebDriver::Error::UnknownError)
from unknown error: unrecognized chrome option: detach
</code></pre>

<p>The <code>lib/selenium/webdriver/chrome/bridge.rb</code> file can be edited to take that out as a quick hack:</p>

<pre><code>      chrome_options['binary']                   = Chrome.path if Chrome.path
      chrome_options['nativeEvents']             = true if native_events
      chrome_options['verbose']                  = true if verbose
      #chrome_options['detach']                   = detach.nil? || !!detach
      chrome_options['noWebsiteTestingDefaults'] = true if no_website_testing_defaults
      chrome_options['prefs']                    = prefs if prefs
</code></pre>