Class: Webdrone::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/webdrone/form.rb,
lib/webdrone/logg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a0) ⇒ Form

Returns a new instance of Form.



11
12
13
14
# File 'lib/webdrone/form.rb', line 11

def initialize(a0)
  @a0 = a0
  @data = nil
end

Instance Attribute Details

#a0Object

Returns the value of attribute a0.



9
10
11
# File 'lib/webdrone/form.rb', line 9

def a0
  @a0
end

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/webdrone/form.rb', line 9

def data
  @data
end

Instance Method Details

#clic(key, n: 1, visible: true, scroll: false, parent: nil, mark: false) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/webdrone/form.rb', line 114

def clic(key, n: 1, visible: true, scroll: false, parent: nil, mark: false)
  item = self.find_item(key, n: n, visible: visible, scroll: scroll, parent: parent)
  @a0.mark.mark_item item if mark
  item.click
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#get(key, n: 1, visible: true, scroll: false, parent: nil, mark: false) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/webdrone/form.rb', line 106

def get(key, n: 1, visible: true, scroll: false, parent: nil, mark: false)
  item = self.find_item(key, n: n, visible: visible, scroll: scroll, parent: parent)
  @a0.mark.mark_item item if mark
  item[:value]
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#mark(key, n: 1, visible: true, scroll: false, parent: nil, color: '#af1616', times: nil, delay: nil, shot: nil) ⇒ Object



122
123
124
125
126
# File 'lib/webdrone/form.rb', line 122

def mark(key, n: 1, visible: true, scroll: false, parent: nil, color: '#af1616', times: nil, delay: nil, shot: nil)
  @a0.mark.mark_item self.find_item(key, n: n, visible: visible, scroll: scroll, parent: parent), color: color, times: times, delay: delay, shot: shot
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#save(filename:, sheet:, item:, name: 'ITEM') ⇒ Object



25
26
27
28
29
30
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
# File 'lib/webdrone/form.rb', line 25

def save(filename:, sheet:, item:, name: 'ITEM')
  prev = @data
  data = {}
  @data = data

  yield
ensure
  File.open(".#{filename}.lock", File::RDWR | File::CREAT, 0644) do |file|
    items = {}
    items[item] = data

    begin
      workbook = RubyXL::Parser.parse(filename)
      worksheet = workbook[sheet]
      worksheet = workbook.add_worksheet sheet unless worksheet
    rescue
      workbook = RubyXL::Workbook.new
      worksheet = workbook[0]
      worksheet.sheet_name = sheet
    end

    rows = worksheet.sheet_data.rows.collect do |row|
      row.cells.collect do |cell|
        cell.value if cell != nil
      end
    end
    heads = rows.shift || []
    rows.each do |row|
      item = {}
      key = nil
      row.each_with_index do |val, i|
        val = val.to_s if val
        if i == 0
          key = val
        elsif key
          items[key] = {} unless items[key]
          items[key][heads[i]] = val if heads[i] != nil and items[key][heads[i]] == nil
        end
      end
    end
    x = heads.shift
    x ||= name
    worksheet.add_cell 0, 0, x

    heads += data.keys.sort
    heads = heads.uniq

    heads.each_with_index do |field, coli|
      worksheet.add_cell 0, coli + 1, field
    end
    worksheet.change_row_bold 0, true
    items.each_with_index do |elem, rowi|
      key, item = elem
      worksheet.add_cell rowi + 1, 0, key
      heads.each_with_index do |field, coli|
        worksheet.add_cell rowi + 1, coli + 1, item[field]
      end
    end

    workbook.write filename
  end

  @data = prev
end

#set(key, val, n: 1, visible: true, scroll: false, parent: nil, mark: false) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/webdrone/form.rb', line 90

def set(key, val, n: 1, visible: true, scroll: false, parent: nil, mark: false)
  item = self.find_item(key, n: n, visible: visible, scroll: scroll, parent: parent)
  @a0.mark.mark_item item if mark
  if item.tag_name == 'select'
    option = item.find_element :xpath, XPath::HTML.option(val).to_s
    option.click
  else
    item.clear
    item.send_keys(val)
  end
  @data[key] = val if @data
  nil
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#submit(key = nil, n: 1, visible: true, scroll: false, parent: nil, mark: false) ⇒ Object



128
129
130
131
132
133
# File 'lib/webdrone/form.rb', line 128

def submit(key = nil, n: 1, visible: true, scroll: false, parent: nil, mark: false)
  self.find_item(key, n: n, visible: visible, scroll: scroll, parent: parent) if key
  @lastitem.submit
rescue => exception
  Webdrone.report_error(@a0, exception)
end

#with_xpath(xpath = nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/webdrone/form.rb', line 16

def with_xpath(xpath = nil, &block)
  old_xpath, @xpath = @xpath, xpath
  instance_eval &block
rescue => exception
  Webdrone.report_error(@a0, exception)
ensure
  @xpath = old_xpath
end

#xlsx(sheet: nil, filename: nil) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/webdrone/form.rb', line 135

def xlsx(sheet: nil, filename: nil)
  @a0.xlsx.dict(sheet: sheet, filename: filename).each do |k, v|
    self.set k, v
  end
rescue => exception
  Webdrone.report_error(@a0, exception)
end