Method: Protractor#initialize

Defined in:
lib/angular_webdriver/protractor/protractor.rb

#initialize(opts = {}) ⇒ Protractor

Creates a new protractor instance and dynamically patches the provided driver.

Parameters:

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

    the options to initialize with

Options Hash (opts):

  • :watir (Watir::Browser)

    the watir instance used for automation

  • :root_element (String)

    the root element on which to find Angular

  • :ignore_sync (Boolean)

    if true, Protractor won’t auto sync the page



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/angular_webdriver/protractor/protractor.rb', line 239

def initialize opts={}
  @watir = opts[:watir]

  valid_watir = defined?(Watir::Browser) && @watir.is_a?(Watir::Browser)
  raise "Driver must be a Watir::Browser not #{@driver.class}" unless valid_watir
  @driver = @watir.driver

  unless Selenium::WebDriver::SearchContext::FINDERS.keys.include?(NEW_FINDERS_KEYS)
    Selenium::WebDriver::SearchContext::FINDERS.merge!(NEW_FINDERS_HASH)
  end

  unless Watir::ElementLocator::WD_FINDERS.include? NEW_FINDERS_KEYS
    old = Watir::ElementLocator::WD_FINDERS
    # avoid const redefinition warning
    Watir::ElementLocator.send :remove_const, :WD_FINDERS
    Watir::ElementLocator.send :const_set, :WD_FINDERS, old + NEW_FINDERS_KEYS
  end

  @driver.protractor = self

  # 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)

  @client_side_scripts = ClientSideScripts

  browser_name = driver.capabilities[:browser_name].to_s.strip
  @reset_url   = reset_url_for_browser browser_name

  @base_url          = opts.fetch(:base_url, nil)

  # must be local var for use with define element below.
  protractor_element = AngularWebdriver::ProtractorElement.new @watir

  # Top level element method to enable protractor syntax.
  # redefine element to point to the new protractor element instance.
  #
  # toplevel self enables by/element from within pry. rspec helpers enables
  # by/element within rspec tests when used with install_rspec_helpers.
  [eval('self', TOPLEVEL_BINDING), AngularWebdriver::RSpecHelpers].each do |obj|
    obj.send :define_singleton_method, :element do |*args|
      protractor_element.element *args
    end

    obj.send :define_singleton_method, :by do
      AngularWebdriver::By
    end
  end

  self
end