Skip to content

Commit

Permalink
Update wait logic and fix tests
Browse files Browse the repository at this point in the history
Fix #34
  • Loading branch information
bootstraponline committed Jun 7, 2015
1 parent 7f133c0 commit 449c605
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 23 deletions.
18 changes: 12 additions & 6 deletions lib/angular_webdriver/protractor/protractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get destination, opt_timeout=driver.max_page_wait_seconds
# now that the url has changed, make sure Angular has loaded
# note that the mock module logic is omitted.
#
waitForAngular
waitForAngular description: 'Protractor.get', timeout: timeout
end

# Invokes the underlying driver.get. Does not wait for angular.
Expand Down Expand Up @@ -329,19 +329,25 @@ def sync webdriver_command
#
# Will wait up to driver.max_wait_seconds (set with driver.set_max_wait)
#
# @param [String] opt_description An optional description to be added
# to webdriver logs.
# @param [Hash] opts
# @option opts [String] :description An optional description to be added
# to webdriver logs.
# @option opts [Integer] :timeout Amount of time in seconds to wait for
# angular to load. Default driver.max_wait_seconds
# @return [WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array]
#
def waitForAngular opt_description='' # Protractor.prototype.waitForAngular
def waitForAngular opts={} # Protractor.prototype.waitForAngular
return if ignore_sync

wait(timeout: driver.max_wait_seconds, bubble: true) do
description = opts.fetch(:description, '')
timeout = opts.fetch(:timeout, driver.max_wait_seconds)

wait(timeout: timeout, bubble: true) do
begin
# the client side script will return a string on error
# the string won't be raised as an error unless we explicitly do so here
error = executeAsyncScript_(client_side_scripts.wait_for_angular,
"Protractor.waitForAngular() #{opt_description}",
"Protractor.waitForAngular() #{description}",
root_element)
raise Selenium::WebDriver::Error::JavascriptError, error if error
rescue Exception => e
Expand Down
2 changes: 1 addition & 1 deletion lib/angular_webdriver/protractor/webdriver_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def set_max_page_wait value

# Gets the wait time in seconds used when loading pages with protractor.get
#
# Defaults to 30
# Defaults to 30 seconds
#
# @return [Numeric] the wait time in seconds
def max_page_wait_seconds
Expand Down
9 changes: 6 additions & 3 deletions spec/helpers/expect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ def expect_no_element_error &block
end
end

# Expects on no_such_element_error and does not modify client wait
# does not use no_wait.
def expect_no_element_error_nowait &block
# Expects on no_such_element_error. The client wait is respected.
# Compare to expect_no_element_error which sets wait to 0 before
# invoking the block.
def expect_no_element_error_with_wait &block
expect { block.call }.to raise_error no_such_element_error
end

Expand All @@ -31,6 +32,8 @@ def expect_no_method_error &block
end

# Expects block to raise error
#
# Does **not** modify client wait.
def expect_error &block
expect { block.call }.to raise_error
end
19 changes: 17 additions & 2 deletions spec/helpers/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def max_wait_seconds_default
10
end

def page_wait_seconds_default
def max_page_wait_seconds_default
30
end

Expand Down Expand Up @@ -32,7 +32,7 @@ def no_wait &block
driver.set_max_page_wait 0
result = block.call
driver.set_max_wait max_wait_seconds_default
driver.set_max_page_wait page_wait_seconds_default
driver.set_max_page_wait max_page_wait_seconds_default
result
end

Expand All @@ -41,6 +41,21 @@ def set_max_wait timeout_seconds
driver.set_max_wait timeout_seconds
end

# driver.max_wait_seconds
def max_wait_seconds
driver.max_wait_seconds
end

# Sets the driver's set_max_page_wait (client side wait used in protractor.get)
def set_max_page_wait timeout_seconds
driver.set_max_page_wait timeout_seconds
end

# Returns driver.max_page_wait_seconds
def max_page_wait_seconds
driver.max_page_wait_seconds
end

