Class: Isomorfeus::Puppetmaster::Document

Inherits:
Object
  • Object
show all
Extended by:
SelfForwardable
Defined in:
lib/isomorfeus/puppetmaster/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SelfForwardable

extended

Constructor Details

#initialize(driver, handle, response) ⇒ Document

Returns a new instance of Document.



64
65
66
67
68
69
# File 'lib/isomorfeus/puppetmaster/document.rb', line 64

def initialize(driver, handle, response)
  @driver = driver
  @handle = handle
  @response = response
  ObjectSpace.define_finalizer(self, @driver.class.document_handle_disposer(@driver, @handle))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/isomorfeus/puppetmaster/document.rb', line 212

def method_missing(name, *args)
  method_name = name.to_s
  if method_name.start_with?('find_by_')
    what = method_name[8..-1]
    return find("[#{what}=\"#{args.first}\"]") if %w[name type value].include?(what)
    return find_xpath("//*[text()=\"#{args.first}\"]") if what == 'content'
  elsif method_name.start_with?('has_')
    #       :has_checked_field?, #

    #       :has_content?,

    #       :has_css?,

    #       :has_field?,

    #       :has_link?,

    #       :has_select?,

    #       :has_selector?,

    #       :has_table?,

    #       :has_text?,

    #       :has_unchecked_field?,

    #       :has_xpath?,

    # :has_button?, # method_missing

  end
  super(name, *args)
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



62
63
64
# File 'lib/isomorfeus/puppetmaster/document.rb', line 62

def handle
  @handle
end

#responseObject (readonly)

Returns the value of attribute response.



62
63
64
# File 'lib/isomorfeus/puppetmaster/document.rb', line 62

def response
  @response
end

Instance Method Details

#await_ruby(ruby_source = '', &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/isomorfeus/puppetmaster/document.rb', line 75

def await_ruby(ruby_source = '', &block)
  ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  ruby_source = "#{ruby_source}.then { |result| $promise_result = result; $promise_resolved = true }.fail { |result| $promise_result = result; $promise_resolved = true }"
  compiled_ruby = compile_ruby_source(ruby_source)
  if compiled_ruby.start_with?('/*')
    start_of_code = compiled_ruby.index('*/') + 3
    compiled_ruby = compiled_ruby[start_of_code..-1]
  end
  evaluate_script "    (function(){\n      fun = function() {\n        if (Opal) {\n          Opal.gvars.promise_resolved = false;\n          Opal.await_ruby_exception = null;\n          try {\n            return \#{compiled_ruby}\n          } catch (e) {\n            Opal.await_ruby_exception = e;\n            Opal.gvars.promise_resolved = true;\n          }\n        } else {\n          setTimeout(fun, 100);\n        }\n      }\n      fun();\n    })()\n  JAVASCRIPT\n  have_result = false\n  start = Time.now\n  until have_result do\n    raise \"await_ruby: execution timed out! Is Opal available?\" if (Time.now - start) > 30\n    have_result = evaluate_script 'Opal ? Opal.gvars.promise_resolved : null'\n    sleep 0.1 unless have_result\n  end\n  result, exception = execute_script <<~JAVASCRIPT\n    var result;\n    var exception = false;\n    if (Opal.await_ruby_exception) {\n      var e = Opal.await_ruby_exception;\n      exception = { message: e.message, name: e.name, stack: e.stack }\n    } else if (Opal.gvars.promise_result['$respond_to?']('is_a?') && Opal.gvars.promise_result['$is_a?'](Opal.Exception)) {\n      let r = Opal.gvars.promise_result;\n      exception = { message: r.$message(), name: r.$class().$name(), stack: r.$backtrace() }\n    } else if (Opal.gvars.promise_result['$respond_to?']('to_n')) { \n      result = Opal.gvars.promise_result.$to_n()\n    } else { result = Opal.gvars.promise_result };\n    delete Opal.gvars.promise_result;\n    delete Opal.gvars.promise_resolved;\n    return [result, exception];\n  JAVASCRIPT\n  if exception\n    e = Isomorfeus::Puppetmaster::Error.new(\"\#{exception['name']}: \#{exception['message']}\")\n    e.set_backtrace(exception['stack'])\n    raise e\n  end\n  result\nend\n"

#browserObject



71
72
73
# File 'lib/isomorfeus/puppetmaster/document.rb', line 71

def browser
  @driver.browser
end

