FrankCucumber Instance Method Summary

在写自动化测试代码时,很多时候往往不知道具体有哪些方法可以调用,以下为Frank-Cucumber的各种实例方法及其实现

  1 require 'net/http'
2 require 'json'
3
4 module Frank module Cucumber
5
6 module FrankHelper
7
8 def touch( uiquery )
9 views_touched = frankly_map( uiquery, 'touch' )
10 raise "could not find anything matching [#{uiquery}] to touch" if views_touched.empty?
11 #TODO raise warning if views_touched.count > 1
12 end
13
14 def element_exists( query )
15 matches = frankly_map( query, 'accessibilityLabel' )
16 # TODO: raise warning if matches.count > 1
17 !matches.empty?
18 end
19
20 def check_element_exists( query )
21 #puts "checking #{query} exists..."
22 element_exists( query ).should be_true
23 end
24
25 def check_element_does_not_exist( query )
26 #puts "checking #{query} does not exist..."
27 element_exists( query ).should be_false
28 end
29
30 def view_with_mark_exists(expected_mark)
31 element_exists( "view marked:'#{expected_mark}'" )
32 end
33
34 def check_view_with_mark_exists(expected_mark)
35 check_element_exists( "view marked:'#{expected_mark}'" )
36 end
37
38 # a better name would be element_exists_and_is_not_hidden
39 def element_is_not_hidden(query)
40 matches = frankly_map( query, 'isHidden' )
41 matches.delete(true)
42 !matches.empty?
43 end
44
45 def app_exec(method_name, *method_args)
46 operation_map = {
47 :method_name => method_name,
48 :arguments => method_args
49 }
50
51 before = Time.now
52 res = post_to_uispec_server( 'app_exec', :operation => operation_map )
53 logger.debug( "MAP applying #{method_name} with args:( #{method_args.inspect} ) to 'Application Delegate' took #{Time.now - before} seconds" )
54
55 res = JSON.parse( res )
56 if res['outcome'] != 'SUCCESS'
57 raise "app_exec #{method_name} failed because: #{res['reason']}\n#{res['details']}"
58 end
59
60 res['results']
61 end
62
63
64 def frankly_map( query, method_name, *method_args )
65 operation_map = {
66 :method_name => method_name,
67 :arguments => method_args
68 }
69 res = post_to_uispec_server( 'map', :query => query, :operation => operation_map )
70 res = JSON.parse( res )
71 if res['outcome'] != 'SUCCESS'
72 raise "frankly_map #{query} #{method_name} failed because: #{res['reason']}\n#{res['details']}"
73 end
74
75 res['results']
76 end
77
78 def frankly_dump
79 res = get_to_uispec_server( 'dump' )
80 puts JSON.pretty_generate(JSON.parse(res)) rescue puts res #dumping a super-deep DOM causes errors
81 end
82
83 def frankly_oriented_portrait?
84 'portrait' == frankly_current_orientation
85 end
86
87 def frankly_oriented_landscape?
88 'landscape' == frankly_current_orientation
89 end
90
91 def frankly_current_orientation
92 res = get_to_uispec_server( 'orientation' )
93 orientation = JSON.parse( res )['orientation']
94 puts "orientation reported as '#{orientation}'" if $DEBUG
95 end
96
97
98 def frankly_is_accessibility_enabled
99 res = get_to_uispec_server( 'accessibility_check' )
100 JSON.parse( res )['accessibility_enabled'] == 'true'
101 end
102
103 def wait_for_frank_to_come_up
104 num_consec_successes = 0
105 num_consec_failures = 0
106 Timeout.timeout(20) do
107 while num_consec_successes <= 6
108 if frankly_ping
109 num_consec_failures = 0
110 num_consec_successes += 1
111 print (num_consec_successes == 1 ) ? "\n" : "\r"
112 print "FRANK!".slice(0,num_consec_successes)
113 else
114 num_consec_successes = 0
115 num_consec_failures += 1
116 print (num_consec_failures == 1 ) ? "\n" : "\r"
117 print "PING FAILED" + "!"*num_consec_failures
118 end
119 STDOUT.flush
120 sleep 0.2
121 end
122 puts ''
123 end
124
125 unless frankly_is_accessibility_enabled
126 raise "ACCESSIBILITY DOES NOT APPEAR TO BE ENABLED ON YOUR SIMULATOR. Hit the home button, go to settings, select Accessibility, and turn the inspector on."
127 end
128 end
129
130 def frankly_ping
131 get_to_uispec_server('')
132 return true
133 rescue Errno::ECONNREFUSED
134 return false
135 rescue EOFError
136 return false
137 end
138
139 #taken from Ian Dee's Encumber
140 def post_to_uispec_server( verb, command_hash )
141 url = frank_url_for( verb )
142 req = Net::HTTP::Post.new url.path
143 req.body = command_hash.to_json
144
145 make_http_request( url, req )
146 end
147
148 def get_to_uispec_server( verb )
149 url = frank_url_for( verb )
150 req = Net::HTTP::Get.new url.path
151 make_http_request( url, req )
152 end
153
154 def frank_url_for( verb )
155 url = URI.parse "http://localhost:37265/"
156 url.path = '/'+verb
157 url
158 end
159
160 def make_http_request( url, req )
161 http = Net::HTTP.new(url.host, url.port)
162
163 res = http.start do |sess|
164 sess.request req
165 end
166
167 res.body
168 end
169
170 def start_recording
171 %x{osascript<<APPLESCRIPT
172 tell application "QuickTime Player"
173 set sr to new screen recording
174 tell sr to start
175 end tell
176 APPLESCRIPT}
177
178 end
179
180 def stop_recording
181 %x{osascript<<APPLESCRIPT
182 tell application "QuickTime Player"
183 set sr to (document 1)
184 tell sr to stop
185 end tell
186 APPLESCRIPT}
187 end
188
189 def quit_simulator
190 %x{osascript<<APPLESCRIPT-
191 application "iPhone Simulator" quit
192 APPLESCRIPT}
193 end
194
195 #Note this needs to have "Enable access for assistive devices"
196 #chcked in the Universal Access system preferences
197 def simulator_hardware_menu_press( menu_label )
198 %x{osascript<<APPLESCRIPT
199 activate application "iPhone Simulator"
200 tell application "System Events"
201 click menu item "#{menu_label}" of menu "Hardware" of menu bar of process "iPhone Simulator"
202 end tell
203 APPLESCRIPT}
204 end
205
206 def press_home_on_simulator
207 simulator_hardware_menu_press "Home"
208 end
209
210 def rotate_simulator_left
211 simulator_hardware_menu_press "Rotate Left"
212 end
213
214 def rotate_simulator_right
215 simulator_hardware_menu_press "Rotate Right"
216 end
217
218 def shake_simulator
219 simulator_hardware_menu_press "Shake Gesture"
220 end
221
222 def simulate_memory_warning
223 simulator_hardware_menu_press "Simulate Memory Warning"
224 end
225
226 def toggle_call_status_bar
227 simulator_hardware_menu_press "Toggle In-Call Status Bar"
228 end
229
230 def simulate_hardware_keyboard
231 simulator_hardware_menu_press "Simulate Hardware Keyboard"
232 end
233 end
234
235
236 end end
原文地址:https://www.cnblogs.com/simonshi2012/p/2160725.html