Class: TDriver::OptimizedXML::TestObjectAdapter

Inherits:
Object
  • Object
show all
Extended by:
Abstraction::TestObjectAdapter
Defined in:
lib/tdriver/base/test_object/xml/adapter.rb

Class Method Summary collapse

Methods included from Abstraction::TestObjectAdapter

identify_test_object_adapter_from_data

Class Method Details

.application_layout_direction(sut) ⇒ Object

TODO: document me



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 523

def self.application_layout_direction( sut )

  # temporary fix until testobject will be associated to parent application object; add 'layoutDirection' to dynamic attributes filtering whitelist...
  TDriver::AttributeFilter.add_attribute( 'layoutDirection' ){ 

    # this block will be executed if attribute was not in filter list

    # temporary fix: ... and refresh sut to retrieve updated xml data
    sut.refresh

  }
        
  # TODO: parent application test object should be passed to get_test_objects; TestObjectAdapter#test_object_attribute( @app.xml_data, 'layoutDirection')
  #( sut.xml_data.at_xpath('*//obj[@type="application"]/attr[@name="layoutDirection"]/text()').content || 'LeftToRight' ).to_s

  ( sut.xml_data.at_xpath('.//obj[@type="application"]/attr[@name="layoutDirection"]/text()').content || 'LeftToRight' ).to_s

end

.create_child_accessors!(source_data, test_object) ⇒ Object

TODO: document me



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 543

def self.create_child_accessors!( source_data, test_object )

  # iterate through each child object type attribute and create accessor method  
  source_data.xpath( 'obj/@type' ).each{ | object_type |

    # skip if object type value is nil or empty due to child accessor cannot be created
    next if object_type.nil? || object_type.to_s.empty?

    # convert attribute node value to string
    object_type = object_type.content

    # skip if child accessor is already created 
    next if test_object.respond_to?( object_type ) 

    begin

      # create child accessor method to test object unless already exists
      test_object.instance_eval(

        "def #{ object_type }( rules = {} ); raise TypeError,'parameter <rules> should be hash' unless rules.kind_of?( Hash ); rules[ :type ]=:#{ object_type }; child( rules ); end;"

      ) unless object_type.empty?

    # in case if object type has some invalid characters, e.g. type is "ns:object"
    rescue SyntaxError
    
      warn "warning: unable to create accessor to child test object whose type is #{ object_type.inspect }"

    end

  }

end

.get_objects(source_data, rules, find_all_children) ⇒ Object

TODO: document me



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 285

def self.get_objects( source_data, rules, find_all_children )

  rule = xpath_to_object( rules, find_all_children )

  [ 

    # perform xpath to source xml data
    source_data.xpath( rule, self ),

    # return also created xpath  
    rule

  ]   

end

.get_test_object_identifiers(xml_source, test_object = nil) ⇒ Object

TODO: document me



646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 646

def self.get_test_object_identifiers( xml_source, test_object = nil )

  # retrieve parent xpath if test_object given
  parent_xpath = test_object ? test_object.instance_variable_get( :@parent ).x_path : ""

  # retrieve type attribute
  type = xml_source.attribute( 'type' )

  # retrieve id attribute
  id = xml_source.attribute( 'id' )

  # retrieve env attribute
  env = xml_source.attribute( 'env' )

  # retrieve test object element attributes and return array containting xpath to test object, name, type and id elements
  [ 
    # x_path to test object
    #test_object ? "#{ parent_xpath }/*//obj[@type='#{ type }' and @id='#{ id }']" : nil,

    test_object ? "#{ parent_xpath }/.//obj[@type='#{ type }' and @id='#{ id }']" : nil,

    # test object name 
    xml_source.attribute( 'name' ),
    
    # test object type 
    type,
    
    # test object id 
    id,

    env
    
  ]

end

.get_xml_element_for_test_object(test_object) ⇒ Object

TODO: document me



632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 632

def self.get_xml_element_for_test_object( test_object )
  
  # retrieve nodeset from sut xml_data
  nodeset = test_object.instance_variable_get( :@sut ).xml_data.xpath( test_object.instance_variable_get( :@x_path ) )

  # raise exception if no test objects found 
			  raise MobyBase::TestObjectNotFoundError if nodeset.empty?
			
  # return first test object from the nodeset
			  nodeset.first

end

.hash_to_element_attributes(hash) ⇒ Object

TODO: document me



683
684
685
686
687
688
689
690
691
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 683

