diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b2d3694..89155272 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Read `release_notes.md` for commit level details. ## [Unreleased] ### Enhancements +- Allow to override an existing method by `Appium::Core::Base::Driver#add_command` since Appium drivers/plugins allow to override them ### Bug fixes diff --git a/lib/appium_lib_core/common/base/bridge/mjsonwp.rb b/lib/appium_lib_core/common/base/bridge/mjsonwp.rb index 0ddd0d66..a4ede9b4 100644 --- a/lib/appium_lib_core/common/base/bridge/mjsonwp.rb +++ b/lib/appium_lib_core/common/base/bridge/mjsonwp.rb @@ -46,7 +46,7 @@ def commands(command) # command for Appium 2.0. def add_command(method:, url:, name:, &block) - raise ::Appium::Core::Error::ArgumentError, "#{name} is already defined" if @available_commands.key? name + ::Appium::Logger.debug "Overriding the method '#{name}' for '#{url}'" if @available_commands.key? name @available_commands[name] = [method, url] diff --git a/lib/appium_lib_core/common/base/bridge/w3c.rb b/lib/appium_lib_core/common/base/bridge/w3c.rb index ab78cbd0..f5dd7f58 100644 --- a/lib/appium_lib_core/common/base/bridge/w3c.rb +++ b/lib/appium_lib_core/common/base/bridge/w3c.rb @@ -47,7 +47,7 @@ def commands(command) # command for Appium 2.0. def add_command(method:, url:, name:, &block) - raise ::Appium::Core::Error::ArgumentError, "#{name} is already defined" if @available_commands.key? name + ::Appium::Logger.info "Overriding the method '#{name}' for '#{url}'" if @available_commands.key? name @available_commands[name] = [method, url] diff --git a/lib/appium_lib_core/common/base/driver.rb b/lib/appium_lib_core/common/base/driver.rb index b412da9d..b0464d15 100644 --- a/lib/appium_lib_core/common/base/driver.rb +++ b/lib/appium_lib_core/common/base/driver.rb @@ -100,7 +100,7 @@ def update_sending_request_to(protocol:, host:, port:, path:) # values in the hash. # The third argument should be hash. The hash will be the request body. # Please read examples below for more details. - # @raise [ArgumentError] If the given +name+ is already defined or +method+ are invalid value. + # @raise [ArgumentError] If the given +method+ are invalid value. # # @example # diff --git a/test/unit/android/webdriver/mjsonwp/commands_test.rb b/test/unit/android/webdriver/mjsonwp/commands_test.rb index e9c1eb93..306548e2 100644 --- a/test/unit/android/webdriver/mjsonwp/commands_test.rb +++ b/test/unit/android/webdriver/mjsonwp/commands_test.rb @@ -99,24 +99,27 @@ def test_add_command_error end end - def test_add_command_already_defined + 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 - assert_raises ::Appium::Core::Error::ArgumentError do - @driver.add_command( - method: :get, - url: 'session/:session_id/path/to/custom/url', - name: :test_command - ) - end + @driver.test_command - assert_equal @driver.respond_to?(:test_command), true + assert_requested(:get, "#{SESSION}/path/to/custom/url", times: 1) end def test_no_session_id diff --git a/test/unit/android/webdriver/w3c/commands_test.rb b/test/unit/android/webdriver/w3c/commands_test.rb index 96a4be8f..06532bd4 100644 --- a/test/unit/android/webdriver/w3c/commands_test.rb +++ b/test/unit/android/webdriver/w3c/commands_test.rb @@ -99,24 +99,27 @@ def test_add_command_error end end - def test_add_command_already_defined + 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 - assert_raises ::Appium::Core::Error::ArgumentError do - @driver.add_command( - method: :get, - url: 'session/:session_id/path/to/custom/url', - name: :test_command - ) - end + @driver.test_command - assert_equal @driver.respond_to?(:test_command), true + assert_requested(:get, "#{SESSION}/path/to/custom/url", times: 1) end def test_no_session_id