Method: Appium.promote_appium_methods

Defined in:
lib/appium_lib/appium.rb

.promote_appium_methods(class_array, driver = $driver) ⇒ Object

Promote appium methods to class instance methods

To promote methods to all classes:

It’s better to promote on specific classes instead of Object

Examples:


Appium.promote_appium_methods Object

# promote on rspec
Appium.promote_appium_methods RSpec::Core::ExampleGroup

# promote on minispec
Appium.promote_appium_methods Minitest::Spec

Parameters:

  • class_array (Array<Class>)

    An array of classes

  • driver (Driver) (defaults to: $driver)

    A driver to extend for

Raises:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/appium_lib/appium.rb', line 207

def promote_appium_methods(class_array, driver = $driver)
  raise ArgumentError, 'Driver is nil' if driver.nil?

  # Wrap single class into an array
  class_array = [class_array] unless class_array.instance_of? Array
  # Promote Appium driver methods to class instance methods.
  class_array.each do |klass|
    driver.public_methods(false).each do |method|
      klass.class_eval do
        # NOTE: Do not skip re-definding methods to not keep old instance information.
        # Probably the global driver ($driver) stuff needs to override (re-defined)
        # every time to not keep unexpected state.
        # https://github.com/appium/ruby_lib/issues/917

        # Remove the method before adding it.
        remove_method method if method_defined? method

        define_method method do |*args, &block|
          # Prefer existing method.
          # super will invoke method missing on driver
          super(*args, &block)

          # minitest also defines a name method,
          # so rescue argument error
          # and call the name method on $driver
        rescue NoMethodError, ArgumentError
          if args.size == 1 && args.first.is_a?(Hash)
            # To prevent warnings by keyword arguments (for Ruby 2.7 and 3)
            driver.send method, **args.first, &block if driver.respond_to?(method)
          else
            ::Appium::Logger.warn "Should fix this '#{args}' for Ruby 2.7 (and 3)" if args.first.is_a?(Hash)

            driver.send method, *args, &block if driver.respond_to?(method)
          end
        end
      end
    end
  end
  nil # return nil
end