def self.hash_to_element_attributes( hash )

  hash.collect{ | key, value | 

    "#{ key.to_s }=\"#{ value.to_s }\"" 

  }.join(' ')

end

.list_test_objects_as_string(source_data) ⇒ Object

TODO: document me



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 742

def self.list_test_objects_as_string( source_data )

  source_data.collect{ | object |
      
    path = [ object.attribute( 'type' ) ]

    while object.attribute( 'type' ) != 'application' do
    
      # object/objects/object/../..
      object = object.parent
      
      path << object.attribute( 'type' )
    
    end

    path.reverse.join( '.' )
  
  }.sort

end

.merge_application_elements(xml_string) ⇒ Object

TODO: document me



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 694

def self.merge_application_elements( xml_string )

  # parse the ui state xml
  document_root = MobyUtil::XML.parse_string( xml_string ).root

  # retrieve application objects as nodeset
  nodeset = document_root.xpath('/tasMessage/tasInfo/obj')

  # check if multiple application objects found
  if nodeset.count > 1

    # new header, apply original element attributes
    new_xml = "<tasMessage #{ hash_to_element_attributes( document_root.attributes )  }><tasInfo #{ hash_to_element_attributes( nodeset.first.parent.attributes ) }>"

    # flag defining that is application element already created
    application_element_set = false

    # keep error element
    new_xml << document_root.xpath('/tasMessage/tasInfo/obj[@type="Error"]').to_s

    # collect environment values
    environments = document_root.xpath('/tasMessage/tasInfo/obj[@type="application"]/@env').collect{ | attribute | attribute.to_s }

    #limit to apps
    nodeset = document_root.xpath('/tasMessage/tasInfo/obj[@type="application"]')          
    close_vkb = false
    if nodeset.count > 0
      new_xml << create_merged_element(nodeset, environments, false) 
      close_vkb = true
    end
    # do the same to the vkbs
    environments = document_root.xpath('/tasMessage/tasInfo/obj[@type="vkb_app"]/@env').collect{ | attribute | attribute.to_s }
    nodeset = document_root.xpath('/tasMessage/tasInfo/obj[@type="vkb_app"]')
    new_xml << create_merged_element(nodeset, environments, close_vkb)

    # multiple applications found, return merged application xml
    new_xml << "</obj></tasInfo></tasMessage>"

  else
  
    # only one application found, return data as is
    xml_string
  
  end

end

.parent_test_object_element(test_object) ⇒ Object



378
379
380
381
382
383
384
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 378

def self.parent_test_object_element( test_object )

  # retrieve parent of current xml element; obj/..
  test_object.refresh #Refresh the test object for identification
  test_object.xml_data.parent #.parent

end

.regexp_compare(nodeset, source, options) ⇒ Object

TODO: document me



153
154
155
156
157
158
159
160
161
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 153

def self.regexp_compare( nodeset, source, options ) 
  
  # rebuild defined regexp, used while matching element content
  regexp_object = Regexp.new( source.to_s, options.to_i )
        
  # collect all nodes matching with regexp
  nodeset.find_all{ | node | node.content =~ regexp_object }

end

.retrieve_parent_application(xml_source) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 593

def self.retrieve_parent_application( xml_source )

  xml_source_iterator = xml_source.clone

  while xml_source_iterator.kind_of?( MobyUtil::XML::Element )

    if ( test_object_element_attribute( xml_source_iterator, 'type' ) == 'application' )

      return xml_source_iterator

    end

    if xml_source_iterator.kind_of?( MobyUtil::XML::Element )

      xml_source_iterator = xml_source_iterator.parent

    else
    
      # not found from xml tree
      break
    
    end

  end

  #warn("warning: unable to retrieve parent application")

  raise MobyBase::TestObjectNotFoundError, "Unable to retrieve parent application"

  # return application object or nil if no parent found
  # Does is make sense to return nil - shouldn't all test objects belong to an application? Maybe throw exception if application not found
  
  #nil

  #return @sut.child( :type => 'application' ) rescue nil
      
end

.sort_elements(nodeset, layout_direction = 'LeftToRight') ⇒ Object

Sort XML nodeset of test objects with layout direction



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
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 310

