diff --git a/spec/driver_rendering_spec.rb b/spec/driver_rendering_spec.rb index 3cb8a19..c9ff9eb 100644 --- a/spec/driver_rendering_spec.rb +++ b/spec/driver_rendering_spec.rb @@ -3,14 +3,10 @@ require 'capybara/webkit/driver' require 'mini_magick' describe Capybara::Webkit::Driver, "rendering an image" do + include AppRunner - before(:all) do - # Set up the tmp directory and file name - tmp_dir = File.join(PROJECT_ROOT, 'tmp') - FileUtils.mkdir_p tmp_dir - @file_name = File.join(tmp_dir, 'render-test.png') - - app = lambda do |env| + let(:driver) do + driver_for_app do |env| body = <<-HTML
@@ -22,22 +18,25 @@ describe Capybara::Webkit::Driver, "rendering an image" do { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]] end - - @driver = Capybara::Webkit::Driver.new(app, :browser => $webkit_browser) - @driver.visit("/hello/world?success=true") end - after(:all) { @driver.reset! } + before(:each) do + # Set up the tmp directory and file name + tmp_dir = File.join(PROJECT_ROOT, 'tmp') + FileUtils.mkdir_p tmp_dir + @file_name = File.join(tmp_dir, 'render-test.png') + driver.visit '/' + end def render(options) FileUtils.rm_f @file_name - @driver.render @file_name, options + driver.render @file_name, options @image = MiniMagick::Image.open @file_name end context "with default options" do - before(:all){ render({}) } + before { render({}) } it "should be a PNG" do @image[:format].should == "PNG" @@ -54,7 +53,7 @@ describe Capybara::Webkit::Driver, "rendering an image" do end context "with dimensions set larger than necessary" do - before(:all){ render(:width => 500, :height => 400) } + before { render(:width => 500, :height => 400) } it "width should match the width given" do @image[:width].should == 500 @@ -66,7 +65,7 @@ describe Capybara::Webkit::Driver, "rendering an image" do end context "with dimensions set smaller than the document's default" do - before(:all){ render(:width => 50, :height => 10) } + before { render(:width => 50, :height => 10) } it "width should be greater than the width given" do @image[:width].should > 50 @@ -76,5 +75,4 @@ describe Capybara::Webkit::Driver, "rendering an image" do @image[:height].should > 10 end end - end diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 42d70ab..4167697 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -3,15 +3,11 @@ require 'capybara/webkit/driver' require 'base64' describe Capybara::Webkit::Driver do - subject { Capybara::Webkit::Driver.new(@app, :browser => $webkit_browser) } - before do - subject.reset! - subject.visit("/hello/world?success=true") - end + include AppRunner context "iframe app" do - before(:all) do - @app = lambda do |env| + let(:driver) do + driver_for_app do |env| params = ::Rack::Utils.parse_query(env['QUERY_STRING']) if params["iframe"] == "true" # We are in an iframe request. @@ -45,71 +41,74 @@ describe Capybara::Webkit::Driver do end end + before do + driver.visit("/") + end + it "finds frames by index" do - subject.within_frame(0) do - subject.find("//*[contains(., 'goodbye')]").should_not be_empty + driver.within_frame(0) do + driver.find("//*[contains(., 'goodbye')]").should_not be_empty end end it "finds frames by id" do - subject.within_frame("f") do - subject.find("//*[contains(., 'goodbye')]").should_not be_empty + driver.within_frame("f") do + driver.find("//*[contains(., 'goodbye')]").should_not be_empty end end it "raises error for missing frame by index" do - expect { subject.within_frame(1) { } }. + expect { driver.within_frame(1) { } }. to raise_error(Capybara::Webkit::InvalidResponseError) end it "raise_error for missing frame by id" do - expect { subject.within_frame("foo") { } }. + expect { driver.within_frame("foo") { } }. to raise_error(Capybara::Webkit::InvalidResponseError) end it "returns an attribute's value" do - subject.within_frame("f") do - subject.find("//p").first["id"].should == "farewell" + driver.within_frame("f") do + driver.find("//p").first["id"].should == "farewell" end end it "returns a node's text" do - subject.within_frame("f") do - subject.find("//p").first.text.should == "goodbye" + driver.within_frame("f") do + driver.find("//p").first.text.should == "goodbye" end end it "returns the current URL" do - subject.within_frame("f") do - port = subject.instance_variable_get("@rack_server").port - subject.current_url.should == "http://127.0.0.1:#{port}/?iframe=true" + driver.within_frame("f") do + driver.current_url.should == driver_url(driver, "/?iframe=true") end end it "returns the source code for the page" do - subject.within_frame("f") do - subject.source.should =~ %r{.*farewell.*}m + driver.within_frame("f") do + driver.source.should =~ %r{.*farewell.*}m end end it "evaluates Javascript" do - subject.within_frame("f") do - result = subject.evaluate_script(%#{env['CONTENT_TYPE']}
" [200, {"Content-Type" => "text/html", "Content-Length" => content_type.length.to_s}, [content_type]] @@ -159,48 +160,49 @@ describe Capybara::Webkit::Driver do end end + before { driver.visit("/") } + it "should redirect without content type" do - subject.visit("/form") - subject.find("//input").first.click - subject.find("//p").first.text.should == "" + driver.visit("/form") + driver.find("//input").first.click + driver.find("//p").first.text.should == "" end it "returns the current URL when changed by pushState after a redirect" do - subject.visit("/redirect-me") - port = subject.instance_variable_get("@rack_server").port - subject.execute_script("window.history.pushState({}, '', '/pushed-after-redirect')") - subject.current_url.should == "http://127.0.0.1:#{port}/pushed-after-redirect" + driver.visit("/redirect-me") + driver.execute_script("window.history.pushState({}, '', '/pushed-after-redirect')") + driver.current_url.should == driver_url(driver, "/pushed-after-redirect") end it "returns the current URL when changed by replaceState after a redirect" do - subject.visit("/redirect-me") - port = subject.instance_variable_get("@rack_server").port - subject.execute_script("window.history.replaceState({}, '', '/replaced-after-redirect')") - subject.current_url.should == "http://127.0.0.1:#{port}/replaced-after-redirect" + driver.visit("/redirect-me") + driver.execute_script("window.history.replaceState({}, '', '/replaced-after-redirect')") + driver.current_url.should == driver_url(driver, "/replaced-after-redirect") end end context "css app" do - before(:all) do + let(:driver) do body = "css" - @app = lambda do |env| + driver_for_app do |env| [200, {"Content-Type" => "text/css", "Content-Length" => body.length.to_s}, [body]] end - subject.visit("/") end + before { driver.visit("/") } + it "renders unsupported content types gracefully" do - subject.body.should =~ /css/ + driver.body.should =~ /css/ end it "sets the response headers with respect to the unsupported request" do - subject.response_headers["Content-Type"].should == "text/css" + driver.response_headers["Content-Type"].should == "text/css" end end context "hello app" do - before(:all) do - @app = lambda do |env| + let(:driver) do + driver_for_app do |env| body = <<-HTML @@ -227,166 +229,165 @@ describe Capybara::Webkit::Driver do end end + before { driver.visit("/") } + it "handles anchor tags" do - subject.visit("#test") - subject.find("//*[contains(., 'hello')]").should_not be_empty - subject.visit("#test") - subject.find("//*[contains(., 'hello')]").should_not be_empty + driver.visit("#test") + driver.find("//*[contains(., 'hello')]").should_not be_empty + driver.visit("#test") + driver.find("//*[contains(., 'hello')]").should_not be_empty end it "finds content after loading a URL" do - subject.find("//*[contains(., 'hello')]").should_not be_empty + driver.find("//*[contains(., 'hello')]").should_not be_empty end it "has an empty page after reseting" do - subject.reset! - subject.find("//*[contains(., 'hello')]").should be_empty + driver.reset! + driver.find("//*[contains(., 'hello')]").should be_empty end it "has a blank location after reseting" do - subject.reset! - subject.current_url.should == "" + driver.reset! + driver.current_url.should == "" end it "raises an error for an invalid xpath query" do - expect { subject.find("totally invalid salad") }. + expect { driver.find("totally invalid salad") }. to raise_error(Capybara::Webkit::InvalidResponseError, /xpath/i) end it "returns an attribute's value" do - subject.find("//p").first["id"].should == "greeting" + driver.find("//p").first["id"].should == "greeting" end it "parses xpath with quotes" do - subject.find('//*[contains(., "hello")]').should_not be_empty + driver.find('//*[contains(., "hello")]').should_not be_empty end it "returns a node's text" do - subject.find("//p").first.text.should == "hello" + driver.find("//p").first.text.should == "hello" end it "normalizes a node's text" do - subject.find("//div[contains(@class, 'normalize')]").first.text.should == "Spaces not normalized" + driver.find("//div[contains(@class, 'normalize')]").first.text.should == "Spaces not normalized" end it "returns the current URL" do - port = subject.instance_variable_get("@rack_server").port - subject.current_url.should == "http://127.0.0.1:#{port}/hello/world?success=true" + driver.visit "/hello/world?success=true" + driver.current_url.should == driver_url(driver, "/hello/world?success=true") end it "returns the current URL when changed by pushState" do - port = subject.instance_variable_get("@rack_server").port - subject.execute_script("window.history.pushState({}, '', '/pushed')") - subject.current_url.should == "http://127.0.0.1:#{port}/pushed" + driver.execute_script("window.history.pushState({}, '', '/pushed')") + driver.current_url.should == driver_url(driver, "/pushed") end it "returns the current URL when changed by replaceState" do - port = subject.instance_variable_get("@rack_server").port - subject.execute_script("window.history.replaceState({}, '', '/replaced')") - subject.current_url.should == "http://127.0.0.1:#{port}/replaced" + driver.execute_script("window.history.replaceState({}, '', '/replaced')") + driver.current_url.should == driver_url(driver, "/replaced") end it "does not double-encode URLs" do - subject.visit("/hello/world?success=%25true") - subject.current_url.should =~ /success=\%25true/ + driver.visit("/hello/world?success=%25true") + driver.current_url.should =~ /success=\%25true/ end it "visits a page with an anchor" do - subject.visit("/hello#display_none") - subject.current_url.should =~ /hello#display_none/ + driver.visit("/hello#display_none") + driver.current_url.should =~ /hello#display_none/ end it "returns the source code for the page" do - subject.source.should =~ %r{.*greeting.*}m + driver.source.should =~ %r{.*greeting.*}m end it "evaluates Javascript and returns a string" do - result = subject.evaluate_script(%