Class: Insite::Element

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, ComponentMethods, DOMMethods
Includes:
CommonMethods, ComponentInstanceMethods, ElementInstanceMethods
Defined in:
lib/insite/element/element.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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) ⇒ Element



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
# File 'lib/insite/element/element.rb', line 37

def initialize(query_scope, *args)
  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
    @selector = @target.selector.dup
    @args     = @selector
  elsif  args[0].is_a?(Insite::ElementCollection) || args[0].is_a?(Watir::ElementCollection)
    @args     = nil
    @selector = @target.instance_variable_get(:@selector).dup
    @args     = @selector
  else
    if [Insite::Element, Insite::HTMLElement].include?(self.class)
      @args = parse_args(args.dup)
    elsif tag = parse_args(args)[:tag_name]
      @args = parse_args(args.dup)
    elsif Insite.class_to_tag(self.class)
      @args = parse_args(args.dup).merge(
        tag_name: Insite.class_to_tag(self.class)
      )
    else
      @args = parse_args(args.dup)
    end

    # 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

    if watir_class = Insite::CLASS_MAP.key(self.class)
      @target = watir_class.new(@query_scope.target, @args)
    else
      @target = Watir::HTMLElement.new(@query_scope.target, @args)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mth, *args, &block) ⇒ Object

For page component code.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/insite/element/element.rb', line 112

def method_missing(mth, *args, &block)
  if @target.respond_to? mth
    out = @target.send(mth, *args, &block)
  elsif @target.class.descendants.any? { |x| x.instance_methods.include? mth }
    out = @target.to_subtype.send(mth, *args, &block)
  else
    super
  end

  if out == @target
    self
  elsif out.is_a?(Watir::Element) || out.is_a?(Watir::ElementCollection)
     Insite::CLASS_MAP[out.class].new(@query_scope, out)
  else
    out
  end
end

Class Attribute Details

.predefined_selectorObject (readonly)

Returns the value of attribute predefined_selector.



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

def predefined_selector
  @predefined_selector
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



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

def site
  @site
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

Class Method Details

.collection?Boolean



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

def self.collection?
  false
end

Instance Method Details

#attributesObject



23
24
25
26
27
# File 'lib/insite/element/element.rb', line 23

def attributes
  nokogiri.xpath("//#{selector[:tag_name]}")[0].attributes.values.map do |x|
    [x.name, x.value]
  end.to_h
end

#classesObject



29
30
31
# File 'lib/insite/element/element.rb', line 29

def classes
  attribute('class').split
end

#collection?Boolean



33
34
35
# File 'lib/insite/element/element.rb', line 33

def collection?
  false
end

#inspectObject

def initialize(site, element)

@site    = site
@browser = @site.browser
@target  = element

# Temporary replacement for custom wait_until.
# TODO: Continue looking at scolling solutions.
if @target.present? && @target.respond_to?(:scroll)
  @target.scroll.to
  t = ::Time.now + 2
  while ::Time.now <= t do
    break if @target.present?
    sleep 0.1
  end
end

@target

end



102
103
104
105
106
107
108
109
# File 'lib/insite/element/element.rb', line 102

def inspect
  if @target.selector.present?
    s = @selector.to_s
  else
    s = '{element: (selenium element)}'
  end
  "#<#{self.class}: located: #{[email protected]}; @selector=#{s}>"
end

#present?Boolean



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/insite/element/element.rb', line 130

def present?
  sleep 0.1
  begin
    if @query_scope
      if @query_scope.respond_to?(:present?) && @query_scope.present? && @target.present?
        true
      else
        false
      end
    else
      if @target.present?
        true
      else
        false
      end
    end
  rescue => e
    false
  end
end

#respond_to_missing?(mth, include_priv = false) ⇒ Boolean



151
152
153
# File 'lib/insite/element/element.rb', line 151

def respond_to_missing?(mth, include_priv = false)
  @target.respond_to?(mth, include_priv) || super
end