Skip to content

Commit

Permalink
Add more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bootstraponline committed May 21, 2015
1 parent 0ac13fc commit 2bc9785
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 32 deletions.
38 changes: 21 additions & 17 deletions lib/angular_webdriver/protractor/protractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@

class Protractor
# code/comments from protractor/lib/protractor.js
attr_accessor :root_element, :ignore_sync

attr_reader :client_side_scripts, :driver, :reset_url, :base_url

# The css selector for an element on which to find Angular. This is usually
# 'body' but if your ng-app is on a subsection of the page it may be
# a subelement.
#
# @return [String]
def self.root_element
@@root_element
end
attr_accessor :root_element

# The css selector for an element on which to find Angular. This is usually
# 'body' but if your ng-app is on a subsection of the page it may be
# a subelement.
# If true, Protractor will not attempt to synchronize with the page before
# performing actions. This can be harmful because Protractor will not wait
# until $timeouts and $http calls have been processed, which can cause
# tests to become flaky. This should be used only when necessary, such as
# when a page continuously polls an API using $timeout.
#
# @return [String]
def root_element
@@root_element
end
# @return [Boolean]
attr_accessor :ignore_sync

attr_reader :client_side_scripts, :driver, :reset_url, :base_url

# @see webdriver.WebDriver.get
#
Expand All @@ -53,8 +50,13 @@ def get destination, opt_timeout=30
raise "Invalid destination #{destination}"
end

destination = base_url.scheme == 'file' ?
base_url + destination : URI.join(base_url, destination)
destination = if base_url.scheme == 'file'
base_url + destination
else
# data urls won't parse successfully by URI
# so return destination directly in that case.
URI.join(base_url, destination) rescue destination
end

# destination must be string
# no implicit conversion of URI::HTTP into String
Expand All @@ -71,7 +73,9 @@ def get destination, opt_timeout=30

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

# now that the url has changed, make sure Angular has loaded
Expand Down Expand Up @@ -158,7 +162,7 @@ def initialize opts={}
# a subelement.
#
# @return [String]
@@root_element = opts.fetch :root_element, 'body'
@root_element = opts.fetch :root_element, 'body'

# If true, Protractor will not attempt to synchronize with the page before
# performing actions. This can be harmful because Protractor will not wait
Expand Down
113 changes: 98 additions & 15 deletions spec/protractor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
require_relative 'spec_helper'

=begin
Each test runs in the same browser instance.
The test must initialize any required state at the start (it block)
After each test the page is reset and ignore_sync is set to false. (rspec before)
=end

describe 'client side scripts' do

before do
before(:all) do
# remote driver is useful for debugging
# @driver = Selenium::WebDriver.for :remote, desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox
# driver = Selenium::WebDriver.for :remote, desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox
@driver = Selenium::WebDriver.for :firefox
raise 'Driver is nil!' unless @driver
raise 'Driver is nil!' unless driver

# Must activate protractor before any driver commands
@protractor = Protractor.new driver: @driver
@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
driver.manage.timeouts.script_timeout = 60 # seconds
end

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

before do
protractor.ignore_sync = false
protractor.driver_get protractor.reset_url
end

# requires angular's test app to be running
Expand All @@ -22,37 +38,104 @@ def angular_website
end

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

after do
@driver.quit rescue nil
def protractor
@protractor
end

def driver
@driver
end

def angular_not_found_error
error_class = Selenium::WebDriver::Error::JavascriptError
error_message = /angular could not be found on the window/
[error_class, error_message]
end

it 'root_element' do
expect(protractor.root_element).to eq('body')

a_div = 'a.div'
protractor.root_element = a_div
expect(protractor.root_element).to eq(a_div)

protractor.root_element = 'body'
expect(protractor.root_element).to eq('body')
end

it 'get' do
# should raise error when loading blank page and ignore sync is false
expect { driver.get protractor.reset_url }.to raise_error(*angular_not_found_error)

# should not raise error when loading blank page and ignore sync is true
protractor.ignore_sync = true
expect { driver.get protractor.reset_url }.to_not raise_error

end

it 'driver_get' do
# should never raise error since driver_get will never sync
# (even when ignore sync is false)
expect { protractor.driver_get protractor.reset_url }.to_not raise_error
end

it 'refresh' do
# should raise error when loading blank page and ignore sync is false
expect { driver.navigate.refresh }.to raise_error(*angular_not_found_error)

# should not raise error when loading blank page and ignore sync is true
protractor.ignore_sync = true
expect { driver.navigate.refresh }.to_not raise_error
end

=begin
todo: write tests for:
setLocation
getLocationAbsUrl
initialize
sync
waitForAngular
_js_comment
executeAsyncScript_
executeScript_
debugger
driver
driver
root_element
ignore_sync
client_side_scripts
reset_url
reset_url
base_url
=end

it 'finds by bindings' do
visit 'async'

eles = @driver.find_elements(:binding, 'slowHttpStatus')
eles = driver.find_elements(:binding, 'slowHttpStatus')
result = eles.first.is_a? Selenium::WebDriver::Element
expect(result).to eq(true)


ele = @driver.find_element(:binding, 'slowHttpStatus')
ele = driver.find_element(:binding, 'slowHttpStatus')
result = ele.is_a? Selenium::WebDriver::Element
expect(result).to eq(true)

expect { @driver.find_element(:binding, "doesn't exist") }.to raise_error(Selenium::WebDriver::Error::NoSuchElementError)
expect { driver.find_element(:binding, "doesn't exist") }.to raise_error(Selenium::WebDriver::Error::NoSuchElementError)
end

it 'waitForAngular should error on non-angular pages' do
error_class = Selenium::WebDriver::Error::JavascriptError
error_message = /angular could not be found on the window/
expect { @protractor.waitForAngular }.to raise_error(error_class, error_message)
# ignore sync is false and we're on the reset page
expect { protractor.waitForAngular }.to raise_error(*angular_not_found_error)
end

it 'waitForAngular should succeed on angular pages with wait' do
visit 'async'

wait(timeout: 15) { @protractor.waitForAngular }
wait(timeout: 15) { protractor.waitForAngular }
end
end

0 comments on commit 2bc9785

Please sign in to comment.