1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Making "save and open page" file output path configurable to optionally avoid loads of capybara-*.html files in project root.

This commit is contained in:
Jonas Grimfelt 2010-06-27 02:36:25 +02:00
parent a94f9949d5
commit af1df52c1d
4 changed files with 65 additions and 20 deletions

3
.gitignore vendored
View file

@ -1,6 +1,7 @@
.idea/ .idea/
.DS_Store .DS_Store
pkg pkg
tmp
*~ *~
spec.opts spec.opts
capybara-*.html

View file

@ -11,15 +11,16 @@ module Capybara
class TimeoutError < CapybaraError; end class TimeoutError < CapybaraError; end
class LocateHiddenElementError < CapybaraError; end class LocateHiddenElementError < CapybaraError; end
class InfiniteRedirectError < TimeoutError; end class InfiniteRedirectError < TimeoutError; end
class << self class << self
attr_accessor :debug, :asset_root, :app_host, :run_server, :default_host attr_accessor :debug, :asset_root, :app_host, :run_server, :default_host
attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements
attr_accessor :save_and_open_page_path
def default_selector def default_selector
@default_selector ||= :xpath @default_selector ||= :xpath
end end
def default_wait_time def default_wait_time
@default_wait_time ||= 2 @default_wait_time ||= 2
end end
@ -29,14 +30,14 @@ module Capybara
true true
end end
end end
autoload :Server, 'capybara/server' autoload :Server, 'capybara/server'
autoload :Session, 'capybara/session' autoload :Session, 'capybara/session'
autoload :Node, 'capybara/node' autoload :Node, 'capybara/node'
autoload :XPath, 'capybara/xpath' autoload :XPath, 'capybara/xpath'
autoload :Searchable, 'capybara/searchable' autoload :Searchable, 'capybara/searchable'
autoload :VERSION, 'capybara/version' autoload :VERSION, 'capybara/version'
module Driver module Driver
autoload :Base, 'capybara/driver/base' autoload :Base, 'capybara/driver/base'
autoload :RackTest, 'capybara/driver/rack_test_driver' autoload :RackTest, 'capybara/driver/rack_test_driver'

View file

@ -3,8 +3,11 @@ module Capybara
extend(self) extend(self)
def save_and_open_page(html) def save_and_open_page(html)
name="capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}.html" name = File.join(*[Capybara.save_and_open_page_path, "capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}.html"].compact)
unless Capybara.save_and_open_page_path.blank? || File.directory?(Capybara.save_and_open_page_path )
FileUtils.mkdir_p(Capybara.save_and_open_page_path)
end
FileUtils.touch(name) unless File.exist?(name) FileUtils.touch(name) unless File.exist?(name)
tempfile = File.new(name,'w') tempfile = File.new(name,'w')

View file

@ -5,18 +5,12 @@ require 'launchy'
describe Capybara::SaveAndOpenPage do describe Capybara::SaveAndOpenPage do
describe "#save_save_and_open_page" do describe "#save_save_and_open_page" do
before do before do
@time = Time.new.strftime("%Y%m%d%H%M%S") @time = Time.new.strftime("%Y%m%d%H%M%S")
@name = "capybara-#{@time}.html"
@temp_file = mock("FILE") @temp_file = mock("FILE")
@temp_file.stub!(:write) @temp_file.stub!(:write)
@temp_file.stub!(:close) @temp_file.stub!(:close)
@temp_file.stub!(:path).and_return(@name)
File.should_receive(:exist?).and_return true
File.should_receive(:new).and_return @temp_file
@html = <<-HTML @html = <<-HTML
<html> <html>
<head> <head>
@ -30,14 +24,60 @@ describe Capybara::SaveAndOpenPage do
Launchy::Browser.stub(:run) Launchy::Browser.stub(:run)
end end
it "should create a new temporary file" do describe "defaults" do
@temp_file.should_receive(:write).with @html before do
Capybara::SaveAndOpenPage.save_and_open_page @html @name = "capybara-#{@time}.html"
end
@temp_file.stub!(:path).and_return(@name)
it "should open the file in the browser" do File.should_receive(:exist?).and_return true
Capybara::SaveAndOpenPage.should_receive(:open_in_browser).with(@name) File.should_receive(:new).and_return @temp_file
Capybara::SaveAndOpenPage.save_and_open_page @html end
it "should create a new temporary file" do
@temp_file.should_receive(:write).with @html
Capybara::SaveAndOpenPage.save_and_open_page @html
end
it "should open the file in the browser" do
Capybara::SaveAndOpenPage.should_receive(:open_in_browser).with(@name)
Capybara::SaveAndOpenPage.save_and_open_page @html
end
end
describe "custom output path" do
before do
@custom_path = File.join('tmp', 'capybara')
@custom_name = File.join(@custom_path, "capybara-#{@time}.html")
@temp_file.stub!(:path).and_return(@custom_name)
Capybara.should_receive(:save_and_open_page_path).at_least(:once).and_return(@custom_path)
end
it "should create a new temporary file in the custom path" do
File.should_receive(:directory?).and_return true
File.should_receive(:exist?).and_return true
File.should_receive(:new).and_return @temp_file
@temp_file.should_receive(:write).with @html
Capybara::SaveAndOpenPage.save_and_open_page @html
end
it "should open the file - in the custom path - in the browser" do
Capybara::SaveAndOpenPage.should_receive(:open_in_browser).with(@custom_name)
Capybara::SaveAndOpenPage.save_and_open_page @html
end
it "should be possible to configure output path" do
Capybara.should respond_to(:save_and_open_page_path)
default_setting = Capybara.save_and_open_page_path
lambda {
Capybara.save_and_open_page_path = File.join('tmp', 'capybara')
Capybara.save_and_open_page_path.should == File.join('tmp', 'capybara')
}.should_not raise_error
Capybara.save_and_open_page_path = default_setting
end
end end
end end
end end