def self.sort_elements( nodeset, layout_direction = 'LeftToRight' )

  # cache for x_absolute and y_absolute values; reduces dramatically number of xpath calls
  cache = {}

  # xpath pattern to be used for x_absolute attribute value
  x_absolute_pattern = './attr[@name="x_absolute"]/text()'

  # xpath pattern to be used for x_absolute attribute value
  y_absolute_pattern = './attr[@name="y_absolute"]/text()'

  # collect only nodes that has x_absolute and y_absolute attributes
  nodeset.collect!{ | node |

    # retrieve x_absolute attribute
    x_absolute = node.at_xpath( x_absolute_pattern )

    # retrieve y_absolute attribute
    y_absolute = node.at_xpath( y_absolute_pattern )

    # return unmodified nodeset if both attributes was not found 
    if x_absolute.nil? || y_absolute.nil?

      #warn("Warning: Unable to sort object set due to object type of #{ node.attribute( 'type' ).inspect } does not have \"x_absolute\" or \"y_absolute\" attribute")

      return nodeset

    else

      # store attributes to cache for further processing
      cache[ node ] = [ x_absolute.content.to_i, y_absolute.content.to_i ]

      # return node as result
      node

    end

  }.compact!.sort!{ | element_a, element_b |

    # retrieve element a's attributes x and y
    element_a_x, element_a_y = cache[ element_a ]

    # retrieve element b's attributes x and y
    element_b_x, element_b_y = cache[ element_b ]

    case layout_direction
    
      when 'LeftToRight'

        # compare elements
        ( element_a_y == element_b_y ) ? ( element_a_x <=> element_b_x ) : ( element_a_y <=> element_b_y ) 

      when 'RightToLeft'

        # compare elements
        ( element_a_y == element_b_y ) ? ( element_b_x <=> element_a_x ) : ( element_a_y <=> element_b_y ) 

    else

      # raise exception if layout direction it not supported 
      raise ArgumentError, "Unsupported layout direction #{ layout_direction.inspect }"

    end

  }

end

.state_object_xml(source_data, id) ⇒ Object

TODO: document me



578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 578

def self.state_object_xml( source_data, id )

  # collect each object from source xml
  #objects = source_data.xpath( 'tasInfo/obj' ).collect{ | element | element.to_s }.join

  # return xml root element
  #MobyUtil::XML.parse_string( 
  #  "<sut name='sut' type='sut' id='#{ id }'>#{ objects }</sut>"
  #).root

  # return xml root element
  MobyUtil::XML.parse_string( "<sut name='sut' type='sut' id='#{ id }'>#{ source_data.xpath('tasInfo/obj').inject(""){ | result, element | result << element.to_s; } }</sut>" ).root

end

.test_object_attribute(source_data, attribute_name, *default, &block) ⇒ Object

TODO: document me



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 438

def self.test_object_attribute( source_data, attribute_name, *default, &block )

  # TODO: consider using at_xpath and adding text() to query string; however "downside" is that if multiple matches found only first value will be returned as result

  # retrieve attribute(s) from xml
  nodeset = source_data.xpath(
   
    "attr[translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='#{ attribute_name.downcase }']"
    
  )

  # if no attributes found call optional code block or raise exception 
  if nodeset.empty? 

    if block_given?

      # pass return value of block as result
      yield( attribute_name )

    else

      # raise exception if no default value given
      if default.empty?
      
        # raise exception if no such attribute found
        raise MobyBase::AttributeNotFoundError, "Could not find attribute #{ attribute_name.inspect }" # for test object of type #{ type.to_s }"

      else
      
        # pass default value as result
        default.first
      
      end
      
    end # block_given?

  else # not nodeset.empty?
  
    # attribute(s) found
    # Need to disable this for now 
    # raise MobyBase::MultipleAttributesFoundError.new( "Multiple attributes found with name '#{ name }'" ) if nodeset.count > 1

    # return found attribute
    nodeset.first.content
    
  end

end

.test_object_attributes(source_data, inclusive_filter = []) ⇒ Object

TODO: document me



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 488

def self.test_object_attributes( source_data, inclusive_filter = [] )

  # convert all keys to lowercase
  inclusive_filter.collect!{ | key | key.to_s.downcase } unless inclusive_filter.empty?

  # return hash of test object attributes
  object_attributes = {}

    # iterate each attribute and collect name and value
    source_data.xpath( 'attr' ).collect{ | value |

      # retrieve attribute name
      name = value.attribute('name').to_s

      # collect attribute elements name and content
      unless inclusive_filter.empty?

        object_attributes[name]=value.content if inclusive_filter.include?( name.downcase )

      else
        # pass the attribute pair - no filtering done
        if object_attributes[name]
          object_attributes[name]="#{object_attributes[name]},#{value.content}"
        else
          object_attributes[name]=value.content
        end
      end

    }

  object_attributes

