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

Class Method Summary collapse

Class Method Details

.handler_failure(driver) ⇒ Object



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
# File 'lib/appium_failure_helper/capture.rb', line 36

def self.handler_failure(driver)
  begin
    timestamp = Time.now.strftime('%Y%m%d_%H%M%S')
    output_folder = "screenshots/failure_#{timestamp}"
    
    FileUtils.mkdir_p(output_folder)
    
    screenshot_path = "#{output_folder}/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 = "#{output_folder}/page_source_#{timestamp}.xml"
    File.write(xml_path, page_source)

    doc = Nokogiri::XML(page_source)

    platform = driver.capabilities['platformName']&.downcase || 'unknown'

    seen_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']}|#{attrs['content-desc']}|#{attrs['text']}"
      
      unless seen_elements[unique_key]
        name = self.suggest_name(node.name, attrs)
        locators = self.xpath_generator(node.name, attrs, platform)
        
        suggestions << { name: name, locators: locators }
        seen_elements[unique_key] = true
      end
    end

    yaml_path = "#{output_folder}/element_suggestions_#{timestamp}.yaml"
    File.open(yaml_path, 'w') do |f|
      f.write(YAML.dump(suggestions))
    end

    puts "Element suggestions saved to #{yaml_path}"
  rescue => e
    puts "Error capturing failure details: #{e.message}\n#{e.backtrace.join("\n")}"
  end
end