Skip to content

Commit

Permalink
Fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bootstraponline committed May 21, 2015
1 parent 2230374 commit a4bad05
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
1 change: 1 addition & 0 deletions angular_webdriver.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'appium_thor', '>= 0.0.7'
s.add_development_dependency 'pry', '>= 0.10.1'
s.add_development_dependency 'webdriver_utils', '>= 0.0.3'
s.add_development_dependency 'trace_files', '~> 0.0.2'

s.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(gen|test|spec|features|protractor)/})
Expand Down
35 changes: 28 additions & 7 deletions lib/angular_webdriver/protractor/protractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,36 @@ def root_element
#
# @param destination [String] The destination URL to load, can be relative if base_url is set
# @param opt_timeout [Integer] Number of seconds to wait for Angular to start. Default 30.
def get destination, opt_timeout
timeout = opt_timeout ? opt_timeout : 30
def get destination, opt_timeout=30
# do not use driver.get because that redirects to this method
# instead driver_get is provided.

destination = base_url.start_with?('file://') ?
timeout = opt_timeout

raise "Invalid timeout #{timeout}" unless timeout.is_a?(Numeric)

unless destination.is_a?(String) || destination.is_a?(URI)
raise "Invalid destination #{destination}"
end

destination = base_url.scheme == 'file' ?
base_url + destination : URI.join(base_url, destination)

# destination must be string
# no implicit conversion of URI::HTTP into String
destination = destination.to_s

msg = lambda { |str| 'Protractor.get(' + destination + ') - ' + str }

return driver.get(destination) if ignore_sync
return driver_get(destination) if ignore_sync

driver.get(reset_url)
driver_get(reset_url)
executeScript_(
'window.location.replace("' + destination + '");',
msg.call('reset url'))

wait(timeout) do
url = executeScript_('return window.location.href;', msg('get url'))
url = executeScript_('return window.location.href;', msg.call('get url'))
raise 'still on reset url' unless url != reset_url
end

Expand All @@ -66,6 +79,12 @@ def get destination, opt_timeout
waitForAngular
end

# Invokes the underlying driver.get. Does not wait for angular.
# Does not use base_url or reset_url logic.
def driver_get url
driver.bridge.driver_get url
end

# @see webdriver.WebDriver.refresh
#
# Makes a full reload of the current page and loads mock modules before
Expand Down Expand Up @@ -168,8 +187,10 @@ def initialize opts={}
def sync webdriver_command
return unless webdriver_command
webdriver_command = webdriver_command.intern
# Note get must not sync here because the get command is redirected to
# protractor.get which already has the sync logic built in.
sync_whitelist = [
:getCurrentUrl, :get, :refresh, :getPageSource,
:getCurrentUrl, :refresh, :getPageSource,
:getTitle, :findElement, :findElements,
:findChildElement, :findChildElements,
:setLocation
Expand Down
30 changes: 29 additions & 1 deletion lib/angular_webdriver/protractor/webdriver_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def protractor
def protractor= protractor_object
@bridge.protractor = protractor_object
end

def bridge
@bridge
end
end

module Remote
Expand Down Expand Up @@ -78,6 +82,17 @@ def find_elements_by(how, what, parent = nil)
ids.map { |id| Element.new self, element_id_from(id) }
end

#
# driver.get uses execute which invokes protractor.get
# protractor.get needs to call driver.get without invoking
# protractor.get.
#
# this is accomplished by using driver_get and raw_execute
#

def driver_get(url)
raw_execute(:get, {}, :url => url)['value']
end

#
# executes a command on the remote server.
Expand All @@ -87,7 +102,20 @@ def find_elements_by(how, what, parent = nil)
#

def execute(*args)
protractor.sync(args.first) unless protractor.ignore_sync
raise 'Must initialize protractor' unless protractor
unless protractor.ignore_sync
# override get method which has special sync logic
# (not handled via sync method)

method_symbol = args.first

if method_symbol == :get
url = args.last[:url]
return protractor.get url
end

protractor.sync method_symbol
end

raw_execute(*args)['value']
end
Expand Down
12 changes: 6 additions & 6 deletions spec/protractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
@driver = Selenium::WebDriver.for :firefox
raise 'Driver is nil!' unless @driver

# Must activate protractor before any driver commands
@protractor = Protractor.new driver: @driver

# set script timeout for protractor client side javascript
# https://github.com/angular/protractor/issues/117
@driver.manage.timeouts.script_timeout = 60 # seconds

@protractor = Protractor.new driver: @driver
end

# requires angular's test app to be running
def angular_website
'http://localhost:8081/#/'
'http://localhost:8081/#/'.freeze
end

def visit page
def visit page=''
@driver.get angular_website + page
@protractor.waitForAngular
end

after do
Expand Down Expand Up @@ -51,7 +51,7 @@ def visit page
end

it 'waitForAngular should succeed on angular pages with wait' do
@driver.get angular_website
visit 'async'

wait(timeout: 15) { @protractor.waitForAngular }
end
Expand Down
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@ def symbolize_keys(hash)
end
result
end

# -- Trace

trace = false

if trace
require 'trace_files'

targets = Dir.glob(File.join(__dir__, '../lib/**/*.rb'))
targets.map! { |t| File.expand_path t }
puts "Tracing: #{targets}"

TraceFiles.set trace: targets
end

0 comments on commit a4bad05

Please sign in to comment.