From 2206c1d1d3fa6fc4e5874b0f20c4c7eff649b9ae Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 26 Nov 2012 14:10:00 -0500 Subject: [PATCH] rearrange and set up readme --- README.md | 45 +++++++++++++++++++----------- lib/persistent_selenium/browser.rb | 18 ++++++++++++ lib/persistent_selenium/cli.rb | 13 ++++++--- lib/persistent_selenium/driver.rb | 2 +- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8d8e45e..ff36427 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,40 @@ -# PersistentSelenium +Now you can keep that precious browser window open when doing continuous integration testing. +Save seconds, and sanity, with every test re-run! -TODO: Write a gem description +Also, the browser stays open at its last state so you can inspect it and fix your tests and/or code. -## Installation +Start an instance: -Add this line to your application's Gemfile: +``` bash +persistent_selenium [ --port 9854 ] [ --browser firefox ] +``` - gem 'persistent_selenium' +Tell Capybara to use it: -And then execute: +``` +# features/support/env.rb - $ bundle +require 'persistent_selenium/driver' +Capybara.default_driver = :persistent_selenium +``` -Or install it yourself as: +Should work just the same as if you used the standard Capybara Selenium driver, except for +these two differences: - $ gem install persistent_selenium +* The browser starts up first thing and sticks around +* The last page you were on before your tests passed/failed stays there, so you can inspect it. -## Usage +The browser's cookies and such are reset before the next test runs, so you still get the state +cleared out before your next set of tests. -TODO: Write usage instructions here +### Under the hood -## Contributing +It's DRb, which Just Works (tm), and a little reshuffling of the default Capybara Selenium driver's code. -1. Fork it -2. Create your feature branch (`git checkout -b my-new-feature`) -3. Commit your changes (`git commit -am 'Added some feature'`) -4. Push to the branch (`git push origin my-new-feature`) -5. Create new Pull Request +Copyright © 2012 John Bintz + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/persistent_selenium/browser.rb b/lib/persistent_selenium/browser.rb index e1e5699..ada1beb 100644 --- a/lib/persistent_selenium/browser.rb +++ b/lib/persistent_selenium/browser.rb @@ -11,6 +11,10 @@ module PersistentSelenium @browser ||= Selenium::WebDriver.for(@browser_type) end + def options + {} + end + def visit(path) if !path[/^http/] path = @app_host + path @@ -21,6 +25,20 @@ module PersistentSelenium def set_app_host(host) @app_host = host + + browser.navigate.to('about:blank') + end + + def reset! + if @browser + begin + @browser.manage.delete_all_cookies + rescue Selenium::WebDriver::Error::UnhandledError => e + # delete_all_cookies fails when we've previously gone + # to about:blank, so we rescue this error and do nothing + # instead. + end + end end end end diff --git a/lib/persistent_selenium/cli.rb b/lib/persistent_selenium/cli.rb index 006141d..1cee11f 100644 --- a/lib/persistent_selenium/cli.rb +++ b/lib/persistent_selenium/cli.rb @@ -4,22 +4,27 @@ require 'persistent_selenium' module PersistentSelenium class CLI < Thor desc "start", "Start the server" + method_options :port => PersistentSelenium.port, :browser => PersistentSelenium.browser def start require 'persistent_selenium/browser' require 'drb' - port = ENV['PORT'] || PersistentSelenium.port - browser = ENV['BROWSER'] || PersistentSelenium.browser + PersistentSelenium.configure do |c| + c.port = options[:port] + c.browser = options[:browser] + end - puts "Starting persistent_selenium on #{port} with #{browser}" + puts "Starting persistent_selenium on #{PersistentSelenium.port} with #{PersistentSelenium.browser}" - browser = Browser.new(browser) + browser = Browser.new(PersistentSelenium.browser) browser.browser DRb.start_service PersistentSelenium.url, browser DRb.thread.join end + + default_task :start end end diff --git a/lib/persistent_selenium/driver.rb b/lib/persistent_selenium/driver.rb index 0395e90..17ae4b2 100644 --- a/lib/persistent_selenium/driver.rb +++ b/lib/persistent_selenium/driver.rb @@ -1,6 +1,6 @@ require 'persistent_selenium' -Capybara.register_driver :drb do |app| +Capybara.register_driver :persistent_selenium do |app| require 'drb' DRb.start_service