Module: Appium::Core::Base::TakeScreenshot

Included in:
Driver
Defined in:
lib/appium_lib_core/common/base/screenshot.rb

Instance Method Summary collapse

Instance Method Details

#element_screenshot_as(element, format) ⇒ Object

Return a PNG screenshot in the given format as a string

Examples:


@@driver.element_screenshot_as element, :base64 #=> "iVBORw0KGgoAAAANSUhEUgAABDgAAAB+CAIAAABOPDa6AAAAAX"


70
71
72
73
74
75
76
77
78
79
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 70

def element_screenshot_as(element, format)
  case format
  when :base64
    bridge.take_element_screenshot(element)
  when :png
    bridge.take_element_screenshot(element).unpack('m')[0]
  else
    raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
  end
end

#save_element_screenshot(element, png_path) ⇒ File Also known as: take_element_screenshot

Returns Path to the screenshot.

Examples:


@driver.save_element_screenshot(element, "fine_name.png")


49
50
51
52
53
54
55
56
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 49

def save_element_screenshot(element, png_path)
  extension = File.extname(png_path).downcase
  if extension != '.png'
    ::Appium::Logger.warn 'name used for saved screenshot does not match file type. '\
                        'It should end with .png extension'
  end
  File.open(png_path, 'wb') { |f| f << element_screenshot_as(element, :png) }
end

#save_screenshot(png_path) ⇒ Object

Save a PNG screenshot to the given path



10
11
12
13
14
15
16
17
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 10

def save_screenshot(png_path)
  extension = File.extname(png_path).downcase
  if extension != '.png'
    ::Appium::Logger.warn 'name used for saved screenshot does not match file type. '\
                          'It should end with .png extension'
  end
  File.open(png_path, 'wb') { |f| f << screenshot_as(:png) }
end

#screenshot_as(format) ⇒ Object

Return a PNG screenshot in the given format as a string

Examples:


@@driver.screenshot_as :base64 #=> "iVBORw0KGgoAAAANSUhEUgAABDgAAAB+CAIAAABOPDa6AAAAAX"


30
31
32
33
34
35
36
37
38
39
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 30

def screenshot_as(format)
  case format
  when :base64
    bridge.screenshot
  when :png
    bridge.screenshot.unpack('m')[0]
  else
    raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
  end
end