refactor test
parent
601a12f1ee
commit
7d8c56e3f2
|
@ -1,6 +1,4 @@
|
|||
require "./spec_helper"
|
||||
WebMock.stub(:get, "example.com/")
|
||||
WebMock.stub(:get, "another_domain.com/")
|
||||
WebMock.stub(:get, "example.com/cookies1").to_return(headers: {"Set-Cookie" => "id=123"})
|
||||
WebMock.stub(:get, "example.com/cookies2").to_return(headers: {"Set-Cookie" => "name=kanezoh"})
|
||||
WebMock.stub(:get, "example.com/meta_cookie").to_return(body:
|
||||
|
@ -15,30 +13,11 @@ WebMock.stub(:get, "example.com/meta_cookie").to_return(body:
|
|||
</html>
|
||||
BODY
|
||||
)
|
||||
WebMock.stub(:get, "html_example.com").to_return(body:
|
||||
<<-BODY
|
||||
<html>
|
||||
<head>
|
||||
<title>page_title</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="post_path" method="post" name="sample_form">
|
||||
<input type="text" name="name">
|
||||
<input type="text" name="email">
|
||||
<input type="submit" value="">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
BODY
|
||||
)
|
||||
WebMock.stub(:post, "http://html_example.com/post_path").
|
||||
with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}).
|
||||
to_return(body: "success")
|
||||
|
||||
describe "Mechanize Agent test" do
|
||||
it "can fill and submit form" do
|
||||
agent = Mechanize.new
|
||||
page = agent.get("http://html_example.com/")
|
||||
page = agent.get("http://example.com/form")
|
||||
form = page.forms[0]
|
||||
form.field_with("name").value = "foo"
|
||||
form.field_with("email").value = "bar"
|
||||
|
@ -55,7 +34,6 @@ describe "Mechanize Agent test" do
|
|||
agent.get("http://example.com/")
|
||||
agent.request_headers["Cookie"].should eq "id=123"
|
||||
end
|
||||
|
||||
it "can receive and send multiple cookies" do
|
||||
agent = Mechanize.new
|
||||
# receive cookies1
|
||||
|
@ -87,7 +65,7 @@ describe "Mechanize Agent test" do
|
|||
agent.get("http://example.com/")
|
||||
agent.history.size.should eq 1
|
||||
agent.history.last.title.should eq ""
|
||||
agent.get("http://html_example.com/")
|
||||
agent.get("http://example.com/form")
|
||||
agent.history.size.should eq 2
|
||||
agent.history.last.title.should eq "page_title"
|
||||
end
|
||||
|
@ -95,7 +73,7 @@ describe "Mechanize Agent test" do
|
|||
it "can back previous page" do
|
||||
agent = Mechanize.new
|
||||
agent.get("http://example.com/")
|
||||
agent.get("http://html_example.com/")
|
||||
agent.get("http://example.com/form")
|
||||
agent.current_page.title.should eq "page_title"
|
||||
agent.back
|
||||
agent.current_page.title.should eq ""
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
require "./spec_helper"
|
||||
WebMock.stub(:get, "html_example.com").to_return(body:
|
||||
|
||||
WebMock.stub(:get, "example.com/check_form").to_return(body:
|
||||
<<-BODY
|
||||
<html>
|
||||
<meta>
|
||||
<head>
|
||||
<title>page_title</title>
|
||||
</head>
|
||||
|
@ -10,7 +10,7 @@ WebMock.stub(:get, "html_example.com").to_return(body:
|
|||
<form action="post_path" method="post" name="sample_form">
|
||||
<input type="text" name="name">
|
||||
<input type="text" name="email">
|
||||
<input type="checkbox" id="ababa" name="ababa" checked>
|
||||
<input type="checkbox" id="remember_me" name="remember_me" checked>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -19,7 +19,7 @@ BODY
|
|||
|
||||
describe "Mechanize Form test" do
|
||||
agent = Mechanize.new
|
||||
uri = "http://html_example.com/"
|
||||
uri = "http://example.com/check_form"
|
||||
page = agent.get(uri)
|
||||
form = page.forms.first
|
||||
|
||||
|
@ -59,8 +59,10 @@ describe "Mechanize Form test" do
|
|||
checkbox.click
|
||||
checkbox.checked?.should eq true
|
||||
end
|
||||
it "returns query value" do
|
||||
p checkbox.query_value
|
||||
it "doesn't include request data if checkbox isn't checked" do
|
||||
form.request_data.should contain("remember_me=on")
|
||||
checkbox.uncheck
|
||||
form.request_data.should_not contain("remember_me=")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
require "./spec_helper"
|
||||
WebMock.stub(:get, "example.com")
|
||||
WebMock.stub(:get, "http://example.com/?foo=bar&foo1=bar2")
|
||||
WebMock.stub(:post, "http://example.com/post").
|
||||
with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}).
|
||||
|
|
|
@ -1,24 +1,4 @@
|
|||
require "./spec_helper"
|
||||
WebMock.stub(:get, "example.com")
|
||||
WebMock.stub(:get, "fail_example.com").to_return(status: 500)
|
||||
WebMock.stub(:get, "body_example.com").to_return(body: "hello")
|
||||
WebMock.stub(:get, "html_example.com").to_return(body:
|
||||
<<-BODY
|
||||
<html>
|
||||
<meta>
|
||||
<head>
|
||||
<title>page_title</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="post_path">
|
||||
<input type="text" name="name">
|
||||
<input type="text" name="email">
|
||||
<input type="submit" value="">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
BODY
|
||||
)
|
||||
|
||||
describe "Mechanize Page test" do
|
||||
it "return status code of request" do
|
||||
|
@ -41,13 +21,13 @@ describe "Mechanize Page test" do
|
|||
agent = Mechanize.new
|
||||
page = agent.get("http://example.com/")
|
||||
page.title.should eq ""
|
||||
page = agent.get("http://html_example.com")
|
||||
page = agent.get("http://example.com/form")
|
||||
page.title.should eq "page_title"
|
||||
end
|
||||
|
||||
it "return page forms" do
|
||||
agent = Mechanize.new
|
||||
page = agent.get("http://html_example.com")
|
||||
page = agent.get("http://example.com/form")
|
||||
page.forms.size.should eq 1
|
||||
page.forms.first.action.should eq "post_path"
|
||||
end
|
||||
|
|
|
@ -1,3 +1,29 @@
|
|||
require "spec"
|
||||
require "webmock"
|
||||
require "../src/mechanize"
|
||||
|
||||
|
||||
WebMock.stub(:get, "example.com")
|
||||
WebMock.stub(:get, "fail_example.com").to_return(status: 500)
|
||||
WebMock.stub(:get, "body_example.com").to_return(body: "hello")
|
||||
WebMock.stub(:get, "another_domain.com/")
|
||||
|
||||
WebMock.stub(:get, "example.com/form").to_return(body:
|
||||
<<-BODY
|
||||
<html>
|
||||
<head>
|
||||
<title>page_title</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="post_path" method="post" name="sample_form">
|
||||
<input type="text" name="name">
|
||||
<input type="text" name="email">
|
||||
<input type="submit" value="">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
BODY
|
||||
)
|
||||
WebMock.stub(:post, "example.com/post_path").
|
||||
with(body: "name=foo&email=bar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}).
|
||||
to_return(body: "success")
|
||||
|
|
Loading…
Reference in New Issue