Class: AppiumFailureHelper::Capture

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

Constant Summary collapse

PREFIX =
{
  'android.widget.Button' => 'btn',
  'android.widget.TextView' => 'txt',
  'android.widget.ImageView' => 'img',
  'android.widget.EditText' => 'input',
  'android.widget.CheckBox' => 'chk',
  'android.widget.RadioButton' => 'radio',
  'android.widget.Switch' => 'switch',
  'android.widget.ViewGroup' => 'group',
  'android.widget.View' => 'view',
  'android.widget.FrameLayout' => 'frame',
  'android.widget.LinearLayout' => 'linear',
  'android.widget.RelativeLayout' => 'relative',
  'android.widget.ScrollView' => 'scroll',
  'android.webkit.WebView' => 'web',
  'android.widget.Spinner' => 'spin',
  'XCUIElementTypeButton' => 'btn',
  'XCUIElementTypeStaticText' => 'txt',
  'XCUIElementTypeTextField' => 'input',
  'XCUIElementTypeImage' => 'img',
  'XCUIElementTypeSwitch' => 'switch',
  'XCUIElementTypeScrollView' => 'scroll',
  'XCUIElementTypeOther' => 'elm',
  'XCUIElementTypeCell' => 'cell',
}.freeze
MAX_VALUE_LENGTH =
100
@@logger =
nil

Class Method Summary collapse

Class Method Details

.handler_failure(driver, exception) ⇒ Object



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
92
93
94
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
# File 'lib/appium_failure_helper/capture.rb', line 38

def self.handler_failure(driver, exception)
  begin
    self.setup_logger unless @@logger
    
    # Remove a pasta reports_failure ao iniciar uma nova execução
    FileUtils.rm_rf("reports_failure")
    @@logger.info("Pasta 'reports_failure' removida para uma nova execução.")
    
    timestamp = Time.now.strftime('%Y%m%d_%H%M%S')
    output_folder = "reports_failure/failure_#{timestamp}"
    
    FileUtils.mkdir_p(output_folder)
    @@logger.info("Pasta de saída criada: #{output_folder}")
    
    # Captura o Base64 e salva o PNG
    screenshot_base64 = driver.screenshot_as(:base64)
    screenshot_path = "#{output_folder}/screenshot_#{timestamp}.png"
    File.open(screenshot_path, 'wb') do |f|
      f.write(Base64.decode64(screenshot_base64))
    end
    @@logger.info("Screenshot salvo em #{screenshot_path}")

    page_source = driver.page_source
    xml_path = "#{output_folder}/page_source_#{timestamp}.xml"
    File.write(xml_path, page_source)
    @@logger.info("Page source salvo em #{xml_path}")

    doc = Nokogiri::XML(page_source)
    platform = driver.capabilities['platformName']&.downcase || 'unknown'

    failed_element_info = self.extract_info_from_exception(exception)

    # --- Processamento de todos os elementos ---
    seen_elements = {}
    all_elements_suggestions = []
    doc.xpath('//*').each do |node|
      next if node.name == 'hierarchy'
      attrs = node.attributes.transform_values(&:value)
      
      unique_key = "#{node.name}|#{attrs['resource-id'].to_s}|#{attrs['content-desc'].to_s}|#{attrs['text'].to_s}"
      
      unless seen_elements[unique_key]
        name = self.suggest_name(node.name, attrs)
        locators = self.xpath_generator(node.name, attrs, platform)
        
        all_elements_suggestions << { name: name, locators: locators }
        seen_elements[unique_key] = true
      end
    end

    # --- Geração do Relatório FOCADO (1) ---
    targeted_report = {
      failed_element: failed_element_info,
      similar_elements: [],
    }

    if failed_element_info && failed_element_info[:selector_value]
      targeted_report[:similar_elements] = self.find_similar_elements(doc, failed_element_info, platform)
    end
    
    targeted_yaml_path = "#{output_folder}/failure_analysis_#{timestamp}.yaml"
    File.open(targeted_yaml_path, 'w') do |f|
      f.write(YAML.dump(targeted_report))
    end
    @@logger.info("Análise direcionada salva em #{targeted_yaml_path}")

    # --- Geração do Relatório COMPLETO (2) ---
    full_dump_yaml_path = "#{output_folder}/all_elements_dump_#{timestamp}.yaml"
    File.open(full_dump_yaml_path, 'w') do |f|
      f.write(YAML.dump(all_elements_suggestions))
    end
    @@logger.info("Dump completo da página salvo em #{full_dump_yaml_path}")

    # --- Geração do Relatório HTML (3) ---
    html_report_path = "#{output_folder}/report_#{timestamp}.html"
    html_content = self.generate_html_report(targeted_report, all_elements_suggestions, screenshot_base64, platform, timestamp)
    File.write(html_report_path, html_content)
    @@logger.info("Relatório HTML completo salvo em #{html_report_path}")

  rescue => e
    @@logger.error("Erro ao capturar detalhes da falha: #{e.message}\n#{e.backtrace.join("\n")}")
  end
end