end

.test_object_element_attribute(source_data, attribute_name, *default, &block) ⇒ Object

TODO: document me



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 400

def self.test_object_element_attribute( source_data, attribute_name, *default, &block )

  result = source_data.attribute( attribute_name )
  
  # if no attribute found call optional code block or raise exception 
  unless result
        
    if block_given?
      
      # pass return value of block as result
      yield( attribute_name )
    
    else
    
      # raise exception if no default value given
      if default.empty?
    
        # raise exception if no such attribute found
        raise MobyBase::AttributeNotFoundError, "Could not find test object element attribute #{ attribute_name.inspect }"

      else
      
        # pass default value as result
        default.first
      
      end
    
    end
  
  else
  
    result
  
  end

end

.test_object_element_attributes(source_data) ⇒ Object

TODO: document me



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 387

def self.test_object_element_attributes( source_data )

  #Hash[
  #  source_data.attributes.collect{ | key, value | 
  #    [ key.to_s, value.to_s ]
  #  }
  #]

  source_data.attributes

end

.test_object_hash(object_id, object_type, object_name) ⇒ Object

TODO: document me



302
303
304
305
306
307
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 302

def self.test_object_hash( object_id, object_type, object_name )

  # calculate test object hash
  ( ( ( 17 * 37 + object_id ) * 37 + object_type.hash ) * 37 + object_name.hash )

end

.xpath_literal_string(string) ⇒ Object

TODO: document me



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
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 223

def self.xpath_literal_string( string )

  return string if string.kind_of?( Regexp )

  # make sure that argument is type of string
  string = string.to_s
  
  # does not contain no single quotes
  if not string.include?("'")
  
    result = "'#{ string }'"

  # does not contain no double quotes
  elsif not string.include?('"')

    result = "\"#{ string }\""

  # contains single and double quotes  
  else
  
    # open new item
    result = ["'"]

    # iterate through each character  
    string.each_char{ | char |
    
      case char
      
        # encapsulate single quotes with double quotes
        when "'"
          
          # close current item
          result.last << char
          
          # add encapsulated single quote
          result << "\"'\""
          
          # open new item
          result << char
                   
        else
        
          # any other character will appended as is
          result.last << char
        
      end

    }  

    # close last sentence
    result.last << "'"
        
    # create concat clause for xpath
    result = "concat(#{ result.join(',') })"
      
  end

  result

end

.xpath_to_object(rules, find_all_children) ⇒ Object

TODO: document me



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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/tdriver/base/test_object/xml/adapter.rb', line 164

def self.xpath_to_object( rules, find_all_children )

  # convert hash keys to downcased string
  rules = Hash[ 

    rules.collect{ | key, value | 

      case value

        # pass the value as is if type of regexp or array; array is used in localisation cases e.g. [ 'one', 'yksi', 'uno' ] # etc
        when Regexp, Array

          [ key.to_s.downcase, value ] 

      else

          [ key.to_s.downcase, value.to_s ] 

      end

    } 

  ]

  # xpath container array
  test_object_xpath_array = []

  # store and remove object element attributes from hash
  object_element_attributes = rules.delete_keys!( 'name', 'type', 'parent', 'id' )

  # children method may request test objects of any type
  if object_element_attributes[ 'type' ] == '*' 

    # test object with any name, type, parent and id is allowed
    test_object_xpath_array << '@*'

  else

    # required attributes          
    test_object_xpath_array << xpath_attributes( object_element_attributes, true, object_element_attributes[ 'type' ] )
  
  end

  # additional attributes, eg. :text, :x, :y etc. 
  test_object_xpath_array << xpath_attributes( rules, false, object_element_attributes[ 'type' ] )

  # join required and additional attribute strings
  test_object_xpath_string = test_object_xpath_array.compact.join( ' and ' )

  # return any child element under current node or only immediate child element 
  #find_all_children ? "*//obj[#{ test_object_xpath_string }]" : "obj[1]/obj[#{ test_object_xpath_string }]"

  find_all_children ? ".//obj[#{ test_object_xpath_string }]" : "obj[#{ test_object_xpath_string }]"

  #"*//obj[#{ test_object_xpath_string }]"

end