@@ -135,25 +135,21 @@ def update_sending_request_to(protocol:, host:, port:, path:)
135135 # drivers/plugins in Appium 2.0. Appium 2.0 and its custom drivers/plugins allow you
136136 # to define custom commands that are not part of W3C spec.
137137 #
138+ # Uses Selenium's +Bridge.add_command+ under the hood to register the command
139+ # and define the method on the bridge.
140+ #
138141 # @param [Symbol] method HTTP request method as https://www.w3.org/TR/webdriver/#endpoints
139142 # @param [string] url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
140143 # +:session_id+ is the placeholder of 'session id'.
141144 # Other place holders can be specified with +:+ prefix like +:id+.
142145 # Then, the +:id+ will be replaced with a given value as the seconds argument of +execute+
143146 # @param [Symbol] name The name of method that is called as the driver instance method.
144- # @param [Proc] block The block to involve as the method.
145- # Please define a method that has the same +name+ with arguments you want.
146- # The method must has +execute+ method. tHe +execute+ is calls the +url+
147- # with the given parameters.
148- # The first argument should be +name+ as symbol.
149- # The second argument should be hash. If keys in the hash matches +:+ prefix
150- # string in the given url, the matched string in the given url will be
151- # values in the hash.
152- # The third argument should be hash. The hash will be the request body.
153- # Please read examples below for more details.
147+ # @param [Proc] block The block becomes the method body. It is executed in the context of
148+ # the bridge instance, so +execute+ is available. When no block is given,
149+ # a default implementation calling +execute(name)+ is used.
154150 # @raise [ArgumentError] If the given +method+ is invalid value.
155151 #
156- # @example
152+ # @example Simple GET command (no block)
157153 #
158154 # @driver.add_command(
159155 # method: :get,
@@ -163,42 +159,39 @@ def update_sending_request_to(protocol:, host:, port:, path:)
163159 # # Send a GET request to 'session/<session id>/path/to/custom/url'
164160 # @driver.test_command
165161 #
162+ # @example POST command with arguments (block style)
166163 #
167164 # @driver.add_command(
168165 # method: :post,
169166 # url: 'session/:session_id/path/to/custom/url',
170167 # name: :test_command
171- # ) do
172- # def test_command(argument)
173- # execute(:test_command, {}, { dummy: argument })
174- # end
175- # end
176- # # Send a POST request to 'session/<session id>/path/to/custom/url'
177- # # with body "{ dummy: 1 }" as JSON object. "1" is the argument.
178- # # ':session_id' in the given 'url' is replaced with current 'session id'.
168+ # ) { |argument| execute(:test_command, {}, { dummy: argument }) }
179169 # @driver.test_command(1)
180170 #
171+ # @example POST command with URL placeholders (block style)
181172 #
182173 # @driver.add_command(
183174 # method: :post,
184175 # url: 'session/:session_id/element/:id/custom/action',
185176 # name: :test_action_command
186- # ) do
187- # def test_action_command(element_id, action)
188- # execute(:test_action_command, {id: element_id}, { dummy_action: action })
189- # end
190- # end
191- # # Send a POST request to 'session/<session id>/element/<element id>/custom/action'
192- # # with body "{ dummy_action: #{action} }" as JSON object. "action" is the seconds argument.
193- # # ':session_id' in the given url is replaced with current 'session id'.
194- # # ':id' in the given url is replaced with the given 'element_id'.
177+ # ) { |element_id, action| execute(:test_action_command, {id: element_id}, { dummy_action: action }) }
195178 # e = @driver.find_element :accessibility_id, 'an element'
196179 # @driver.test_action_command(e.id, 'action')
197180 #
198181 def add_command ( method :, url :, name :, &block )
199182 raise ::Appium ::Core ::Error ::ArgumentError , "Available method is either #{ AVAILABLE_METHODS } " unless AVAILABLE_METHODS . include? method
200183
201- @bridge . add_command method : method , url : url , name : name , &block
184+ if Bridge . extra_commands &.key? ( name )
185+ ::Appium ::Logger . info "Overriding the command '#{ name } ' for '#{ url } '"
186+ end
187+
188+ # Use Selenium's Bridge.add_command to register the command and define the method.
189+ # When no block is given, create a default implementation that calls execute.
190+ block ||= proc { execute ( name ) }
191+ Bridge . add_command ( name , method , url , &block )
192+
193+ # Ensure the driver delegates the new method to bridge
194+ self . class . class_eval { def_delegator :@bridge , name } unless self . class . method_defined? ( name )
202195 end
203196
204197 ### Methods for Appium
0 commit comments