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

Class Method Summary collapse

Class Method Details

.handler_failure(driver) ⇒ Object



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

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'

    yaml_path = "#{output_folder}/element_suggestions_#{timestamp}.yaml"
    File.open(yaml_path, 'w') do |f|
      suggestions = doc.xpath('//*').map do |node|
        next if node.name == 'hierarchy'
        attrs = node.attributes.transform_values(&:value)
        
        name = self.suggest_name(node.name, attrs)
        xpath = self.xpath_generator(node.name, attrs, platform)

        parent_node = node.parent
        parent_xpath = parent_node ? parent_node.path : nil
        
        { 
          name: name, 
          type: 'xpath', 
          locator: xpath,
          parent_locator: parent_xpath
        }
      end.compact

      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