Class: AppiumFailureHelper::Capture

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

Class Method Summary collapse

Class Method Details

.handler_failure(driver) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/appium_failure_helper/capture.rb', line 7

def self.handler_failure(driver)
  begin
    timestamp = Time.now.strftime('%Y%m%d_%H%M%S')
    folder_path = "screenshots"
    
    FileUtils.mkdir_p(folder_path)
    
    screenshot_path = "#{folder_path}/screenshot_#{timestamp}.png"
    File.open(screenshot_path, 'wb') do |f|
      f.write(Base64.decode64(driver.screenshot_as(:base64)))
    end
    puts "Screenshot saved to #{screenshot_path}"

    page_source = driver.page_source
    xml_path = "#{folder_path}/page_source_#{timestamp}.xml"
    File.write(xml_path, page_source)

    doc = Nokogiri::XML(page_source)

    prefix = {
      'Button' => 'btn',
      'TextView' => 'txt',
      'ImageView' => 'img',
      'EditText' => 'input',
      'CheckBox' => 'chk',
      'RadioButton' => 'radio',
      'Switch' => 'switch',
    }

    line = doc.xpath('//*').map do |node|
      next if node.name == 'hierarchy'
      attrs = node.attributes.transform_values(&:value)
      
      # Os métodos agora estão no escopo correto.
      name = self.suggest_name(node.name, attrs, prefix)
      xpath = self.xpath_generator(node.name, attrs)
      
      "[\"#{name}\", \"xpath\", \"#{xpath}\"]"
    end

    yaml_path = "#{folder_path}/element_suggestions_#{timestamp}.yaml"
    File.open(yaml_path, 'w') {|f| f.puts(line.compact.join("\n"))}

    puts "Element suggestions saved to #{yaml_path}"
  rescue => e
    puts "Error capturing failure details: #{e.message}"
  end
end