Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: removing add_command from bridge #555

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/appium_lib_core/common/base/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here can be later. Firstly we could use the bridge function to other existing methods

# ::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]
Expand Down
39 changes: 35 additions & 4 deletions lib/appium_lib_core/common/base/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
139 changes: 69 additions & 70 deletions test/unit/android/webdriver/w3c/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down
Loading