capybara-webkit/spec/driver_spec.rb

115 lines
3.2 KiB
Ruby
Raw Normal View History

2011-02-19 03:53:06 +00:00
require 'spec_helper'
require 'capybara/driver/webkit'
describe Capybara::Driver::Webkit do
let(:hello_app) do
lambda do |env|
body = <<-HTML
<html><body>
<script type="text/javascript">
2011-02-25 05:15:08 +00:00
document.write("<p id='greeting'>he" + "llo</p>");
2011-02-19 03:53:06 +00:00
</script>
</body></html>
HTML
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end
before(:all) { @@browser = Capybara::Driver::Webkit::Browser.new }
subject { Capybara::Driver::Webkit.new(hello_app, :browser => @@browser) }
2011-02-25 22:53:36 +00:00
before { subject.visit("/hello/world?success=true") }
2011-02-19 03:53:06 +00:00
after { subject.reset! }
it "finds content after loading a URL" do
subject.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
end
it "raises an error for an invalid xpath query" do
expect { subject.find("totally invalid salad") }.
to raise_error(Capybara::Driver::Webkit::WebkitError, /xpath/i)
end
2011-02-25 05:15:08 +00:00
it "returns an attribute's value" do
subject.find("//p").first["id"].should == "greeting"
end
it "parses xpath with quotes" do
subject.find('//*[contains(., "hello")]').should_not be_empty
end
it "returns a node's text" do
subject.find("//p").first.text.should == "hello"
end
2011-02-25 22:53:36 +00:00
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"
end
2011-02-25 23:04:23 +00:00
it "returns the source code for the page" do
subject.source.should == %{<html><head></head><body>
<script type="text/javascript">
document.write("<p id='greeting'>he" + "llo</p>");
</script><p id="greeting">hello</p>
</body></html>}
end
2011-02-26 15:19:24 +00:00
it "aliases body as source" do
subject.body.should == subject.source
end
2011-02-26 18:02:43 +00:00
it "evaluates Javascript and returns a string" do
result = subject.evaluate_script(%<document.getElementById('greeting').innerText>)
result.should == "hello"
end
it "evaluates Javascript and returns an array" do
result = subject.evaluate_script(%<["hello", "world"]>)
result.should == %w(hello world)
end
it "evaluates Javascript and returns an int" do
result = subject.evaluate_script(%<123>)
result.should == 123
end
it "evaluates Javascript and returns a float" do
result = subject.evaluate_script(%<1.5>)
result.should == 1.5
end
it "evaluates Javascript and returns null" do
result = subject.evaluate_script(%<(function () {})()>)
result.should == nil
end
it "evaluates Javascript and returns an object" do
result = subject.evaluate_script(%<({ 'one' : 1 })>)
result.should == { 'one' => 1 }
end
it "evaluates Javascript and returns true" do
result = subject.evaluate_script(%<true>)
result.should === true
end
it "evaluates Javascript and returns false" do
result = subject.evaluate_script(%<false>)
result.should === false
end
it "evaluates Javascript and returns an escaped string" do
result = subject.evaluate_script(%<'"'>)
result.should === "\""
end
2011-02-19 03:53:06 +00:00
end