require File.expand_path(File.dirname(__FILE__) + "../../spec_helper") describe PageModels::Base do class UnimplementedPageModel < PageModels::Base end class TestPageModel < PageModels::Base def initialize(url="http://test-page") @url = url end def url @url end def verify! true end end describe "template methods which must be implemented by your page models" do it "should raise an error if page models do not implement #url" do lambda { UnimplementedPageModel.new.url }.should raise_error(PageModels::ImplementationError) end it "should raise an error if page models do not implement #verify!" do lambda { UnimplementedPageModel.new.verify! }.should raise_error(PageModels::ImplementationError) end end describe "delegating methods to the driver for less verbose page models" do before(:each) do @driver = Object.new PageModels::Configuration.instance.stub(:driver).and_return(@driver) @page_model = TestPageModel.new end it "should delegate a missing method to the driver" do @driver.should_receive(:do_something_cool).with(:please) @page_model.do_something_cool(:please) end it "should should not hide method missing errors if the method does not exist on the driver" do lambda { @page_model.do_something_else }.should raise_error(NoMethodError, "undefined method `do_something_else' for #{@page_model.inspect}") end end describe "constructing page URLs" do before(:each) do PageModels::Configuration.instance.stub(:driver).and_return(Capybara::Session.new) PageModels::Configuration.instance.base_url = "https://1.2.3.4:4321" @page_model = TestPageModel.new end it "uses the base URL from config" do pm = TestPageModel.new("/foo") pm.should_receive(:visit).with("https://1.2.3.4:4321/foo") pm.should_receive(:verify!) pm.open! end it "ignores base URL if http:// is specified" do pm = TestPageModel.new("http://foo") pm.should_receive(:visit).with("http://foo") pm.should_receive(:verify!) pm.open! end it "ignores base URL if https:// is specified" do pm = TestPageModel.new("https://foo") pm.should_receive(:visit).with("https://foo") pm.should_receive(:verify!) pm.open! end end describe "opening a page with driver" do before(:each) do @page_model = TestPageModel.new end describe "for capybara" do it "should visit the page, then call verify" do PageModels::Configuration.instance.stub(:driver).and_return(Capybara::Session.new) @page_model.should_receive(:visit).with("http://test-page") @page_model.should_receive(:verify!) @page_model.open! end end describe "for celerity" do it "should goto the page, then call verify" do PageModels::Configuration.instance.stub(:driver).and_return(Celerity::Browser.new) @page_model.should_receive(:goto).with("http://test-page") @page_model.should_receive(:verify!) @page_model.open! end end describe "for watir-webdriver" do it "should goto the page, then call verify" do PageModels::Configuration.instance.stub(:driver).and_return(Watir::Browser.new(:chrome)) @page_model.should_receive(:goto).with("http://test-page") @page_model.should_receive(:verify!) @page_model.open! end end end end