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"

Parameters:

  • format (:base64, :png)

Returns:

  • String screenshot



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")

Parameters:

Returns:

  • (File)

    Path to the screenshot.



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

#save_viewport_screenshotObject

Save screenshot except for status bar while ‘@driver.save_screenshot` save entire screen.

Examples:


@driver.save_viewport_screenshot 'path/to/save.png' #=> Get the File instance of viewport_screenshot

Since:

  • Appium 1.3.4



89
90
91
92
93
94
95
96
97
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 89

def save_viewport_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
  viewport_screenshot_encode64 = bridge.take_viewport_screenshot
  File.open(png_path, 'wb') { |f| f << viewport_screenshot_encode64.unpack('m')[0] }
end

#screenshot_as(format) ⇒ Object

Return a PNG screenshot in the given format as a string

Examples:


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

Parameters:

  • format (:base64, :png)

Returns:

  • String screenshot



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