From 2936e66d38e41068a453453773aee0c927304ef3 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Wed, 28 Mar 2018 08:31:21 +0100 Subject: [PATCH 1/7] add clipboard api --- lib/appium_lib_core/common/command.rb | 2 ++ lib/appium_lib_core/common/device.rb | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/appium_lib_core/common/command.rb b/lib/appium_lib_core/common/command.rb index 30fa3d85..2c68407e 100644 --- a/lib/appium_lib_core/common/command.rb +++ b/lib/appium_lib_core/common/command.rb @@ -56,6 +56,8 @@ module Commands push_file: [:post, 'session/:session_id/appium/device/push_file'.freeze], pull_file: [:post, 'session/:session_id/appium/device/pull_file'.freeze], pull_folder: [:post, 'session/:session_id/appium/device/pull_folder'.freeze], + get_clipboard: [:post, 'session/:session_id/appium/device/get_clipboard'.freeze], + set_clipboard: [:post, 'session/:session_id/appium/device/set_clipboard'.freeze], get_settings: [:get, 'session/:session_id/appium/settings'.freeze], update_settings: [:post, 'session/:session_id/appium/settings'.freeze], touch_actions: [:post, 'session/:session_id/touch/perform'.freeze], diff --git a/lib/appium_lib_core/common/device.rb b/lib/appium_lib_core/common/device.rb index 2bc03a88..f2dd0ed0 100644 --- a/lib/appium_lib_core/common/device.rb +++ b/lib/appium_lib_core/common/device.rb @@ -657,6 +657,7 @@ def save_viewport_screenshot(png_path) add_handling_context add_screen_recording add_app_management + add_clipboard end # def extended @@ -912,6 +913,27 @@ def stop_and_save_recording_screen(file_path) end end end + + def add_clipboard + add_endpoint_method(:get_clipboard) do + def get_clipboard(content_type: nil) + params = {} + params[:contentType] = content_type unless content_type.nil? + + execute(:get_clipboard, {}, params) + end + end + + add_endpoint_method(:set_clipboard) do + def set_clipboard(content:, content_type: nil, label: nil) + params = { content: content } + params[:contentType] = content_type unless content_type.nil? + params[:label] = label unless label.nil? + + execute(:get_clipboard, {}, params) + end + end + end end # class << self end # module Device end # module Core From 9323620d9caabd537a1524ea083a4e39f9282ec6 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Wed, 28 Mar 2018 08:34:12 +0100 Subject: [PATCH 2/7] add unit test --- test/unit/android/device_test.rb | 4 +++- test/unit/android/device_w3c_test.rb | 4 +++- test/unit/ios/device_test.rb | 2 ++ test/unit/ios/device_w3c_test.rb | 2 ++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/unit/android/device_test.rb b/test/unit/android/device_test.rb index 76731774..f4738bdf 100644 --- a/test/unit/android/device_test.rb +++ b/test/unit/android/device_test.rb @@ -65,7 +65,9 @@ def test_with_arg_definitions :start_activity, :end_coverage, :set_network_connection, - :get_performance_data]) + :get_performance_data, + :get_clipboard, + :set_clipboard]) end ## no args diff --git a/test/unit/android/device_w3c_test.rb b/test/unit/android/device_w3c_test.rb index 1001db51..20e06065 100644 --- a/test/unit/android/device_w3c_test.rb +++ b/test/unit/android/device_w3c_test.rb @@ -65,7 +65,9 @@ def test_with_arg_definitions :start_activity, :end_coverage, :set_network_connection, - :get_performance_data]) + :get_performance_data, + :get_clipboard, + :set_clipboard]) end ## no args diff --git a/test/unit/ios/device_test.rb b/test/unit/ios/device_test.rb index b488de82..d8be38d8 100644 --- a/test/unit/ios/device_test.rb +++ b/test/unit/ios/device_test.rb @@ -47,6 +47,8 @@ def test_with_arg_definitions :push_file, :pull_file, :pull_folder, + :get_clipboard, + :set_clipboard, :get_settings, :update_settings, :touch_actions, diff --git a/test/unit/ios/device_w3c_test.rb b/test/unit/ios/device_w3c_test.rb index 099b62eb..34278c5b 100644 --- a/test/unit/ios/device_w3c_test.rb +++ b/test/unit/ios/device_w3c_test.rb @@ -47,6 +47,8 @@ def test_with_arg_definitions :push_file, :pull_file, :pull_folder, + :get_clipboard, + :set_clipboard, :get_settings, :update_settings, :touch_actions, From bea1588520eba016a3be6ec4b59a2e328fd1bf2f Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Thu, 29 Mar 2018 07:11:52 +0100 Subject: [PATCH 3/7] separate android and ios --- lib/appium_lib_core/android/device.rb | 38 +++++++++++++++++++ lib/appium_lib_core/common/device.rb | 22 ----------- lib/appium_lib_core/ios/device.rb | 33 ++++++++++++++++ .../functional/android/android/device_test.rb | 8 ++++ test/functional/ios/ios/device_test.rb | 8 ++++ 5 files changed, 87 insertions(+), 22 deletions(-) diff --git a/lib/appium_lib_core/android/device.rb b/lib/appium_lib_core/android/device.rb index 905954be..86f766d0 100644 --- a/lib/appium_lib_core/android/device.rb +++ b/lib/appium_lib_core/android/device.rb @@ -1,4 +1,5 @@ require_relative 'device/emulator' +require 'base64' module Appium module Android @@ -117,6 +118,17 @@ module Device # @driver.start_recording_screen video_size: '1280x720', time_limit: '180', bit_rate: '5000000' # + # @!method set_clipboard(content:, content_type:, label:) + # Set the content of device's clipboard. + # @param [String] label: clipboard data label. + # @param [String] content_type: one of supported content types. + # @param [String] content: base64-encoded content to be set. + # + # @example + # + # @driver.get_performance_data package_name: package_name, data_type: data_type, data_read_timeout: 2 + # + #### ## class << self #### @@ -181,6 +193,7 @@ def get_performance_data(package_name:, data_type:, data_read_timeout: 1000) end add_screen_recording + add_clipboard Emulator.emulator_commands end @@ -209,6 +222,31 @@ def start_recording_screen(remote_path: nil, user: nil, pass: nil, method: 'PUT' # rubocop:enable Metrics/ParameterLists end end + + def add_clipboard + ::Appium::Core::Device.add_endpoint_method(:get_clipboard) do + def get_clipboard(content_type: :plaintext) + raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + params = { contentType: content_type } + + execute(:get_clipboard, {}, params) + end + end + + ::Appium::Core::Device.add_endpoint_method(:set_clipboard) do + def set_clipboard(content:, content_type: :plaintext, label: nil) + raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + + params = { + contentType: content_type, + content: Base64.encode64(content) + } + params[:label] = label unless label.nil? + + execute(:set_clipboard, {}, params) + end + end + end end end # module Device end # module Android diff --git a/lib/appium_lib_core/common/device.rb b/lib/appium_lib_core/common/device.rb index f2dd0ed0..2bc03a88 100644 --- a/lib/appium_lib_core/common/device.rb +++ b/lib/appium_lib_core/common/device.rb @@ -657,7 +657,6 @@ def save_viewport_screenshot(png_path) add_handling_context add_screen_recording add_app_management - add_clipboard end # def extended @@ -913,27 +912,6 @@ def stop_and_save_recording_screen(file_path) end end end - - def add_clipboard - add_endpoint_method(:get_clipboard) do - def get_clipboard(content_type: nil) - params = {} - params[:contentType] = content_type unless content_type.nil? - - execute(:get_clipboard, {}, params) - end - end - - add_endpoint_method(:set_clipboard) do - def set_clipboard(content:, content_type: nil, label: nil) - params = { content: content } - params[:contentType] = content_type unless content_type.nil? - params[:label] = label unless label.nil? - - execute(:get_clipboard, {}, params) - end - end - end end # class << self end # module Device end # module Core diff --git a/lib/appium_lib_core/ios/device.rb b/lib/appium_lib_core/ios/device.rb index 9a595b9b..4ab705d2 100644 --- a/lib/appium_lib_core/ios/device.rb +++ b/lib/appium_lib_core/ios/device.rb @@ -1,3 +1,5 @@ +require 'base64' + module Appium module Ios module Device @@ -47,6 +49,37 @@ def toggle_touch_id_enrollment(enabled = true) execute :toggle_touch_id_enrollment, {}, enabled: enabled end end + + add_clipboard + end + + private + + def add_clipboard + ::Appium::Core::Device.add_endpoint_method(:get_clipboard) do + def get_clipboard(content_type: :plaintext) + raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + + params = {} + params[:contentType] = content_type + + data = execute(:get_clipboard, {}, params) + Base64.decode64 data + end + end + + ::Appium::Core::Device.add_endpoint_method(:set_clipboard) do + def set_clipboard(content:, content_type: :plaintext) + raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + + params = { + contentType: content_type, + content: Base64.encode64(content) + } + + execute(:set_clipboard, {}, params) + end + end end end end # module Device diff --git a/test/functional/android/android/device_test.rb b/test/functional/android/android/device_test.rb index 694bb719..1fb48c17 100644 --- a/test/functional/android/android/device_test.rb +++ b/test/functional/android/android/device_test.rb @@ -337,6 +337,14 @@ def test_viewport_screenshot assert !File.exist?(file.path) end + def test_clipbord + input = 'happy testing' + + @@driver.set_clipboard(content: input, label: 'Note') + + assert_equal input, @@driver.get_clipboard + end + private def scroll_to(text) diff --git a/test/functional/ios/ios/device_test.rb b/test/functional/ios/ios/device_test.rb index 6a2e2a92..67cd51bd 100644 --- a/test/functional/ios/ios/device_test.rb +++ b/test/functional/ios/ios/device_test.rb @@ -251,6 +251,14 @@ def test_start_performance_record_and_stop File.delete file.path assert !File.exist?(file.path) end + + def test_clipbord + input = 'happy testing' + + @@driver.set_clipboard(content: input) + + assert_equal input, @@driver.get_clipboard + end end end end From bdad4efb75af521a8bf933c650637e735c161abe Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Thu, 29 Mar 2018 08:30:40 +0100 Subject: [PATCH 4/7] update docstring and introduce clipbord content type constant --- lib/appium_lib_core.rb | 1 + lib/appium_lib_core/android/device.rb | 25 +++++++++++++---- .../device/clipboard_content_type.rb | 9 ++++++ lib/appium_lib_core/ios/device.rb | 28 +++++++++++++++++-- 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 lib/appium_lib_core/device/clipboard_content_type.rb diff --git a/lib/appium_lib_core.rb b/lib/appium_lib_core.rb index 3ee01227..d560741b 100644 --- a/lib/appium_lib_core.rb +++ b/lib/appium_lib_core.rb @@ -10,6 +10,7 @@ require_relative 'appium_lib_core/device/multi_touch' require_relative 'appium_lib_core/device/screen_record' require_relative 'appium_lib_core/device/app_state' +require_relative 'appium_lib_core/device/clipboard_content_type' require_relative 'appium_lib_core/android' require_relative 'appium_lib_core/android_uiautomator2' diff --git a/lib/appium_lib_core/android/device.rb b/lib/appium_lib_core/android/device.rb index 86f766d0..92907164 100644 --- a/lib/appium_lib_core/android/device.rb +++ b/lib/appium_lib_core/android/device.rb @@ -118,15 +118,25 @@ module Device # @driver.start_recording_screen video_size: '1280x720', time_limit: '180', bit_rate: '5000000' # - # @!method set_clipboard(content:, content_type:, label:) + # @!method get_clipboard(content_type: :plaintext) + # Set the content of device's clipboard. + # @param [String] content_type: one of supported content types. + # @return [String] + # + # @example + # + # @driver.get_clipboard #=> "happy testing" + # + + # @!method set_clipboard(content:, content_type: :plaintext, label: nil) # Set the content of device's clipboard. # @param [String] label: clipboard data label. # @param [String] content_type: one of supported content types. - # @param [String] content: base64-encoded content to be set. + # @param [String] content: Contents to be set. (Will encode with base64-encoded inside this method) # # @example # - # @driver.get_performance_data package_name: package_name, data_type: data_type, data_read_timeout: 2 + # @driver.set_clipboard(content: 'happy testing') #=> {"protocol"=>"W3C"} # #### @@ -226,7 +236,10 @@ def start_recording_screen(remote_path: nil, user: nil, pass: nil, method: 'PUT' def add_clipboard ::Appium::Core::Device.add_endpoint_method(:get_clipboard) do def get_clipboard(content_type: :plaintext) - raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + unless ::Appium::Core::Device::Clipboard::CONTENT_TYPE.member?(content_type) + raise "content_type should be #{::Appium::Core::Device::Clipboard::CONTENT_TYPE}" + end + params = { contentType: content_type } execute(:get_clipboard, {}, params) @@ -235,7 +248,9 @@ def get_clipboard(content_type: :plaintext) ::Appium::Core::Device.add_endpoint_method(:set_clipboard) do def set_clipboard(content:, content_type: :plaintext, label: nil) - raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + unless ::Appium::Core::Device::Clipboard::CONTENT_TYPE.member?(content_type) + raise "content_type should be #{::Appium::Core::Device::Clipboard::CONTENT_TYPE}" + end params = { contentType: content_type, diff --git a/lib/appium_lib_core/device/clipboard_content_type.rb b/lib/appium_lib_core/device/clipboard_content_type.rb new file mode 100644 index 00000000..d6b51a34 --- /dev/null +++ b/lib/appium_lib_core/device/clipboard_content_type.rb @@ -0,0 +1,9 @@ +module Appium + module Core + module Device + module Clipboard + CONTENT_TYPE = [:plaintext, :image, :url].freeze + end + end + end +end diff --git a/lib/appium_lib_core/ios/device.rb b/lib/appium_lib_core/ios/device.rb index 4ab705d2..c0254daf 100644 --- a/lib/appium_lib_core/ios/device.rb +++ b/lib/appium_lib_core/ios/device.rb @@ -30,6 +30,26 @@ module Device # @driver.toggle_touch_id_enrollment false #=> Disable toggle enrolled # + # @!method get_clipboard(content_type: :plaintext) + # Set the content of device's clipboard. + # @param [String] content_type: one of supported content types. + # @return [String] + # + # @example + # + # @driver.get_clipboard #=> "happy testing" + # + + # @!method set_clipboard(content:, content_type: :plaintext) + # Set the content of device's clipboard. + # @param [String] content_type: one of supported content types. + # @param [String] content: Contents to be set. (Will encode with base64-encoded inside this method) + # + # @example + # + # @driver.set_clipboard(content: 'happy testing') #=> {"protocol"=>"W3C"} + # + #### ## class << self #### @@ -58,7 +78,9 @@ def toggle_touch_id_enrollment(enabled = true) def add_clipboard ::Appium::Core::Device.add_endpoint_method(:get_clipboard) do def get_clipboard(content_type: :plaintext) - raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + unless ::Appium::Core::Device::Clipboard::CONTENT_TYPE.member?(content_type) + raise "content_type should be #{::Appium::Core::Device::Clipboard::CONTENT_TYPE}" + end params = {} params[:contentType] = content_type @@ -70,7 +92,9 @@ def get_clipboard(content_type: :plaintext) ::Appium::Core::Device.add_endpoint_method(:set_clipboard) do def set_clipboard(content:, content_type: :plaintext) - raise 'content_type should be [:plaintext, :image, :url]' unless [:plaintext, :image, :url].member?(content_type) + unless ::Appium::Core::Device::Clipboard::CONTENT_TYPE.member?(content_type) + raise "content_type should be #{::Appium::Core::Device::Clipboard::CONTENT_TYPE}" + end params = { contentType: content_type, From 39dc0869de341a59db5acc4a9686d388e90f346a Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Thu, 29 Mar 2018 19:24:06 +0100 Subject: [PATCH 5/7] fix tests --- test/unit/script/commands_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/script/commands_test.rb b/test/unit/script/commands_test.rb index f1cfa2bc..d875686d 100644 --- a/test/unit/script/commands_test.rb +++ b/test/unit/script/commands_test.rb @@ -22,7 +22,7 @@ def test_get_all_command_path # depends on webdriver-version... (number of commands) def test_implemented_mjsonwp_commands - assert_equal 142, @c.implemented_mjsonwp_commands.length + assert_equal 144, @c.implemented_mjsonwp_commands.length assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_mjsonwp_commands.first # pick up an arbitrary command @@ -30,7 +30,7 @@ def test_implemented_mjsonwp_commands end def test_implemented_w3c_commands - assert_equal 113, @c.implemented_w3c_commands.length + assert_equal 115, @c.implemented_w3c_commands.length assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_w3c_commands.first # pick up an arbitrary command @@ -38,7 +38,7 @@ def test_implemented_w3c_commands end def test_implemented_core_commands - assert_equal 56, @c.implemented_core_commands.length + assert_equal 58, @c.implemented_core_commands.length assert_equal ['session/:session_id/contexts', [:get]], @c.implemented_core_commands.first # pick up an arbitrary command From 8b2046dd954639c3b73c2077ddfb8eeeb6f17dba Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Fri, 30 Mar 2018 07:47:07 +0100 Subject: [PATCH 6/7] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45a83c99..1105209e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## [Unreleased] ### Enhancements - Add a `bug_report` option in `start_recording_screen`, Android +- Add clipboard apis [#69](https://github.com/appium/ruby_lib_core/pull/69) ### Bug fixes From efc5a08f451cc6b529a61ef0adb4d2a67fd54641 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Fri, 30 Mar 2018 10:42:29 +0100 Subject: [PATCH 7/7] decode for android response --- lib/appium_lib_core/android/device.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/appium_lib_core/android/device.rb b/lib/appium_lib_core/android/device.rb index 92907164..a131d04e 100644 --- a/lib/appium_lib_core/android/device.rb +++ b/lib/appium_lib_core/android/device.rb @@ -242,7 +242,8 @@ def get_clipboard(content_type: :plaintext) params = { contentType: content_type } - execute(:get_clipboard, {}, params) + data = execute(:get_clipboard, {}, params) + Base64.decode64 data end end