@@ -4,7 +4,8 @@ module Appium
4
4
module Device
5
5
extend Forwardable
6
6
7
- NoArgMethods = {
7
+ # @private
8
+ NoArgPostMethods = {
8
9
shake : 'session/:session_id/appium/device/shake' ,
9
10
launch : 'session/:session_id/appium/app/launch' ,
10
11
closeApp : 'session/:session_id/appium/app/close' ,
@@ -14,7 +15,7 @@ class << self
14
15
def extended ( mod )
15
16
extend_webdriver_with_forwardable
16
17
17
- NoArgMethods . each_pair do |m , p |
18
+ NoArgPostMethods . each_pair do |m , p |
18
19
add_endpoint_method m , p
19
20
end
20
21
@@ -36,47 +37,62 @@ def remove(id)
36
37
end
37
38
end
38
39
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
+
39
50
extend_search_contexts
40
51
end
41
52
42
- def add_endpoint_method ( method , path )
53
+ # @private
54
+ def add_endpoint_method ( method , path , verb = :post )
43
55
if block_given?
44
56
# &Proc.new with no args passes the passed_in block
45
57
# Because creating Procs from blocks is slow
46
- create_bridge_command method , path , &Proc . new
58
+ create_bridge_command method , verb , path , &Proc . new
47
59
else
48
- create_bridge_command method , path
60
+ create_bridge_command method , verb , path
49
61
end
50
62
51
63
delegate_driver_method method
52
64
delegate_appium_driver_method method
53
65
end
54
66
67
+ # @private
55
68
def extend_webdriver_with_forwardable
56
69
return if Selenium ::WebDriver ::Driver . kind_of? Forwardable
57
70
Selenium ::WebDriver ::Driver . class_eval do
58
71
extend Forwardable
59
72
end
60
73
end
61
74
75
+ # @private
62
76
def delegate_driver_method ( method )
63
77
return if Selenium ::WebDriver ::Driver . method_defined? method
64
78
Selenium ::WebDriver ::Driver . class_eval { def_delegator :@bridge , method }
65
79
end
66
80
81
+ # @private
67
82
def delegate_appium_driver_method ( method )
68
83
def_delegator :@driver , method
69
84
end
70
85
71
- def create_bridge_command ( method , path )
86
+ # @private
87
+ def create_bridge_command ( method , verb , path )
72
88
# Don't clobber methods that are moved into Selenium
73
89
if selenium_has method
74
90
log_reimplemented_warning ( method , path )
75
91
return
76
92
end
77
93
78
94
Selenium ::WebDriver ::Remote ::Bridge . class_eval do
79
- command method , :post , path
95
+ command method , verb , path
80
96
if block_given?
81
97
class_eval &Proc . new
82
98
else
@@ -85,10 +101,12 @@ def create_bridge_command(method, path)
85
101
end
86
102
end
87
103
104
+ # @private
88
105
def selenium_has ( method )
89
106
Selenium ::WebDriver ::Remote ::Bridge . method_defined? method
90
107
end
91
108
109
+ # @private
92
110
def log_reimplemented_warning ( method , path )
93
111
msg = "Selenium::WebDriver has now implemented the `#{ method } ` method."
94
112
if Selenium ::WebDriver ::Remote ::COMMANDS [ method ] [ 1 ] == path
@@ -113,5 +131,37 @@ def extend_search_contexts
113
131
end
114
132
end
115
133
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
116
166
end
117
167
end
0 commit comments