Class: Appmodel::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/appmodel/model/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f = nil) ⇒ Model

Returns a new instance of Model.



10
11
12
13
14
15
16
17
18
19
# File 'lib/appmodel/model/model.rb', line 10

def initialize(f=nil)

  @logger = Logging.logger(STDOUT)
  @logger.level = :warn

  unless f.nil?
    @_file=f
    loadPages(@_file)
  end
end

Instance Attribute Details

#_fileObject

Returns the value of attribute _file.



7
8
9
# File 'lib/appmodel/model/model.rb', line 7

def _file
  @_file
end

#app_modelObject

Returns the value of attribute app_model.



8
9
10
# File 'lib/appmodel/model/model.rb', line 8

def app_model
  @app_model
end

Class Method Details

.isPageObject?(_locator) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/appmodel/model/model.rb', line 91

def self.isPageObject?(_locator)
  rc = false
  if _locator.is_a?(String)
    if _locator.match(/^\s*page\s*\(\s*[\w\d_\-]+\s*\)/i)
      rc=true
    elsif _locator.match(/^\s*frame\(.*\)\.locator\(.*\)\s*$/)
      rc=true
    end

  elsif _locator.is_a?(Hash)
    rc = _locator.has_key?('locator') || _locator.has_key?(:locator)
  end

  rc
end

.parseLocator(_locator) ⇒ Object

_a : frame(xyz).frame(123), <locator>



50
51
52
53
54
55
56
57
58
59
60
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
88
# File 'lib/appmodel/model/model.rb', line 50

