Class: Angular::Setup

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/angular/setup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#logger

Constructor Details

#initialize(page) ⇒ Setup

Returns a new instance of Setup.



7
8
9
# File 'lib/angular/setup.rb', line 7

def initialize(page)
  @page = page
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



5
6
7
# File 'lib/angular/setup.rb', line 5

def page
  @page
end

Instance Method Details

#angular_app?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
# File 'lib/angular/setup.rb', line 89

def angular_app?
  begin
    js = "(typeof angular !== 'undefined') && "
    js += "angular.element(document.querySelector('[ng-app], [data-ng-app]')).length > 0"
    @page.evaluate_script js
  rescue Capybara::NotSupportedByDriverError
    false
  end
end

#get_nodes_2(method, params, opt = {}) ⇒ Object

  • :root_selector

  • :wait

Parameters:

  • opt (defaults to: {})

Raises:



25
26
27
28
29
30
31
32
33
# File 'lib/angular/setup.rb', line 25

def get_nodes_2(method, params, opt = {})
  opt = {
    using: nil,
    root_selector: ::Angular.root_selector,
  }.merge(opt)
  ids = make_call(method, params, opt)
 raise NotFound.new("#{method}: #{params} - #{opt.inspect}") if ids.nil? || ids.empty?
  make_nodes(ids, opt)
end

#installObject



107
108
109
110
111
112
113
114
# File 'lib/angular/setup.rb', line 107

def install
  if page_reloaded?
    ClientScript.window_scripts.each do |f|
      @page.evaluate_script(f)
    end
    mark_installed
  end
end

#make_call(method, params, opt = {}) ⇒ Object

  • :root_selector

  • :wait

Parameters:

  • opt (defaults to: {})

Returns:

  • result from page.evaluate_script



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/angular/setup.rb', line 61

def make_call(method, params, opt = {})
  opt = {
    wait: true,
  }.merge(opt)

  ng_wait if opt[:wait]

  params << opt[:using] if opt.has_key?(:using)
  params << opt[:root_selector] if opt.has_key?(:root_selector)
  js_params = params.map do |p|
    if p.nil?
      'null'
    elsif p.is_a? String
      escaped = p.gsub(/'/, "\\\\'").gsub(/"/, '\\\\"')
      "'#{escaped}'"
    else
      p.to_s
    end
  end

  js = "#{method}(#{js_params.join(', ')});"
  logger.debug js
  js_result = page.evaluate_script(js);
#      logger.debug js_result

  js_result
end

#make_nodes(ids, opt) ⇒ Object

Find nodes matching ‘capybara-ng-match’ attributes using ids

Returns:

  • nodes matching ids



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/angular/setup.rb', line 40

def make_nodes(ids, opt)
  result = []
  ids.each do |id|
    id = id.tr('"', '')
    selector = "//*[@capybara-ng-match='#{id}']"
    nodes = page.driver.find_xpath(selector)

    raise NotFound.new("Failed to match found id to node") if nodes.empty?
    result.concat(nodes)
  end
  page.evaluate_script("clearCapybaraNgMatches('#{opt[:root_selector]}')");
  result
end

#mark_installedObject



103
104
105
# File 'lib/angular/setup.rb', line 103

def mark_installed
  @page.evaluate_script("window.ngInstalled = true")
end

#ng_waitObject



11
12
13
14
# File 'lib/angular/setup.rb', line 11

def ng_wait
  install
  Waiter.new(self).wait_until_ready
end

#page_reloaded?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/angular/setup.rb', line 99

def page_reloaded?
  @page.evaluate_script("window.ngInstalled === undefined")
end

#row(opt) ⇒ Object



16
17
18
# File 'lib/angular/setup.rb', line 16

def row(opt)
  opt.has_key?(:row) ? opt[:row] : 0
end