From 52b7301e7f819788751071e661c2fe056171a3a5 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 17 May 2024 17:03:11 -0700 Subject: [PATCH 1/5] add temporary implementation to modify add_command --- lib/appium_lib_core/common/base/bridge.rb | 15 +- lib/appium_lib_core/common/base/driver.rb | 34 ++++- lib/appium_lib_core/device.rb | 5 + .../android/webdriver/w3c/commands_test.rb | 139 +++++++++--------- 4 files changed, 114 insertions(+), 79 deletions(-) diff --git a/lib/appium_lib_core/common/base/bridge.rb b/lib/appium_lib_core/common/base/bridge.rb index 3d6b5c3a..d70202dc 100644 --- a/lib/appium_lib_core/common/base/bridge.rb +++ b/lib/appium_lib_core/common/base/bridge.rb @@ -173,13 +173,18 @@ def json_create(value) # Bridge.add_command name, method, url, &block # end - def add_command(method:, url:, name:, &block) - ::Appium::Logger.info "Overriding the method '#{name}' for '#{url}'" if @available_commands.key? name + # def add_command(method:, url:, name:, &block) + # ::Appium::Logger.info "Overriding the method '#{name}' for '#{url}'" if @available_commands.key? name - @available_commands[name] = [method, url] + # @available_commands[name] = [method, url] - ::Appium::Core::Device.add_endpoint_method name, &block - end + # if block_given? + # Bridge.add_command name, method, url, &block + # else + # Bridge.add_command(name, method, url) { execute method } + # end + # ::Appium::Core::Device.add_endpoint_method_only_driver name, &block + # end def commands(command) @available_commands[command] || Bridge.extra_commands[command] diff --git a/lib/appium_lib_core/common/base/driver.rb b/lib/appium_lib_core/common/base/driver.rb index 3a3d0501..51ec1ed1 100644 --- a/lib/appium_lib_core/common/base/driver.rb +++ b/lib/appium_lib_core/common/base/driver.rb @@ -27,6 +27,12 @@ module Appium module Core class Base class Driver < ::Selenium::WebDriver::Driver + class << self + def add_command(name, &block) + define_method(name, &block) + end + end + include ::Selenium::WebDriver::DriverExtensions::UploadsFiles include ::Selenium::WebDriver::DriverExtensions::HasSessionId include ::Selenium::WebDriver::DriverExtensions::HasWebStorage @@ -189,14 +195,34 @@ def update_sending_request_to(protocol:, host:, port:, path:) # @driver.test_action_command(e.id, 'action') # def add_command(method:, url:, name:, &block) - unless AVAILABLE_METHODS.include? method - raise ::Appium::Core::Error::ArgumentError, "Available method is either #{AVAILABLE_METHODS}" - end + # unless AVAILABLE_METHODS.include? method + # raise ::Appium::Core::Error::ArgumentError, "Available method is either #{AVAILABLE_METHODS}" + # end # TODO: Remove this logger before Appium 2.0 release ::Appium::Logger.info '[Experimental] this method is experimental for Appium 2.0. This interface may change.' - @bridge.add_command method: method, url: url, name: name, &block + # ::Appium::Core::Base::Driver.add_command(name) { bridge.execute method } + + if block_given? + puts "aaaaaaa" + ::Appium::Core::Base::Bridge.add_command name, method, url, &block + else + ::Appium::Core::Base::Bridge.add_command(name, method, url) { + execute name + } + end + # define_method define_method(name, &block) + end + + # todo: need to add dynamic + # def test_command + # @bridge.test_command + # end + + def test_command(argument) + puts "in driver level" + @bridge.test_command(argument, 'dummy') end ### Methods for Appium diff --git a/lib/appium_lib_core/device.rb b/lib/appium_lib_core/device.rb index eb716c98..d9135d20 100644 --- a/lib/appium_lib_core/device.rb +++ b/lib/appium_lib_core/device.rb @@ -46,6 +46,11 @@ def extended(_mod) # @private # Define method in Bridges + def add_endpoint_method_only_driver(method, &block) + delegate_driver_method method + delegate_from_appium_driver method + end + def add_endpoint_method(method, &block) block_given? ? create_bridge_command(method, &block) : create_bridge_command(method) diff --git a/test/unit/android/webdriver/w3c/commands_test.rb b/test/unit/android/webdriver/w3c/commands_test.rb index 8d150073..fccbe3a7 100644 --- a/test/unit/android/webdriver/w3c/commands_test.rb +++ b/test/unit/android/webdriver/w3c/commands_test.rb @@ -28,32 +28,31 @@ def setup @driver ||= android_mock_create_session_w3c end - def test_add_command - @driver.add_command( - method: :get, - url: 'session/:session_id/path/to/custom/url', - name: :test_command - ) + # def test_add_command + # @driver.add_command( + # method: :get, + # url: 'session/:session_id/path/to/custom/url', + # name: :test_command + # ) - assert_equal @driver.respond_to?(:test_command), true + # assert_equal @driver.respond_to?(:test_command), true - stub_request(:get, "#{SESSION}/path/to/custom/url") - .to_return(headers: HEADER, status: 200, body: { value: 'xxxx' }.to_json) + # stub_request(:get, "#{SESSION}/path/to/custom/url") + # .to_return(headers: HEADER, status: 200, body: { value: 'xxxx' }.to_json) - @driver.test_command + # @driver.test_command - assert_requested(:get, "#{SESSION}/path/to/custom/url", times: 1) - end + # assert_requested(:get, "#{SESSION}/path/to/custom/url", times: 1) + # end def test_add_command_block @driver.add_command( method: :post, url: 'session/:session_id/path/to/custom/url', name: :test_command - ) do - def test_command(argument) - execute(:test_command, {}, { dummy: argument }) - end + ) do |arg1, arg2| + puts "in bridge level" + execute(:test_command, {}, { dummy: arg1 }) end assert_equal @driver.respond_to?(:test_command), true @@ -67,60 +66,60 @@ def test_command(argument) assert_requested(:post, "#{SESSION}/path/to/custom/url", times: 1) end - def test_add_command_block_element_id - @driver.add_command( - method: :post, - url: 'session/:session_id/path/to/custom/:element_id/url', - name: :test_command - ) do - def test_command(argument) - execute(:test_command, { element_id: 'dummy_element_id' }, { dummy: argument }) - end - end - - assert_equal @driver.respond_to?(:test_command), true - - stub_request(:post, "#{SESSION}/path/to/custom/dummy_element_id/url") - .with(body: { dummy: 1 }.to_json) - .to_return(headers: HEADER, status: 200, body: { value: nil }.to_json) - - @driver.test_command(1) - - assert_requested(:post, "#{SESSION}/path/to/custom/dummy_element_id/url", times: 1) - end - - def test_add_command_error - assert_raises ::Appium::Core::Error::ArgumentError do - @driver.add_command( - method: :invalid_method, - url: 'session/:session_id/path/to/custom/url', - name: :test_command - ) - end - end - - def test_add_command_already_defined_without_error - stub_request(:get, "#{SESSION}/path/to/custom/url") - .to_return(headers: HEADER, status: 200, body: { value: 'xxxx' }.to_json) - - @driver.add_command( - method: :get, - url: 'session/:session_id/path/to/custom/url', - name: :test_command - ) - assert_equal @driver.respond_to?(:test_command), true - - @driver.add_command( - method: :get, - url: 'session/:session_id/path/to/custom/url', - name: :test_command - ) - assert_equal @driver.respond_to?(:test_command), true - - @driver.test_command - - assert_requested(:get, "#{SESSION}/path/to/custom/url", times: 1) - end + # def test_add_command_block_element_id + # @driver.add_command( + # method: :post, + # url: 'session/:session_id/path/to/custom/:element_id/url', + # name: :test_command + # ) do + # def test_command(argument) + # execute(:test_command, { element_id: 'dummy_element_id' }, { dummy: argument }) + # end + # end + + # assert_equal @driver.respond_to?(:test_command), true + + # stub_request(:post, "#{SESSION}/path/to/custom/dummy_element_id/url") + # .with(body: { dummy: 1 }.to_json) + # .to_return(headers: HEADER, status: 200, body: { value: nil }.to_json) + + # @driver.test_command(1) + + # assert_requested(:post, "#{SESSION}/path/to/custom/dummy_element_id/url", times: 1) + # end + + # def test_add_command_error + # assert_raises ::Appium::Core::Error::ArgumentError do + # @driver.add_command( + # method: :invalid_method, + # url: 'session/:session_id/path/to/custom/url', + # name: :test_command + # ) + # end + # end + + # def test_add_command_already_defined_without_error + # stub_request(:get, "#{SESSION}/path/to/custom/url") + # .to_return(headers: HEADER, status: 200, body: { value: 'xxxx' }.to_json) + + # @driver.add_command( + # method: :get, + # url: 'session/:session_id/path/to/custom/url', + # name: :test_command + # ) + # assert_equal @driver.respond_to?(:test_command), true + + # @driver.add_command( + # method: :get, + # url: 'session/:session_id/path/to/custom/url', + # name: :test_command + # ) + # assert_equal @driver.respond_to?(:test_command), true + + # @driver.test_command + + # assert_requested(:get, "#{SESSION}/path/to/custom/url", times: 1) + # end def test_no_session_id response = { From 98df1b812e0ea5f7cb189467f582dabcc2bee149 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 17 May 2024 17:04:38 -0700 Subject: [PATCH 2/5] fix remove --- lib/appium_lib_core/device.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/appium_lib_core/device.rb b/lib/appium_lib_core/device.rb index d9135d20..eb716c98 100644 --- a/lib/appium_lib_core/device.rb +++ b/lib/appium_lib_core/device.rb @@ -46,11 +46,6 @@ def extended(_mod) # @private # Define method in Bridges - def add_endpoint_method_only_driver(method, &block) - delegate_driver_method method - delegate_from_appium_driver method - end - def add_endpoint_method(method, &block) block_given? ? create_bridge_command(method, &block) : create_bridge_command(method) From f610036bd75be6d64b04d19f2a8ffd4cabcc3827 Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 17 May 2024 17:07:52 -0700 Subject: [PATCH 3/5] remove unnecessary temporary implementation --- lib/appium_lib_core/common/base/driver.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/appium_lib_core/common/base/driver.rb b/lib/appium_lib_core/common/base/driver.rb index 51ec1ed1..d8af9ad5 100644 --- a/lib/appium_lib_core/common/base/driver.rb +++ b/lib/appium_lib_core/common/base/driver.rb @@ -27,12 +27,6 @@ module Appium module Core class Base class Driver < ::Selenium::WebDriver::Driver - class << self - def add_command(name, &block) - define_method(name, &block) - end - end - include ::Selenium::WebDriver::DriverExtensions::UploadsFiles include ::Selenium::WebDriver::DriverExtensions::HasSessionId include ::Selenium::WebDriver::DriverExtensions::HasWebStorage @@ -205,7 +199,6 @@ def add_command(method:, url:, name:, &block) # ::Appium::Core::Base::Driver.add_command(name) { bridge.execute method } if block_given? - puts "aaaaaaa" ::Appium::Core::Base::Bridge.add_command name, method, url, &block else ::Appium::Core::Base::Bridge.add_command(name, method, url) { From 8835e4eb108e4c930265f90e2674157e63a6210e Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 17 May 2024 17:27:58 -0700 Subject: [PATCH 4/5] add few args --- lib/appium_lib_core/common/base/driver.rb | 18 +++++++++++++++--- .../android/webdriver/w3c/commands_test.rb | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/appium_lib_core/common/base/driver.rb b/lib/appium_lib_core/common/base/driver.rb index d8af9ad5..8b8a03fc 100644 --- a/lib/appium_lib_core/common/base/driver.rb +++ b/lib/appium_lib_core/common/base/driver.rb @@ -27,6 +27,11 @@ module Appium module Core class Base class Driver < ::Selenium::WebDriver::Driver + class << self + def add_command(name, &block) + define_method(name, &block) + end + end include ::Selenium::WebDriver::DriverExtensions::UploadsFiles include ::Selenium::WebDriver::DriverExtensions::HasSessionId include ::Selenium::WebDriver::DriverExtensions::HasWebStorage @@ -196,15 +201,18 @@ def add_command(method:, url:, name:, &block) # TODO: Remove this logger before Appium 2.0 release ::Appium::Logger.info '[Experimental] this method is experimental for Appium 2.0. This interface may change.' - # ::Appium::Core::Base::Driver.add_command(name) { bridge.execute method } - if block_given? + # ::Appium::Core::Base::Driver.add_command(name, &block) ::Appium::Core::Base::Bridge.add_command name, method, url, &block else + # ::Appium::Core::Base::Driver.add_command(name) { + # execute name + # } ::Appium::Core::Base::Bridge.add_command(name, method, url) { execute name } end + # define_method define_method(name, &block) end @@ -215,9 +223,13 @@ def add_command(method:, url:, name:, &block) def test_command(argument) puts "in driver level" - @bridge.test_command(argument, 'dummy') + @bridge.test_command(argument) end + # def execute(command, opts = {}, command_hash = nil) + # @bridge.execute(command, opts, command_hash) + # end + ### Methods for Appium # Perform 'key' actions for W3C module. diff --git a/test/unit/android/webdriver/w3c/commands_test.rb b/test/unit/android/webdriver/w3c/commands_test.rb index fccbe3a7..40609fd5 100644 --- a/test/unit/android/webdriver/w3c/commands_test.rb +++ b/test/unit/android/webdriver/w3c/commands_test.rb @@ -50,7 +50,7 @@ def test_add_command_block method: :post, url: 'session/:session_id/path/to/custom/url', name: :test_command - ) do |arg1, arg2| + ) do |arg1| puts "in bridge level" execute(:test_command, {}, { dummy: arg1 }) end From 9319dca239418d6338e30513753c18ccc19aa38b Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Fri, 17 May 2024 17:41:54 -0700 Subject: [PATCH 5/5] remove a line --- lib/appium_lib_core/common/base/bridge.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/appium_lib_core/common/base/bridge.rb b/lib/appium_lib_core/common/base/bridge.rb index d70202dc..abcfbb15 100644 --- a/lib/appium_lib_core/common/base/bridge.rb +++ b/lib/appium_lib_core/common/base/bridge.rb @@ -183,7 +183,6 @@ def json_create(value) # else # Bridge.add_command(name, method, url) { execute method } # end - # ::Appium::Core::Device.add_endpoint_method_only_driver name, &block # end def commands(command)