Module: Appium::Core::Base::TakesScreenshot

Included in:
Driver, Element
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



84
85
86
87
88
89
90
91
92
93
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 84

def element_screenshot_as(element, format)
  case format
  when :base64
    bridge.element_screenshot element.id
  when :png
    bridge.element_screenshot(element.id).unpack('m')[0]
  else
    raise ::Appium::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:

  • element (Selenium::WebDriver::Element)

    A element you’d like to take screenshot.

  • png_path (String)

    A path to save the screenshot

Returns:

  • (File)

    Path to the screenshot.



63
64
65
66
67
68
69
70
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 63

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



24
25
26
27
28
29
30
31
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 24

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



103
104
105
106
107
108
109
110
111
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 103

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.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



44
45
46
47
48
49
50
51
52
53
# File 'lib/appium_lib_core/common/base/screenshot.rb', line 44

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