Module: PageObjectPal::Elements

Included in:
PageObjectPal, Diff
Defined in:
lib/page-object-pal/elements.rb

Instance Method Summary collapse

Instance Method Details

#dsl_to_html(anchor) ⇒ Object

Convert non-HTML anchor_tags from the PageObject::DSL to HTML.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/page-object-pal/elements.rb', line 56

def dsl_to_html(anchor)
  case anchor
  when "image" then :img
  when "link" then :a
  when "list_item" then :li
  when "ordered_list" then :ol
  when "paragraph" then :p
  when "radio_button" then :radio
  when "text_area" then :input
  when "text_field" then :input
  when "unordered_list" then :ul
  else anchor.to_sym
  end
end

#element_tag(string) ⇒ Object

Convert method defining string for PageObject::Accessors#element method.



37
38
39
40
41
42
# File 'lib/page-object-pal/elements.rb', line 37

def element_tag(string)
  tag = string[/,:(\w+)/].gsub(",:","")
  sym = string[/:class|:id|:index|:text|:xpath/].gsub(":","").to_sym
  (sym == :index) ? val = string[/\d+/] : val = string[/"(.+)"/].gsub("\"","")
  { tag => { sym => val } }
end

#html_to_dsl(anchor) ⇒ Object

Convert HTML to PageObject::DSL where the two are not identical.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/page-object-pal/elements.rb', line 74

def html_to_dsl(anchor)
  case anchor
  when :img then "image"
  when :a then "link"
  when :li then "list_item"
  when :ol then "ordered_list"
  when :p then "paragraph"
  when :radio then "radio_button"
  when :input then "text_field/text_area"
  when :ul then "unordered_list"
  else anchor.to_s
  end
end

#parse_element(string) ⇒ Object

Convert method defining string to hash containing anchor_tag, identifying_property, and property_value.



24
25
26
27
28
29
30
31
32
# File 'lib/page-object-pal/elements.rb', line 24

def parse_element(string)
  string.gsub!(/(,\s+)/, ",")
  tag = dsl_to_html(string[/^(\w+)/])

  case tag
  when :element then element_tag(string)
  else standard_tag(tag, string)
  end
end

#parse_methods(methods, path) ⇒ Object

Find lines in the page object class file that define the non-super instance methods.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/page-object-pal/elements.rb', line 8

def parse_methods(methods, path)
  elements = []
  methods.each do |meth|
    File.open(path).read.each_line do |line|
      next unless line.include? meth.to_s
      next unless line.lstrip.start_with? "div", "element", "image", "link", "list_item", "ordered_list", "paragraph", "radio_button", "span", "text_area", "text_field", "unordered_list"
      elements << parse_element(line.lstrip)
    end
  end
  elements
end

#standard_tag(tag, string) ⇒ Object

Convert method defining string for standard PageObject::Accessors methods.



47
48
49
50
51
# File 'lib/page-object-pal/elements.rb', line 47

def standard_tag(tag, string)
  sym = string[/:class|:id|:index|:text|:xpath/].gsub(":","").to_sym
  (sym == :index) ? val = string[/\d+/] : val = string[/"(.+)"/].gsub("\"","")
  { tag => { sym => val } }
end