Class: FormScraperHelper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, headless: false, clipb: true, fd: nil, debug: false) ⇒ FormScraperHelper

note: fd corresponds to FakeDataGenerator22 which is optional



16
17
18
19
20
21
22
23
24
25
# File 'lib/formscraper_helper.rb', line 16

def initialize(url, headless: false, clipb: true, fd: nil, debug: false)

  @url, @clipb, @fd, @debug = url, clipb, fd, debug
  @browser = Ferrum::Browser.new  headless: headless
  @browser.goto(url)

  sleep 2
  scrape()

end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



12
13
14
# File 'lib/formscraper_helper.rb', line 12

def browser
  @browser
end

Instance Method Details

#to_codeObject



31
32
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
84
85
86
87
88
89
90
91
# File 'lib/formscraper_helper.rb', line 31

def to_code()

s="require 'yaml'\nrequire 'ferrum'\nrequire 'nokorexi'\n\nbrowser = Ferrum::Browser.new  headless: false\nurl = '\#{@url}'\nbrowser.goto(url)\nsleep 2\n\ndoc = Nokorexi.new(browser.body).to_doc\n\n# load the YAML document containing the inputs\n#filepath = ''\nfilepath = '/tmp/tmp.yaml'\nh = YAML.load(File.read(filepath))\n"

  @h.each do |key, h|

    puts 'key: ' + key.inspect if @debug

    s += "r = browser.at_xpath('#{h[:xpath]}')\n"

    if h[:type] == 'text' or h[:type] == 'password' then

      var1, s2 = format_var1(h[:title], key)
      s += s2
      s += var1 + " = h['#{var1}']\n"
      s += "r.focus.type #{var1}\n"
      s += "sleep 0.5\n\n"

    elsif h[:type] == 'select'

      var1, s2 = format_var1(h[:title], key)
      s += s2

      s += "# options: #{h[:options].join(', ')}\n"
      s += "#{var1} = h['#{var1}']\n"
      s += 'titles = %w(' + h[:options].join(' ') + ')' + "\n"
      s += 'found = titles.grep /#{' + var1 + '}/i' + "\n"
      s += "n = titles.index(found.first) + 1\n"
      s += "r.focus\n"
      s += "n.times { r.type(:down); sleep 1}\n"
      s += "r.click\n"
      s += "sleep 0.5\n\n"

    elsif h[:type] == 'checkbox'
      s += "r.focus.click\n\n"
    end

  end

  Clipboard.copy s if @clipb
  puts 'generated code copied to clipboard'

  return s

end

#to_hObject



27
28
29
# File 'lib/formscraper_helper.rb', line 27

def to_h()
  @h
end

#to_yamlObject

creates a YAML document for the inputs



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
138
139
140
141
# File 'lib/formscraper_helper.rb', line 95

def to_yaml()

  s = '---' + "\n"

  @h.each do |key, h|

    puts 'key: ' + key.inspect if @debug

    if h[:type] == 'text' or h[:type] == 'password' then

      var1, s2 = format_var1(h[:title], key)

      s += s2

      if h[:type] == 'password' then
        @pwd ||= @fd ? @fd.password : 'xxx'
        s += var1 + ": #{@pwd}\n"
      elsif @fd

        found = @fd.lookup var1
        val = found.is_a?(String) ? found : 'xxx'
        s += var1 + ": #{val}\n"
      else
        s += var1 + ": xxx\n"
      end

    elsif h[:type] == 'select'

      var1, s2 = format_var1(h[:title], key)

      s += s2
      s += "# options: #{h[:options].join(', ')}\n"
      val = h[:options][1..-1].sample
      s += "#{var1}: #{val}\n"

    elsif h[:type] == 'checkbox'

    end

  end

  Clipboard.copy s if @clipb
  puts 'generated YAML copied to clipboard'

  return s

end