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 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 905954be..a131d04e 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,27 @@ module Device # @driver.start_recording_screen video_size: '1280x720', time_limit: '180', bit_rate: '5000000' # + # @!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: Contents to be set. (Will encode with base64-encoded inside this method) + # + # @example + # + # @driver.set_clipboard(content: 'happy testing') #=> {"protocol"=>"W3C"} + # + #### ## class << self #### @@ -181,6 +203,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 +232,37 @@ 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) + 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 } + + 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, label: nil) + 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, + 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/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/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 9a595b9b..c0254daf 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 @@ -28,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 #### @@ -47,6 +69,41 @@ 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) + 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 + + 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) + 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, + 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 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, 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