2008-05-12 03:58:20 +00:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2008-03-02 20:14:52 +00:00
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
describe "clicks_link" do
|
|
|
|
before do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session = Webrat::TestSession.new
|
2008-03-02 20:14:52 +00:00
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should use get by default" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Link text"
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click get links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page", {})
|
2008-10-26 21:02:58 +00:00
|
|
|
@session.clicks_link "Link text", :method => :get
|
2008-03-02 20:14:52 +00:00
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click delete links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:delete).with("/page", {})
|
2008-10-26 21:02:58 +00:00
|
|
|
@session.clicks_link "Link text", :method => :delete
|
2008-03-02 20:14:52 +00:00
|
|
|
end
|
|
|
|
|
2008-07-25 23:26:59 +00:00
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click post links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:post).with("/page", {})
|
2008-10-26 21:02:58 +00:00
|
|
|
@session.clicks_link "Link text", :method => :post
|
2008-03-02 20:14:52 +00:00
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click put links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:put).with("/page", {})
|
2008-10-26 21:02:58 +00:00
|
|
|
@session.clicks_link "Link text", :method => :put
|
2008-03-02 20:14:52 +00:00
|
|
|
end
|
|
|
|
|
2008-10-28 20:02:35 +00:00
|
|
|
it "should click links by regexp" do
|
|
|
|
@session.response_body = <<-EOS
|
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
|
|
|
@session.should_receive(:get).with("/page", {})
|
|
|
|
@session.clicks_link /link [a-z]/i
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click rails javascript links with authenticity tokens" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/posts" onclick="var f = document.createElement('form');
|
|
|
|
f.style.display = 'none';
|
|
|
|
this.parentNode.appendChild(f);
|
|
|
|
f.method = 'POST';
|
|
|
|
f.action = this.href;
|
|
|
|
var s = document.createElement('input');
|
|
|
|
s.setAttribute('type', 'hidden');
|
|
|
|
s.setAttribute('name', 'authenticity_token');
|
|
|
|
s.setAttribute('value', 'aa79cb354597a60a3786e7e291ed4f74d77d3a62');
|
|
|
|
f.appendChild(s);
|
|
|
|
f.submit();
|
|
|
|
return false;">Posts</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Posts"
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click rails javascript delete links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/posts/1" onclick="var f = document.createElement('form');
|
|
|
|
f.style.display = 'none';
|
|
|
|
this.parentNode.appendChild(f);
|
|
|
|
f.method = 'POST';
|
|
|
|
f.action = this.href;
|
|
|
|
var m = document.createElement('input');
|
|
|
|
m.setAttribute('type', 'hidden');
|
|
|
|
m.setAttribute('name', '_method');
|
|
|
|
m.setAttribute('value', 'delete');
|
|
|
|
f.appendChild(m);
|
|
|
|
f.submit();
|
|
|
|
return false;">Delete</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:delete).with("/posts/1", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Delete"
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click rails javascript post links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/posts" onclick="var f = document.createElement('form');
|
|
|
|
f.style.display = 'none';
|
|
|
|
this.parentNode.appendChild(f);
|
|
|
|
f.method = 'POST';
|
|
|
|
f.action = this.href;
|
|
|
|
f.submit();
|
|
|
|
return false;">Posts</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:post).with("/posts", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Posts"
|
|
|
|
end
|
|
|
|
|
2008-06-20 15:22:41 +00:00
|
|
|
it "should click rails javascript post links without javascript" do
|
|
|
|
@session.response_body = <<-EOS
|
|
|
|
<a href="/posts" onclick="var f = document.createElement('form');
|
|
|
|
f.style.display = 'none';
|
|
|
|
this.parentNode.appendChild(f);
|
|
|
|
f.method = 'POST';
|
|
|
|
f.action = this.href;
|
|
|
|
f.submit();
|
|
|
|
return false;">Posts</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/posts", {})
|
2008-06-20 15:22:41 +00:00
|
|
|
@session.clicks_link "Posts", :javascript => false
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click rails javascript put links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/posts" onclick="var f = document.createElement('form');
|
|
|
|
f.style.display = 'none';
|
|
|
|
this.parentNode.appendChild(f);
|
|
|
|
f.method = 'POST';
|
|
|
|
f.action = this.href;
|
|
|
|
var m = document.createElement('input');
|
|
|
|
m.setAttribute('type', 'hidden');
|
|
|
|
m.setAttribute('name', '_method');
|
|
|
|
m.setAttribute('value', 'put');
|
|
|
|
f.appendChild(m);
|
|
|
|
f.submit();
|
|
|
|
return false;">Put</a></h2>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:put).with("/posts", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Put"
|
|
|
|
end
|
|
|
|
|
2008-08-11 04:29:21 +00:00
|
|
|
it "should fail if the javascript link doesn't have a value for the _method input" do
|
|
|
|
@session.response_body = <<-EOS
|
|
|
|
<a href="/posts/1" onclick="var f = document.createElement('form');
|
|
|
|
f.style.display = 'none';
|
|
|
|
this.parentNode.appendChild(f);
|
|
|
|
f.method = 'POST';
|
|
|
|
f.action = this.href;
|
|
|
|
var m = document.createElement('input');
|
|
|
|
m.setAttribute('type', 'hidden');
|
|
|
|
m.setAttribute('name', '_method');
|
|
|
|
f.appendChild(m);
|
|
|
|
f.submit();
|
|
|
|
return false;">Link</a>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
lambda {
|
|
|
|
@session.clicks_link "Link"
|
|
|
|
}.should raise_error
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should assert valid response" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
2008-10-25 16:30:30 +00:00
|
|
|
@session.response_code = 501
|
2008-05-12 03:58:20 +00:00
|
|
|
lambda { @session.clicks_link "Link text" }.should raise_error
|
2008-03-02 20:14:52 +00:00
|
|
|
end
|
|
|
|
|
2008-10-25 16:30:30 +00:00
|
|
|
[200, 300, 400, 499].each do |status|
|
2008-10-25 21:17:00 +00:00
|
|
|
it "should consider the #{status} status code as success" do
|
2008-10-25 16:30:30 +00:00
|
|
|
@session.response_body = <<-EOS
|
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
|
|
|
@session.response_code = status
|
|
|
|
lambda { @session.clicks_link "Link text" }.should_not raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-11 04:29:21 +00:00
|
|
|
it "should fail is the link doesn't exist" do
|
|
|
|
@session.response_body = <<-EOS
|
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
lambda {
|
|
|
|
@session.clicks_link "Missing link"
|
|
|
|
}.should raise_error
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should not be case sensitive" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">Link text</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "LINK TEXT"
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should match link substrings" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page">This is some cool link text, isn't it?</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Link text"
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should work with elements in the link" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page"><span>Link text</span></a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Link text"
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should match the first matching link" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-02 20:14:52 +00:00
|
|
|
<a href="/page1">Link text</a>
|
|
|
|
<a href="/page2">Link text</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page1", {})
|
2008-03-02 20:14:52 +00:00
|
|
|
@session.clicks_link "Link text"
|
|
|
|
end
|
2008-03-13 22:01:00 +00:00
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should choose the shortest link text match" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-13 22:01:00 +00:00
|
|
|
<a href="/page1">Linkerama</a>
|
|
|
|
<a href="/page2">Link</a>
|
|
|
|
EOS
|
|
|
|
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page2", {})
|
2008-03-13 22:01:00 +00:00
|
|
|
@session.clicks_link "Link"
|
|
|
|
end
|
|
|
|
|
2008-07-25 23:26:59 +00:00
|
|
|
it "should treat non-breaking spaces as spaces" do
|
|
|
|
@session.response_body = <<-EOS
|
|
|
|
<a href="/page1">This is a link</a>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
@session.should_receive(:get).with("/page1", {})
|
|
|
|
@session.clicks_link "This is a link"
|
|
|
|
end
|
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should click link within a selector" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-04-12 21:07:33 +00:00
|
|
|
<a href="/page1">Link</a>
|
|
|
|
<div id="container">
|
|
|
|
<a href="/page2">Link</a>
|
|
|
|
</div>
|
|
|
|
EOS
|
|
|
|
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page2", {})
|
2008-04-12 21:07:33 +00:00
|
|
|
@session.clicks_link_within "#container", "Link"
|
|
|
|
end
|
2008-03-06 15:04:01 +00:00
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should not make request when link is local anchor" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-03-06 15:04:01 +00:00
|
|
|
<a href="#section-1">Jump to Section 1</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
# Don't know why @session.should_receive(:get).never doesn't work here
|
|
|
|
@session.should_receive(:send).with('get_via_redirect', '#section-1', {}).never
|
2008-03-06 15:04:01 +00:00
|
|
|
@session.clicks_link "Jump to Section 1"
|
|
|
|
end
|
2008-04-12 23:20:43 +00:00
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should follow relative links" do
|
2008-08-10 19:38:32 +00:00
|
|
|
@session.stub!(:current_url).and_return("/page")
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-04-12 23:20:43 +00:00
|
|
|
<a href="sub">Jump to sub page</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page/sub", {})
|
2008-04-12 23:20:43 +00:00
|
|
|
@session.clicks_link "Jump to sub page"
|
|
|
|
end
|
2008-05-04 08:15:36 +00:00
|
|
|
|
|
|
|
it "should follow fully qualified local links" do
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-05-04 08:15:36 +00:00
|
|
|
<a href="http://www.example.com/page/sub">Jump to sub page</a>
|
|
|
|
EOS
|
2008-08-22 14:57:00 +00:00
|
|
|
@session.should_receive(:get).with("http://www.example.com/page/sub", {})
|
2008-05-04 08:15:36 +00:00
|
|
|
@session.clicks_link "Jump to sub page"
|
|
|
|
end
|
2008-04-12 23:20:43 +00:00
|
|
|
|
2008-05-04 08:15:36 +00:00
|
|
|
it "should follow query parameters" do
|
2008-08-10 19:38:32 +00:00
|
|
|
@session.stub!(:current_url).and_return("/page")
|
2008-05-12 03:58:20 +00:00
|
|
|
@session.response_body = <<-EOS
|
2008-04-12 23:20:43 +00:00
|
|
|
<a href="?foo=bar">Jump to foo bar</a>
|
|
|
|
EOS
|
2008-07-25 23:11:56 +00:00
|
|
|
@session.should_receive(:get).with("/page?foo=bar", {})
|
2008-04-12 23:20:43 +00:00
|
|
|
@session.clicks_link "Jump to foo bar"
|
|
|
|
end
|
2008-03-02 20:14:52 +00:00
|
|
|
end
|