diff --git a/lib/appium_lib_core/common/base/bridge.rb b/lib/appium_lib_core/common/base/bridge.rb index 732db989..7b815bad 100644 --- a/lib/appium_lib_core/common/base/bridge.rb +++ b/lib/appium_lib_core/common/base/bridge.rb @@ -173,13 +173,17 @@ 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 + # 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..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 @@ -189,16 +194,42 @@ 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 + 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 + # todo: need to add dynamic + # def test_command + # @bridge.test_command + # end + + def test_command(argument) + puts "in driver level" + @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 8d150073..40609fd5 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| + 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 = {