Class: Insite::ElementCollection

Inherits:
Element
  • Object
show all
Defined in:
lib/insite/element/element_collection.rb

Direct Known Subclasses

AnchorCollection, AppletCollection, AreaCollection, AudioCollection, BRCollection, BaseCollection, BodyCollection, ButtonCollection, CanvasCollection, CellCollection, CheckBoxCollection, CircleCollection, DListCollection, DataCollection, DataListCollection, DateFieldCollection, DateTimeFieldCollection, DefsCollection, DescCollection, DetailsCollection, DirectoryCollection, DivCollection, EllipseCollection, EmbedCollection, FieldSetCollection, FileFieldCollection, FontCollection, ForeignObjectCollection, FormCollection, FrameCollection, FrameSetCollection, GCollection, GeometryCollection, GradientCollection, GraphicsCollection, HRCollection, HTMLElementCollection, HeadCollection, HeadingCollection, HiddenCollection, HtmlCollection, IFrameCollection, ImageCollection, InputCollection, LICollection, LabelCollection, LegendCollection, LineCollection, LinearGradientCollection, MapCollection, MarkerCollection, MarqueeCollection, MediaCollection, MetaCollection, MetadataCollection, MeterCollection, ModCollection, OListCollection, ObjectCollection, OptGroupCollection, OptionCollection, OutputCollection, ParagraphCollection, ParamCollection, PathCollection, PatternCollection, PictureCollection, PolygonCollection, PolylineCollection, PreCollection, ProgressCollection, QuoteCollection, RadialGradientCollection, RadioCollection, RectCollection, RowCollection, SVGCollection, SVGElementCollection, ScriptCollection, SelectCollection, SourceCollection, SpanCollection, StopCollection, StyleCollection, SwitchCollection, SymbolCollection, TSpanCollection, TableCaptionCollection, TableCellCollection, TableColCollection, TableCollection, TableDataCellCollection, TableHeaderCellCollection, TableRowCollection, TableSectionCollection, TemplateCollection, TextAreaCollection, TextContentCollection, TextFieldCollection, TextPathCollection, TextPositioningCollection, TimeCollection, TitleCollection, TrackCollection, UListCollection, UnknownCollection, UseCollection, VideoCollection, ViewCollection

Instance Attribute Summary collapse

Attributes inherited from Element

#site, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#attributes, #classes, #method_missing, #present?, #respond_to_missing?

Methods included from ElementInstanceMethods

#a, #abbr, #abbrs, #address, #addresses, #area, #areas, #article, #articles, #as, #aside, #asides, #audio, #audios, #b, #base, #bases, #bdi, #bdis, #bdo, #bdos, #blockquote, #blockquotes, #body, #bodys, #br, #brs, #bs, #button, #buttons, #canvas, #canvases, #caption, #captions, #cell, #cells, #checkbox, #checkboxes, #circle, #circles, #cite, #cites, #code, #codes, #col, #colgroup, #colgroups, #cols, #data, #datalist, #datalists, #datas, #date_field, #date_fields, #date_time_field, #date_time_fields, #dd, #dds, #defs, #defss, #del, #dels, #desc, #descs, #details, #detailses, #dfn, #dfns, #div, #divs, #dl, #dls, #dt, #dts, #element, #elements, #ellipse, #ellipses, #em, #embed, #embeds, #ems, #field_set, #field_sets, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #foreign_object, #foreign_objects, #form, #forms, #frame, #frames, #frameset, #framesets, #g, #gs, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #head, #header, #headers, #heads, #hidden, #hiddens, #hr, #hrs, #htmls, #i, #iframe, #iframes, #image, #images, #img, #imgs, #input, #inputs, #ins, #inses, #is, #kbd, #kbds, #label, #labels, #legend, #legends, #li, #line, #linear_gradient, #linear_gradients, #lines, #link, #links, #lis, #main, #mains, #map, #maps, #mark, #marker, #markers, #marks, #meta, #metadata, #metadatas, #metas, #meter, #meters, #nav, #navs, #noscript, #noscripts, #object, #objects, #ol, #ols, #optgroup, #optgroups, #option, #options, #output, #outputs, #p, #param, #params, #path, #paths, #pattern, #patterns, #picture, #pictures, #polygon, #polygons, #polyline, #polylines, #pre, #pres, #progress, #progresses, #ps, #q, #qs, #radial_gradient, #radial_gradients, #radio, #radios, #rb, #rbs, #rect, #rects, #row, #rows, #rp, #rps, #rt, #rtc, #rtcs, #rts, #rubies, #ruby, #s, #samp, #samps, #script, #scripts, #section, #sections, #select, #select_list, #select_lists, #selects, #small, #smalls, #source, #sources, #span, #spans, #ss, #stop, #stops, #strong, #strongs, #style, #styles, #sub, #subs, #summaries, #summary, #sup, #sups, #svg, #svgs, #switch, #switches, #symbol, #symbols, #table, #tables, #tbody, #tbodys, #td, #tds, #template, #templates, #text_field, #text_fields, #text_path, #text_paths, #textarea, #textareas, #tfoot, #tfoots, #th, #thead, #theads, #ths, #time, #times, #titles, #tr, #track, #tracks, #trs, #tspan, #tspans, #u, #ul, #uls, #us, #use, #uses, #var, #vars, #video, #videos, #view, #views, #wbr, #wbrs

