Skip to content

Commit 415b908

Browse files
authored
add take element screenshot for oss (#33)
* add take element screenshot for oss * add a unit test * fix rubocop
1 parent 9dd9c83 commit 415b908

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

lib/appium_lib_core/common/command.rb

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module Commands
4343
hide_keyboard: [:post, 'session/:session_id/appium/device/hide_keyboard'.freeze],
4444
press_keycode: [:post, 'session/:session_id/appium/device/press_keycode'.freeze],
4545
long_press_keycode: [:post, 'session/:session_id/appium/device/long_press_keycode'.freeze],
46+
## take_element_screenshot is for MJSONWP. W3C already has.
47+
take_element_screenshot: [:get, 'session/:session_id/element/:id/screenshot'.freeze],
4648
# keyevent is only for Selendroid
4749
keyevent: [:post, 'session/:session_id/appium/device/keyevent'.freeze],
4850
set_immediate_value: [:post, 'session/:session_id/appium/element/:id/value'.freeze],

lib/appium_lib_core/common/device.rb

+23
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ module Device
375375
# @driver.switch_to_default_context
376376
#
377377

378+
# @!method take_element_screenshot(element, png_path)
379+
# @param [Selenium::WebDriver::Element] element A element you'd like to take screenshot.
380+
# @param [String] png_path A path to save the screenshot
381+
# @return [File] Path to the screenshot.
382+
#
383+
# @example
384+
#
385+
# @driver.take_element_screenshot(element, "fine_name.png")
386+
#
387+
378388
####
379389
## class << self
380390
####
@@ -454,6 +464,19 @@ def hide_keyboard(close_key = nil, strategy = nil)
454464
end
455465
end
456466

467+
add_endpoint_method(:take_element_screenshot) do
468+
def take_element_screenshot(element, png_path)
469+
result = execute :take_element_screenshot, id: element.ref
470+
471+
extension = File.extname(png_path).downcase
472+
if extension != '.png'
473+
WebDriver.logger.warn 'name used for saved screenshot does not match file type. '\
474+
'It should end with .png extension'
475+
end
476+
File.open(png_path, 'wb') { |f| f << result.unpack('m')[0] }
477+
end
478+
end
479+
457480
add_endpoint_method(:set_immediate_value) do
458481
def set_immediate_value(element, *value)
459482
keys = ::Selenium::WebDriver::Keys.encode(value)

test/functional/android/android/device_test.rb

+9
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ def test_within_context
280280
assert_equal 'App', result.text
281281
end
282282

283+
def test_take_element_screenshot
284+
e = @@core.wait { @@driver.find_element :accessibility_id, 'App' }
285+
@@driver.take_element_screenshot(e, 'take_element_screenshot.png')
286+
287+
assert File.exist? 'take_element_screenshot.png'
288+
289+
File.delete 'take_element_screenshot.png'
290+
end
291+
283292
private
284293

285294
def scroll_to(text)

test/unit/android/device_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_with_arg_definitions
5151
:keyevent,
5252
:press_keycode,
5353
:long_press_keycode,
54+
:take_element_screenshot,
5455
:set_immediate_value,
5556
:replace_value,
5657
:push_file,
@@ -351,6 +352,15 @@ def test_long_press_keycode
351352
assert_requested(:post, "#{SESSION}/appium/device/long_press_keycode", times: 1)
352353
end
353354

355+
def test_take_element_screenshot
356+
stub_request(:get, "#{SESSION}/element/id/screenshot")
357+
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)
358+
359+
@driver.take_element_screenshot ::Selenium::WebDriver::Element.new(@driver.send(:bridge), 'id'), 'sample.png'
360+
361+
assert_requested(:get, "#{SESSION}/element/id/screenshot", times: 1)
362+
end
363+
354364
def test_set_immediate_value
355365
stub_request(:post, "#{SESSION}/appium/element/id/value")
356366
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)

test/unit/script/commands_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_get_all_command_path
2222

2323
# depends on webdriver-version... (number of commands)
2424
def test_implemented_mjsonwp_commands
25-
assert_equal 128, @c.implemented_mjsonwp_commands.length
25+
assert_equal 129, @c.implemented_mjsonwp_commands.length
2626
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_mjsonwp_commands.first
2727

2828
# pick up an arbitrary command
@@ -38,7 +38,7 @@ def test_implemented_w3c_commands
3838
end
3939

4040
def test_implemented_core_commands
41-
assert_equal 43, @c.implemented_core_commands.length
41+
assert_equal 44, @c.implemented_core_commands.length
4242
assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_core_commands.first
4343

4444
# pick up an arbitrary command

0 commit comments

Comments
 (0)