def self.parseLocator(_locator)
  hit=nil

  if _locator.is_a?(String)

    rc=_locator.match(/^\s*([([fF]rame\([^\(]*?\)\.)]*)\.([lL]ocator\((.*)\))\s*$/)

    if rc
      hit = { 'frame' => rc[1], 'locator' => rc[3] }

    elsif _locator.match(/^\s*\//)
      hit = { 'locator' => _locator.match(/^\s*(.*)\s*$/)[1] }

    elsif _locator.match(/^\s*(css|CSS)\s*=\s*(.*)\s*$/)
     # hit = { 'locator' => _locator.match(/^\s*(css|CSS)\s*=\s*(.*)\s*$/)[2] }
      hit = { 'locator' => _locator }

    elsif _locator.match(/^\s*[lL]ocator\((.*)\)\s*$/)
      hit = { 'locator' => _locator.match(/^\s*[lL]ocator\((.*)\)\s*$/)[1] }
    elsif _locator.match(/^\s*([([fF]rame\([^\(]*?\)\.)]*)\.([lL]ocator\((.*)\))\s*$/)
      rc=_locator.match(/^\s*([([fF]rame\([^\(]*?\)\.)]*)\.([lL]ocator\((.*)\))\s*$/)

      if rc
        hit = { 'frame' => rc[1], 'locator' => rc[2]}
      end
    else
      hit = { 'locator' => _locator }
    end

  elsif _locator.is_a?(Hash) && _locator.has_key?('locator')
    hit = { 'locator' => _locator['locator']}
    if _locator.has_key?('frame')
      hit['frame'] = _locator['frame']
    end
  end

  hit

end

.toBy(_target, appModel = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/appmodel/model/model.rb', line 21

def self.toBy(_target, appModel=nil)
  _by=:xpath
  _with = nil
  _locator = !_target.nil? ? _target.clone : nil

  if Appmodel::Model.isPageObject?(_locator) && !appModel.nil?
    _pg = appModel.getPageElement(_locator)
    if !_pg.nil?
      _locator = _pg['locator']
    else
      _locator=nil
    end
  end

  if _locator.is_a?(String)
    if _locator.match(/^\s*css\s*=/)
      _with = _locator.match(/^\s*css\s*=(.*)\s*$/)[1]
      seleniumLocator = { :css => _with }
    elsif _locator.match(/^\s*\//)
      seleniumLocator = { :xpath => _locator }
    elsif _locator.match(/^\s*\.\//)
      seleniumLocator = { :xpath => _locator }
    end
  end

  seleniumLocator
end

Instance Method Details

#flattenPageObject(h, path = "") ⇒ Object



272
273
274
275
276
277
278
279
# File 'lib/appmodel/model/model.rb', line 272

def flattenPageObject(h, path="")
  rc=iterate(h, path)
  if rc.is_a?(Array)
    return rc[0]
  end

  nil
end

#getAppModelObject



109
110
111
# File 'lib/appmodel/model/model.rb', line 109

def getAppModel()
  @app_model
end

#getPageElement(s) ⇒ Object

getPageElement(“page(login).get(login_form).get(button)”)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/appmodel/model/model.rb', line 141

def getPageElement(s)
  @logger.debug __FILE__ + (__LINE__).to_s + " getPageElement(#{s})"

  if s.match(/^\s*\//) || s.match(/^\s*css\s*=/i) || s.match(/^\s*#/)
    @logger.debug __FILE__ + (__LINE__).to_s + " getPageElement(#{s} return nil"
    return nil
  end

  hit = @app_model

  nodes = s.split(/\./)

  if nodes
    nodes.each { |elt|

      @logger.debug __FILE__ + (__LINE__).to_s + " process #{elt}"
      getter = elt.split(/\(/)[0]
      _obj = elt.match(/\((.*)\)/)[1]

      @logger.debug __FILE__ + (__LINE__).to_s + " getter : #{getter}  obj: #{_obj}"

      if getter.downcase.match(/(page|pg)/)
        @logger.debug __FILE__ + (__LINE__).to_s + " -- process page --"
        hit=@app_model[_obj]
      elsif getter.downcase=='get'

        if !hit.nil? && hit.has_key?(_obj)
          hit=hit[_obj]

          if hit.is_a?(String) && hit.match(/\s*(file)\((.*)\)/)
            @logger.debug __FILE__ + (__LINE__).to_s + "  LOAD Sub pageObject: #{hit}"
          end

        else
          @logger.debug __FILE__ + (__LINE__).to_s + " Missing getter : #{_obj.to_s}"
          return nil
        end

      else
        @logger.debug __FILE__ + (__LINE__).to_s + " getter : #{getter} is unknown."
        return nil
      end
      @logger.debug __FILE__ + (__LINE__).to_s + " HIT => #{hit}"
    }
  end


  hit

end

#hits(parent, h, condition, _action, pg) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
# File 'lib/appmodel/model/model.rb', line 202

def hits(parent, h, condition, _action, pg)
  #  @logger.debug __FILE__ + (__LINE__).to_s + " collect_item_attributes(#{h})"
  result = []

  if h.is_a?(Hash)

    h.each do |k, v|
      @logger.debug __FILE__ + (__LINE__).to_s + " Key: #{k} => #{v}"
      if k == condition
        #  h[k].each {|k, v| result[k] = v } # <= tweak here
        if !v.is_a?(Array) && v.match(/^\s*#{_action}\s*\((.*)\)\s*$/i)

          pageObject=v.match(/^\s*#{_action}\s*\((.*)\)\s*$/i)[1]

          @logger.debug __FILE__ + (__LINE__).to_s + " <pg, pageObject> : <#{pg}, #{pageObject}>"

          if pg.nil?
            result << parent
          elsif pg == pageObject
            result << parent
          end

        elsif v.is_a?(Array)

          v.each do |vh|
            @logger.debug " =====> #{vh}"

            if vh.is_a?(Hash) && vh.has_key?(condition) && vh[condition].match(/^\s*#{_action}\s*/i)

              pageObject=vh[condition].match(/^\s*#{_action}\s*\((.*)\)\s*$/i)[1]


              @logger.debug __FILE__ + (__LINE__).to_s + " matched on #{_action}, pg:#{pg}, #{pageObject}"

              if pg.nil?
                result << parent
              elsif pg == pageObject
                result << parent
              end

            end

          end

        end

      elsif v.is_a? Hash
        if parent.nil?
          _rc = hits("page(#{k})", h[k], condition, _action, pg)
        else
          _rc = hits("#{parent}.get(#{k})", h[k], condition, _action, pg)
        end


        if !(_rc.nil? || _rc.empty?)
          result << _rc
          @logger.debug __FILE__ + (__LINE__).to_s + " ADDING  #{k} : #{_rc}"
          result.flatten!
        end

      end
    end

  end

  result=nil if result.empty?
  @logger.debug __FILE__ + (__LINE__).to_s + " result : #{result}"
  result
end

#itemize(condition = 'visible_when', _action = 'hover', _pgObj = nil) ⇒ Object

visible_when: hover(page(x).get(y).get(z))



195
196
197
198
199
# File 'lib/appmodel/model/model.rb', line 195

def itemize(condition='visible_when', _action='hover', _pgObj=nil)
  @results=hits(nil, @app_model, condition, _action, _pgObj)
  @logger.debug "[itemize] => #{@results}"
  @results
end

#iterate(h, path = "") ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/appmodel/model/model.rb', line 281

def iterate(h, path="")
  rc=true
  assertions=[]

  @logger.debug __FILE__ + (__LINE__).to_s + " ===== iterate(#{h}, #{path}) ====="

  if h.is_a?(String)
    @logger.debug __FILE__ + (__LINE__).to_s + " => process #{h}"
    if h.match(/^\s*(page)\s*\(.*\)\s*$/i)
      @logger.debug __FILE__ + (__LINE__).to_s + " => process Page #{h}"
      page_elt = getPageElement(h)
      @logger.debug __FILE__ + (__LINE__).to_s + " => #{page_elt}"
      assertions << iterate(page_elt, path)
    else
      @logger.debug __FILE__ + (__LINE__).to_s + " UNKNOWN: #{h}"
      assertions << "#{h} : unknown"
    end

  elsif h.is_a?(Hash) && h.has_key?('locator')

    @logger.debug __FILE__ + (__LINE__).to_s + " == add #{h} =="
    assertions << { :path => path, :data => h }

  elsif h.is_a?(Hash)

    @logger.debug __FILE__ + (__LINE__).to_s + "Keys.size: #{h.keys[0]}"

    if h.keys.size==1 && h[h.keys[0]].is_a?(Hash) && h[h.keys[0]].has_key?('locator')

      @logger.debug __FILE__ + (__LINE__).to_s + " add assertion #{h}"
      assertions << { :path => path, :data => h }

    elsif h.keys.size==1 && h[h.keys[0]].is_a?(Hash)

      _id = h.keys[0]
      @logger.debug __FILE__ + (__LINE__).to_s + " LocatorID : #{_id}"

      if true
      h[_id].each_pair { |_k, _h|

        @logger.debug __FILE__ + (__LINE__).to_s + " | id(#{_id}) => #{_k}, #{_h}"

        if _h.keys.size==1 && _h[_h.keys[0]].is_a?(Hash) && !_h[_h.keys[0]].has_key?('locator')
          @logger.debug __FILE__ + (__LINE__).to_s + " id(#{_id}) => #{_h}"
          _a = iterate(_h, "#{path}.#{_id}")
          _a.each do |_e|
            assertions << _e
          end
        elsif _h.keys.size==1 && _h[_h.keys[0]].is_a?(Hash) && _h[_h.keys[0]].has_key?('locator')
          assertions << { :path => "#{path}.#{_id}", :data => h[_h.keys[0]] }
        elsif _h.is_a?(Hash) && _h.has_key?('locator')
          assertions << { :path => "#{path}.#{_id}.#{_k}", :data => _h }
        else
          @logger.debug __FILE__ + (__LINE__).to_s + " | id(#{path}.#{_id}.#{_k}) - #{_h}"

          _h.each do |k, v|
#                @logger.debug __FILE__ + (__LINE__).to_s + " processing #{_id}.#{k}, #{v}"
            if v.is_a?(Hash) && v.has_key?('locator')
              @logger.debug __FILE__ + (__LINE__).to_s + " processing #{_id}.#{_k}.#{k}"
            elsif v.is_a?(Hash) || v.is_a?(Array)
              @logger.debug __FILE__ + (__LINE__).to_s + " processing #{_id}.#{k} - #{v}"
              _a = iterate(v, "#{path}.#{_id}.#{k}")
              _a.each do |_e|
                assertions << _e
              end
            else
              puts("Assert => k is #{k}, value is #{v}")
         #     assertions << "#{k} #{v}"
              rc=rc && true
            end
          end

          @logger.debug __FILE__ + (__LINE__).to_s + " adding assertions id(#{_id}.#{_k}) - #{_h}"
          assertions << "id(#{_id}.#{_k}) - #{_h}"
        end

      }
      end

    else

      @logger.debug __FILE__ + (__LINE__).to_s + " ** process #{h} **"
      _list=Array.new
      h.each do |k, v|
        if v.is_a?(Hash) && v.has_key?('locator')

          @logger.debug __FILE__ + (__LINE__).to_s + " add to assertion #{k} => #{v}"
          _list.push({ :path => "#{path}.get(#{k})", :dat => v })

          @logger.debug " _list ==> #{_list}"
        elsif v.is_a?(Hash) || v.is_a?(Array)

          @logger.debug __FILE__ + (__LINE__).to_s + " >>>>  #{k} => #{v} <<<<<"

          _a = iterate(v, "#{path}.get(#{k})")
          _a.each do |_e|
            assertions << _e
          end
        else
          @logger.debug(__FILE__ + (__LINE__).to_s + " k is #{k}, value is #{v}")
          assertions << "#{k} #{v}"
        end
      end

     # assertions << _list.flatten
      if !_list.empty?
        _list.each do |_e|
          assertions << _e
        end
      end

    end

  else
    @logger.debug __FILE__ + (__LINE__).to_s + " Huh?"
    assertions << nil
  end

  @logger.debug __FILE__ + (__LINE__).to_s + " assertions => #{assertions}"
  assertions

end

#loadPages(jlist) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/appmodel/model/model.rb', line 113

def loadPages(jlist)

  json_list=[]
  if jlist.kind_of?(String)
    json_list << jlist
  else
    json_list=jlist
  end

  jsonData={}
  json_list.each  { |f|
    @logger.debug __FILE__ + (__LINE__).to_s + " JSON.parse(#{f})"

    begin
      data_hash = JSON.parse File.read(f)
      jsonData.merge!(data_hash)
    rescue JSON::ParserError
      @logger.fatal "raise JSON::ParseError - #{f.to_s}"
      raise "JSONLoadError"
    end

  }
  @logger.debug "merged jsonData => " + jsonData.to_json
  @app_model = jsonData
end