Methods included from CommonMethods

#document, #process_browser, #update_object

Constructor Details

#initialize(query_scope, *args) ⇒ ElementCollection

Returns a new instance of ElementCollection.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/insite/element/element_collection.rb', line 33

def initialize(query_scope, *args)
  @collection_member_type = self.class.collection_member_type

  # Figure out the correct query scope.
  query_scope.respond_to?(:target) ? obj = query_scope : obj = query_scope.site
  @query_scope   = obj
  @site     = query_scope.class.ancestors.include?(Insite) ? query_scope : query_scope.site
  @browser  = @site.browser
  if args[0].is_a?(Insite::Element) || args[0].is_a?(Watir::Element)
    @target   = args[0].target
    @args     = @target.selector.dup
    @selector = @args
  elsif args[0].is_a?(Insite::ElementCollection) || args[0].is_a?(Watir::ElementCollection)
    @target   = args[0]
    @args     = @target.instance_variable_get(:@selector).dup
    @selector   = @args
  else
    if @collection_member_type == Insite::HTMLElement
      @args     = parse_args(args)
      @selector = @args

      # Figure out the correct query scope.
      @non_relative = @args.delete(:non_relative) || false
      if @non_relative
        @query_scope = query_scope.site
      else
        query_scope.respond_to?(:target) ? obj = query_scope : obj = query_scope.site
        @query_scope = obj
      end

      @target   = Watir::HTMLElementCollection.new(@query_scope.target, @args)
    else
      @args   = parse_args(args).merge(
        tag_name: Insite.class_to_tag(@collection_member_type)
      )
      @selector = @args

      # Figure out the correct query scope.
      @non_relative = @args.delete(:non_relative) || false
      if @non_relative
        @query_scope = query_scope.site
      else
        query_scope.respond_to?(:target) ? obj = query_scope : obj = query_scope.site
        @query_scope = obj
      end
      @selector = @args

      @target   = Insite::CLASS_MAP.key(self.class).new(@query_scope.target, @args)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Insite::Element

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



3
4
5
# File 'lib/insite/element/element_collection.rb', line 3

def browser
  @browser
end

#selectorObject (readonly)

Returns the value of attribute selector.



4
5
6
# File 'lib/insite/element/element_collection.rb', line 4

def selector
  @selector
end

Class Method Details

.collection?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/insite/element/element_collection.rb', line 6

def self.collection?
  true
end

.collection_member_typeObject



23
24
25
26
27
28
29
30
31
# File 'lib/insite/element/element_collection.rb', line 23

def self.collection_member_type
  if Insite::CLASS_MAP.values.include?(self)
    @collection_member_type = self.to_s.gsub('Collection', '').constantize
  elsif Insite::CLASS_MAP.values.include?(self.superclass)
    @collection_member_type = self.superclass.to_s.gsub('Collection', '').constantize
  else
    raise "Unable to determine collection member type for #{self}."
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



10
11
12
# File 'lib/insite/element/element_collection.rb', line 10

def ==(other)
  to_a == other.to_a
end

#[](idx) ⇒ Object



15
16
17
# File 'lib/insite/element/element_collection.rb', line 15

def[](idx)
  to_a[idx]
end

#collection?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/insite/element/element_collection.rb', line 19

def collection?
  true
end

#each(&block) ⇒ Object



89
90
91
# File 'lib/insite/element/element_collection.rb', line 89

def each(&block)
  to_a.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/insite/element/element_collection.rb', line 93

def empty?
  length == 0
end

#firstObject



85
86
87
# File 'lib/insite/element/element_collection.rb', line 85

def first
  to_a[0]
end

#inspectObject



97
98
99
100
# File 'lib/insite/element/element_collection.rb', line 97

def inspect
  @selector.empty? ? s = '{element: (selenium element)}' : s = @selector.to_s
  "#<#{self.class}: @query_scope: #{@query_scope}; @selector=#{s}>"
end

#lastObject



102
103
104
# File 'lib/insite/element/element_collection.rb', line 102

def last
  to_a[-1]
end

#lengthObject Also known as: count, size



106
107
108
# File 'lib/insite/element/element_collection.rb', line 106

def length
  to_a.length
end

#to_aObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/insite/element/element_collection.rb', line 112

def to_a
  out = []
  @target.to_a.each_with_index do |elem, idx|
    out << @collection_member_type.new(
      @query_scope,
      @args.merge(index: idx)
    )
  end
  out
end