#evaluate_ruby(ruby_source = '', &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/isomorfeus/puppetmaster/document.rb', line 133

def evaluate_ruby(ruby_source = '', &block)
  ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  compiled_ruby = compile_ruby_source(ruby_source)
  if compiled_ruby.start_with?('/*')
    start_of_code = compiled_ruby.index('*/') + 3
    compiled_ruby = compiled_ruby[start_of_code..-1]
  end
  evaluate_script "    (function(){\n      return \#{compiled_ruby}\n    })()\n  JAVASCRIPT\nend\n"

#evaluate_with_opal(ruby_source = '', &block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/isomorfeus/puppetmaster/document.rb', line 147

def evaluate_with_opal(ruby_source = '', &block)
  ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  compiled_ruby = compile_ruby_source(ruby_source)
  if compiled_ruby.start_with?('/*')
    start_of_code = compiled_ruby.index('*/') + 3
    compiled_ruby = compiled_ruby[start_of_code..-1]
  end
  evaluate_script "    (function(){\n      if (typeof Opal === \"undefined\") {\n        \#{Isomorfeus::Puppetmaster.opal_prelude}\n      }\n      return \#{compiled_ruby}\n    })()\n  JAVASCRIPT\nend\n"

#go_backObject



164
165
166
167
# File 'lib/isomorfeus/puppetmaster/document.rb', line 164

def go_back
  @response = @driver.document_go_back(self)
  self
end

#go_forwardObject



169
170
171
172
# File 'lib/isomorfeus/puppetmaster/document.rb', line 169

def go_forward
  @response = @driver.document_go_forward(self)
  self
end

#goto(uri) ⇒ Object Also known as: visit



174
175
176
177
# File 'lib/isomorfeus/puppetmaster/document.rb', line 174

def goto(uri)
  @response = @driver.document_goto(self, uri)
  self
end

#has_content?(content, **options) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/isomorfeus/puppetmaster/document.rb', line 180

def has_content?(content, **options)
  body.has_content?(content, **options)
end

#has_css?(selector, **options) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/isomorfeus/puppetmaster/document.rb', line 184

def has_css?(selector, **options)
  body.has_css?(selector, **options)
end

#has_current_path?(other_path) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/isomorfeus/puppetmaster/document.rb', line 188

def has_current_path?(other_path)
  path == other_path
end

#has_text?(text, **options) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/isomorfeus/puppetmaster/document.rb', line 192

def has_text?(text, **options)
  body.has_text?(text, **options)
end

#has_xpath?(query, **options) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/isomorfeus/puppetmaster/document.rb', line 196

def has_xpath?(query, **options)
  body.has_xpath?(query, **options)
end

#isomorphic(ruby_source = '', &block) ⇒ Object



200
201
202
203
204
# File 'lib/isomorfeus/puppetmaster/document.rb', line 200

def isomorphic(ruby_source = '', &block)
  ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  Isomorfeus::Puppetmaster.served_app.on_server(ruby_source)
  evaluate_ruby(ruby_source)
end

#isomorphic_with_opal(ruby_source = '', &block) ⇒ Object



206
207
208
209
210
# File 'lib/isomorfeus/puppetmaster/document.rb', line 206

def isomorphic_with_opal(ruby_source = '', &block)
  ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  Isomorfeus::Puppetmaster.served_app.on_server(ruby_source)
  evaluate_with_opal(ruby_source)
end

#open_document_by(&block) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/isomorfeus/puppetmaster/document.rb', line 235

def open_document_by(&block)
  open_documents = @driver.document_handles
  block.call
  new_documents = @driver.document_handles - open_documents
  raise 'Multiple documents opened' if new_documents.size > 1
  raise 'No window opened' if new_documents.size < 1
  Isomorfeus::Puppetmaster::Document.new(@driver, new_documents.first, Isomorfeus::Puppetmaster::Response.new)
end

#pathObject



244
245
246
# File 'lib/isomorfeus/puppetmaster/document.rb', line 244

def path
  URI.parse(url).path
end

#reloadObject



248
249
250
# File 'lib/isomorfeus/puppetmaster/document.rb', line 248

def reload
  @response = @driver.reload(self)
end

#respond_to?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
255
# File 'lib/isomorfeus/puppetmaster/document.rb', line 252

def respond_to?(name, include_private = false)
  return true if i[find_by_content find_by_name find_by_type find_by_value].include?(name)
  super(name, include_private)
end