spec/env_spec.rb in grope-0.0.4 vs spec/env_spec.rb in grope-0.1.1
- old
+ new
@@ -1,52 +1,65 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'pathname'
+require 'shellwords'
+
describe Grope::Env do
before do
@env = Grope::Env.new
- @env.load('http://example.com')
+ @env.load('http://httpstat.us/')
+ sleep 1
end
it "should initialize" do
- @env.document.title.should eql('Example Web Page')
+ @env.document.title.should eql('httpstat.us')
end
it "should get elements by XPath" do
body = @env.find('//body')
- @env.find('//a', body).href.should eql('http://www.rfc-editor.org/rfc/rfc2606.txt')
- @env.all('//a', body)[0].href.should eql('http://www.rfc-editor.org/rfc/rfc2606.txt')
+ @env.find('//a', body).href.should eql('http://httpstat.us/200')
+ @env.all('//a', body)[0].href.should eql('http://httpstat.us/200')
end
it "should get links" do
result = @env.document.links
- result[0].href.should eql('http://www.rfc-editor.org/rfc/rfc2606.txt')
+ result[0].href.should eql('http://httpstat.us/200')
end
it "should eval" do
- @env.window.location.href.should eql('http://example.com/')
+ @env.window.location.href.should eql('http://httpstat.us/')
end
- it "should redirect" do
- @env.eval('location.href="http://example.org"')
- @env.document.location.href.should eql('http://example.org/')
+ it "should redirect by javascript" do
+ @env.eval('location.href="http://httpstat.us/200"')
+ @env.wait
+
+ @env.document.URL.should eql('http://httpstat.us/200')
end
+ it "should redirect by status code" do
+ @env.load('http://httpstat.us/302')
+ @env.wait
+
+ @env.document.URL.should eql('http://httpstat.us/')
+ end
+
it "should redirect by click" do
anchor = @env.document.links[0]
js = @env.eval('return Grope;')
js.click(anchor)
# TODO: wait automatically after js function is called
@env.wait
- @env.document.URL.should eql('http://www.rfc-editor.org/rfc/rfc2606.txt')
+ @env.document.URL.should eql('http://httpstat.us/200')
end
it "should get/call function" do
function = @env.eval('return function(x, y) { return x * y }')
- function.call(false, 3, 5).should eql(15)
- function.apply(false, [3, 5]).should eql(15)
+ function.call(false, 3, 5).should eql(15.0)
+ function.apply(false, [3, 1.5]).should eql(4.5)
end
it "should call function in object" do
obj = @env.eval('return { hello: function() { return "hello"; }}')
obj.hello.should eql('hello')
@@ -67,7 +80,57 @@
it "should use own cookie storage if 'use_shared_cookie' option is false" do
@env.instance_eval { @resource_load_delegate }.should_not be_nil
@env.load('http://google.com/')
@env.instance_eval { @resource_load_delegate }.cookie_storage.cookies(URI('http://google.com/')).should_not be_nil
+ end
+
+ describe "#capture" do
+ before do
+ dir = Pathname(Dir.tmpdir)
+ @filename = dir + 'test.png'
+ end
+
+ after do
+ if @filename.file?
+ @filename.unlink
+ end
+ end
+
+ it "should capture specified element and write to file as png" do
+
+ element = @env.find('//p')
+
+ @env.capture(element, @filename)
+
+ info = `file #{Shellwords.shellescape(@filename.to_s)}`
+
+ info.should match(/PNG image/)
+
+ width, height = info.scan(/(\d+) x (\d+)/)[0]
+
+ width.to_i.should eql(element.clientWidth)
+ height.to_i.should eql(element.clientHeight)
+ end
+
+ it "should capture whole content" do
+ @env.load('http://httpstat.us/200')
+ @env.capture(nil, @filename)
+
+ info = `file #{Shellwords.shellescape(@filename.to_s)}`
+
+ info.should match(/PNG image/)
+
+ width, height = info.scan(/(\d+) x (\d+)/)[0]
+
+ width.to_i.should eql(1024)
+ height.to_i.should eql(600)
+ end
+
+ it "should raise error if width(or height) of specified element is zero" do
+ lambda {
+ element = @env.find('//a')
+ @env.capture(element, @filename)
+ }.should raise_error(RuntimeError)
+ end
end
end