mechanize.cr/spec/agent_spec.cr

67 lines
2.0 KiB
Crystal
Raw Normal View History

2021-06-12 16:17:45 +02:00
require "./spec_helper"
describe "Mechanize Agent test" do
2021-06-16 07:17:11 +02:00
it "can fill and submit form" do
2021-06-14 16:41:36 +02:00
agent = Mechanize.new
2021-06-16 16:20:03 +02:00
page = agent.get("http://example.com/form")
2021-06-14 16:41:36 +02:00
form = page.forms[0]
form.field_with("name").value = "foo"
form.field_with("email").value = "bar"
page = agent.submit(form)
page.not_nil!.code.should eq 200
page.not_nil!.body.should eq "success"
end
2021-06-13 00:58:50 +02:00
2021-06-29 12:14:30 +02:00
it "can fill and submit form with submit button" do
agent = Mechanize.new
page = agent.get("http://example.com/form")
form = page.forms[0]
form.field_with("name").value = "foo"
form.field_with("email").value = "bar"
submit_button = form.buttons[0]
page = agent.submit(form, submit_button)
page.not_nil!.code.should eq 200
page.not_nil!.body.should eq "success with button"
end
2021-06-16 07:17:11 +02:00
it "can save history" do
agent = Mechanize.new
agent.get("http://example.com/")
agent.history.size.should eq 1
agent.history.last.title.should eq ""
2021-06-16 16:20:03 +02:00
agent.get("http://example.com/form")
2021-06-16 07:17:11 +02:00
agent.history.size.should eq 2
agent.history.last.title.should eq "page_title"
end
2021-06-16 07:22:29 +02:00
it "can back previous page" do
agent = Mechanize.new
agent.get("http://example.com/")
2021-06-16 16:20:03 +02:00
agent.get("http://example.com/form")
2021-06-16 07:22:29 +02:00
agent.current_page.title.should eq "page_title"
agent.back
agent.current_page.title.should eq ""
end
2021-08-10 07:24:47 +02:00
it "can set maximum number of items allowed in the history" do
agent1 = Mechanize.new
agent1.get("http://example.com/")
agent1.get("http://example.com/form")
agent1.history.size.should eq 2
# set max size
agent2 = Mechanize.new
agent2.max_history = 1
agent2.get("http://example.com/")
agent2.get("http://example.com/form")
agent2.history.size.should eq 1
agent2.history.pop.uri.to_s.should eq "http://example.com/form"
end
2021-08-22 02:47:10 +02:00
it "can download page" do
agent = Mechanize.new
agent.download("http://example.com", "mechanizecr_example.html")
File.exists?("mechanizecr_example.html").should eq true
File.delete("mechanizecr_example.html")
end
2021-06-12 16:17:45 +02:00
end