Skip to content

Commit eb12171

Browse files
Merge pull request #135 from DylanLacey/contexts
Add context handling & warnings
2 parents c13b877 + b8f3a91 commit eb12171

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

docs_gen/make_docs.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def globs paths
7878
paths.map! { |path| "#{Dir.pwd}#{path}" }
7979
end
8080

81-
common_globs = '/lib/appium_lib/*.rb', '/lib/appium_lib/common/**/*.rb'
81+
common_globs = '/lib/appium_lib/*.rb', '/lib/appium_lib/device/*.rb', '/lib/appium_lib/common/**/*.rb'
8282
android_globs = common_globs + [ '/lib/appium_lib/android/**/*.rb' ]
8383
ios_globs = common_globs + [ '/lib/appium_lib/ios/**/*.rb' ]
8484

lib/appium_lib/device/device.rb

+57-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module Appium
44
module Device
55
extend Forwardable
66

7-
NoArgMethods = {
7+
# @private
8+
NoArgPostMethods = {
89
shake: 'session/:session_id/appium/device/shake',
910
launch: 'session/:session_id/appium/app/launch',
1011
closeApp: 'session/:session_id/appium/app/close',
@@ -14,7 +15,7 @@ class << self
1415
def extended(mod)
1516
extend_webdriver_with_forwardable
1617

17-
NoArgMethods.each_pair do |m, p|
18+
NoArgPostMethods.each_pair do |m, p|
1819
add_endpoint_method m, p
1920
end
2021

@@ -36,47 +37,62 @@ def remove(id)
3637
end
3738
end
3839

40+
add_endpoint_method(:available_contexts, 'session/:session_id/contexts', :get)
41+
42+
add_endpoint_method(:current_context, 'session/:session_id/context', :get)
43+
44+
add_endpoint_method(:current_context=, 'session/:session_id/context') do
45+
def current_context=(context=null)
46+
execute :current_context=, {}, :context => context
47+
end
48+
end
49+
3950
extend_search_contexts
4051
end
4152

42-
def add_endpoint_method(method, path)
53+
# @private
54+
def add_endpoint_method(method, path, verb=:post)
4355
if block_given?
4456
# &Proc.new with no args passes the passed_in block
4557
# Because creating Procs from blocks is slow
46-
create_bridge_command method, path, &Proc.new
58+
create_bridge_command method, verb, path, &Proc.new
4759
else
48-
create_bridge_command method, path
60+
create_bridge_command method, verb, path
4961
end
5062

5163
delegate_driver_method method
5264
delegate_appium_driver_method method
5365
end
5466

67+
# @private
5568
def extend_webdriver_with_forwardable
5669
return if Selenium::WebDriver::Driver.kind_of? Forwardable
5770
Selenium::WebDriver::Driver.class_eval do
5871
extend Forwardable
5972
end
6073
end
6174

75+
# @private
6276
def delegate_driver_method(method)
6377
return if Selenium::WebDriver::Driver.method_defined? method
6478
Selenium::WebDriver::Driver.class_eval { def_delegator :@bridge, method}
6579
end
6680

81+
# @private
6782
def delegate_appium_driver_method(method)
6883
def_delegator :@driver, method
6984
end
7085

71-
def create_bridge_command(method, path)
86+
# @private
87+
def create_bridge_command(method, verb, path)
7288
# Don't clobber methods that are moved into Selenium
7389
if selenium_has method
7490
log_reimplemented_warning(method, path)
7591
return
7692
end
7793

7894
Selenium::WebDriver::Remote::Bridge.class_eval do
79-
command method, :post, path
95+
command method, verb, path
8096
if block_given?
8197
class_eval &Proc.new
8298
else
@@ -85,10 +101,12 @@ def create_bridge_command(method, path)
85101
end
86102
end
87103

104+
# @private
88105
def selenium_has(method)
89106
Selenium::WebDriver::Remote::Bridge.method_defined? method
90107
end
91108

109+
# @private
92110
def log_reimplemented_warning(method, path)
93111
msg = "Selenium::WebDriver has now implemented the `#{method}` method."
94112
if Selenium::WebDriver::Remote::COMMANDS[method][1] == path
@@ -113,5 +131,37 @@ def extend_search_contexts
113131
end
114132
end
115133
end
134+
135+
# Perform a block within the given context, then switch back to the starting context.
136+
# @param context (String) The context to switch to for the duration of the block.
137+
#
138+
# ```ruby
139+
# within_context('NATIVE_APP') do
140+
# find_element [:tag, "button"]
141+
# ```
142+
def within_context(context)
143+
existing_context = current_context
144+
yield if block_given?
145+
current_context = existing_context
146+
end
147+
148+
# Change to the default context. This is equivalent to `current_context= nil`.
149+
def switch_to_default_context
150+
current_context = nil
151+
end
152+
153+
# @!method current_context=
154+
# Change the context to the given context.
155+
# @param [String] The context to change to
156+
#
157+
# ```ruby
158+
# current_context= "NATIVE_APP"
159+
# ```
160+
161+
# @!method current_context
162+
# @return [String] The context currently being used.
163+
164+
# @!method available_contexts
165+
# @return [Array<String>] All usable contexts, as an array of strings
116166
end
117167
end

lib/appium_lib/logger.rb

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class << self
44
extend Forwardable
55
def_delegators :@logger, :warn, :error, :info
66

7+
# @private
78
def logger
89
@logger ||= Logger.new
910
end

0 commit comments

Comments
 (0)