Class: Protractor

Inherits:
Object
  • Object
show all
Defined in:
lib/angular_webdriver/protractor/protractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Protractor

Returns a new instance of Protractor.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to initialize with

Options Hash (opts):

  • :root_element (String)

    the root element on which to find Angular

  • :ignore_sync (Boolean)

    if true, Protractor won’t auto sync the page



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/angular_webdriver/protractor/protractor.rb', line 16

def initialize opts={}
  @driver = opts[:driver]
  raise 'Must supply Selenium::WebDriver' unless @driver

  watir = defined?(Watir::Browser) && @driver.is_a?(Watir::Browser)
  @driver = watir ? @driver.driver : @driver

  # 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]
  @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
  # 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 [Boolean]
  @ignore_sync  = !!opts.fetch(:ignore_sync, false)

  scripts_file = File.expand_path '../clientSideScripts.json', __FILE__
  @client_side_scripts = OpenStruct.new JSON.parse File.read scripts_file
end

Instance Attribute Details

#client_side_scriptsObject (readonly)

Returns the value of attribute client_side_scripts.



11
12
13
# File 'lib/angular_webdriver/protractor/protractor.rb', line 11

def client_side_scripts
  @client_side_scripts
end

#driverObject (readonly)

Returns the value of attribute driver.



11
12
13
# File 'lib/angular_webdriver/protractor/protractor.rb', line 11

def driver
  @driver
end

#ignore_syncObject

code/comments from protractor/lib/protractor.js



9
10
11
# File 'lib/angular_webdriver/protractor/protractor.rb', line 9

def ignore_sync
  @ignore_sync
end

#root_elementObject

code/comments from protractor/lib/protractor.js



9
10
11
# File 'lib/angular_webdriver/protractor/protractor.rb', line 9

def root_element
  @root_element
end

Instance Method Details

#executeAsyncScript_(script, description, args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/angular_webdriver/protractor/protractor.rb', line 66

def executeAsyncScript_ script, description, args
  # ensure description is exactly one line that ends in a newline
  description = description ? '// ' + description.split.join(' ') : ''
  description = description.strip + "\n"

  # add description as comment to script so it shows up in server logs
  script      = description + script

  # puts "Evaluating:\n#{script}"

  driver.execute_async_script script, args
end

#waitForAngular(opt_description = '') ⇒ WebDriver::Element, ...

Instruct webdriver to wait until Angular has finished rendering and has no outstanding $http or $timeout calls before continuing. Note that Protractor automatically applies this command before every WebDriver action.

Parameters:

  • opt_description (String) (defaults to: '')

    An optional description to be added to webdriver logs.

Returns:

  • (WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/angular_webdriver/protractor/protractor.rb', line 51

def waitForAngular opt_description='' # Protractor.prototype.waitForAngular
  return if ignore_sync

  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.waitForAngular,
                        "Protractor.waitForAngular() #{opt_description}",
                        root_element)
      raise Selenium::WebDriver::Error::JavascriptError, error if error
  rescue Exception => e
    raise e.class, "Error while waiting for Protractor to sync with the page: #{e}"
  end
end