# Returns time in seconds it took for the block to execute.
def time_seconds &block
start = Time.now
Expand Down
55 changes: 47 additions & 8 deletions spec/protractor_compatability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,73 @@
no_wait { expect(does_not_exist.present?).to eq(false) }
end

it 'client waits when using custom protractor locator' do
it 'set_max_wait' do
# expect_no_element_error_nowait is used because it doesn't modify the wait
# don't use expect_no_element_error. that will modify the wait.
does_not_exist = by.binding('does not exist')

set_max_wait 0
time = time_seconds { expect_no_element_error_nowait { element(does_not_exist).visible? } }
time = time_seconds { expect_no_element_error_with_wait { element(does_not_exist).visible? } }
expect_equal time, 0
expect_equal driver.max_wait_seconds, 0
expect_equal max_wait_seconds, 0

# find by all returns [] when there are no matches.
time = time_seconds { expect_no_error { element.all(does_not_exist).to_a } }
expect_equal time, 0
expect_equal driver.max_wait_seconds, 0
expect_equal max_wait_seconds, 0

set_max_wait 3
time = time_seconds { expect_no_element_error_nowait { element(does_not_exist).visible? } }
time = time_seconds { expect_no_element_error_with_wait { element(does_not_exist).visible? } }
expect_equal time, 3
expect_equal driver.max_wait_seconds, 3
expect_equal max_wait_seconds, 3

# find by all returns [] when there are no matches.
time= time_seconds { expect_no_error { element.all(does_not_exist).to_a } }
expect_equal time, 3
expect_equal driver.max_wait_seconds, 3
expect_equal max_wait_seconds, 3

# restore default client wait for use by remaining tests
set_max_wait max_wait_seconds_default
expect_equal driver.max_wait_seconds, max_wait_seconds_default
expect_equal max_wait_seconds, max_wait_seconds_default
end

it 'set_max_page_wait' do
good_url = lambda { time_seconds { expect_no_error { protractor.get angular_website } } }
bad_url = lambda { time_seconds { expect_error { protractor.get 'http://doesnotexist' } } }

# When ignoring sync, bad url get should not error
expect_no_error { protractor.get 'http://doesnotexist' }

# we need to sync for max_page_wait to be respected
protractor.ignore_sync = false

# Gets website successfully with 0 wait.
set_max_page_wait 0
time = good_url.call
expect_equal time, 0
expect_equal max_page_wait_seconds, 0

# Gets website successfully with 3 wait.
set_max_page_wait 0
time = good_url.call
expect_equal time, 0
expect_equal max_page_wait_seconds, 0

# Successfully waits and errors on invalid website
set_max_page_wait 3
time = bad_url.call
expect_equal time, 3
expect_equal max_page_wait_seconds, 3

# Successfully waits and errors on invalid website
set_max_page_wait 0
time = bad_url.call
expect_equal time, 0
expect_equal max_page_wait_seconds, 0

# restore for use by remaining tests
set_max_page_wait max_page_wait_seconds_default
expect_equal max_page_wait_seconds, max_page_wait_seconds_default
end

describe 'element' do
Expand Down
8 changes: 5 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ def browser_name

driver.manage.timeouts.page_load = _60_seconds

driver.manage.timeouts.implicit_wait = 0
raise 'incorrect driver wait seconds default' unless driver.max_wait_seconds == 0
driver.manage.timeouts.implicit_wait = 0

# check protractor wait defaults
expect(driver.max_wait_seconds).to eq(0)
expect(driver.max_page_wait_seconds).to eq(30)

# sometimes elements just don't exist even though the page has loaded
# and wait for angular has succeeded. in these situations, use client wait.
Expand All @@ -70,7 +73,6 @@ def browser_name
driver.set_max_wait max_wait_seconds_default # seconds
end


config.after(:all) do
driver.quit rescue nil
end
Expand Down

0 comments on commit 449c605

Please sign in to comment.