Skip to content

Commit

Permalink
Merge pull request cheezy#254 from leviwilson/expected_title
Browse files Browse the repository at this point in the history
Fix #wait_for_expected_title?
  • Loading branch information
cheezy committed Dec 8, 2014
2 parents 7264170 + fa8dbf6 commit 5edaacd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/page-object/accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,18 @@ def page_url(url)
#
def wait_for_expected_title(expected_title, timeout=::PageObject.default_element_wait)
define_method("wait_for_expected_title?") do
page_title = title
has_expected_title = (expected_title === page_title)
if not has_expected_title and not timeout.nil?
wait_until(timeout, "Expected title '#{expected_title}' instead of '#{page_title}'") do
has_expected_title = (expected_title === page_title)
has_expected_title
end
end
raise "Expected title '#{expected_title}' instead of '#{page_title}'" unless has_expected_title
error_message = lambda { "Expected title '#{expected_title}' instead of '#{title}'" }

has_expected_title = (expected_title === title)
wait_until(timeout, error_message.call) do
has_expected_title = (expected_title === title)
end unless has_expected_title

raise error_message.call unless has_expected_title
has_expected_title
end
end


#
# Creates a method that compares the expected_title of a page against the actual.
# @param [String] expected_title the literal expected title for the page
Expand Down
40 changes: 40 additions & 0 deletions spec/page-object/accessors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'

class GenericPage
include PageObject

wait_for_expected_title 'expected title'
end

describe 'accessors' do
let(:browser) { mock_watir_browser }
let(:page) { GenericPage.new browser }

context '#wait_for_expected_title' do
before(:each) do
allow(browser).to receive(:wait_until).and_yield
end

it 'true if already there' do
allow(browser).to receive(:title).and_return 'expected title'
expect(page.wait_for_expected_title?).to be_truthy
end

it 'does not wait if it already is there' do
allow(browser).to receive(:title).and_return 'expected title'
expect(browser).to_not receive(:wait_until)

expect(page.wait_for_expected_title?).to be_truthy
end

it 'errors when it does not match' do
allow(browser).to receive(:title).and_return 'wrong title'
expect { page.wait_for_expected_title? }.to raise_error "Expected title 'expected title' instead of 'wrong title'"
end

it 'picks up when the title changes' do
allow(browser).to receive(:title).and_return 'wrong title', 'expected title'
expect(page.wait_for_expected_title?).to be_truthy
end
end
end

0 comments on commit 5edaacd

Please sign in to comment.