Skip to content

Commit fe6c7d7

Browse files
committedApr 28, 2013
Fix os specific patches
Automatically quit old driver when a new driver is created. Appium only supports one active driver. Rename last_driver global to driver.
1 parent d9b9c1a commit fe6c7d7

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed
 

‎lib/appium_lib.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# encoding: utf-8
22

3-
$last_driver = nil
3+
$driver = nil
44

55
# Invoke top level methods on last created Appium driver.
66
def self.method_missing method, *args, &block
7-
raise "driver is nil. called #{method}" if $last_driver == nil
7+
raise "driver is nil. called #{method}" if $driver == nil
88

9-
$last_driver.respond_to?(method) ?
10-
$last_driver.send( method, *args, &block ) :
11-
super
9+
$driver.respond_to?(method) ?
10+
$driver.send( method, *args, &block ) :
11+
super
1212
end
1313

1414
module Appium

‎lib/appium_lib/android/patch.rb

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# encoding: utf-8
22
module Appium::Android
3-
# Implement useful features for element.
4-
class Selenium::WebDriver::Element
5-
# Cross platform way of entering text into a textfield
6-
def type text
7-
puts 'android type'
8-
self.send_keys text
3+
# class_eval inside a method because class Selenium::WebDriver::Element
4+
# will trigger as soon as the file is required. in contrast a method
5+
# will trigger only when invoked.
6+
def patch_webdriver_element
7+
Selenium::WebDriver::Element.class_eval do
8+
# Cross platform way of entering text into a textfield
9+
def type text
10+
self.send_keys text
11+
end
12+
end
913
end
10-
end
11-
end
14+
end

‎lib/appium_lib/common/patch.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def raw_execute(command, opts = {}, command_hash = nil)
7575
path_str = path.sub(path_match[0], '') unless path_match.nil?
7676

7777
puts "#{verb} #{path_str}"
78-
puts command_hash.to_json unless command_hash.to_json == 'null'
78+
ap command_hash.to_json unless command_hash.to_json == 'null'
7979
# puts "verb: #{verb}, path #{path}, command_hash #{command_hash.to_json}"
8080
http.call verb, path, command_hash
8181
end # def

‎lib/appium_lib/driver.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class Driver
3838
:app_wait_activity, :sauce_username, :sauce_access_key,
3939
:port, :os, :ios_js
4040
def initialize opts={}
41+
# quit last driver
42+
$driver.driver_quit if $driver
43+
4144
opts = {} if opts.nil?
4245
# Path to the .apk, .app or .app.zip.
4346
# The path can be local or remote for Sauce.
@@ -81,18 +84,21 @@ def initialize opts={}
8184
extend Appium::Ios
8285
end
8386

87+
# apply os specific patches
88+
patch_webdriver_element
89+
8490
# Save global reference to last created Appium driver for top level methods.
85-
$last_driver = self
91+
$driver = self
8692

8793
# Promote exactly once the first time the driver is created.
8894
# Subsequent drivers do not trigger promotion.
8995
unless @@loaded
9096
@@loaded = true
9197
# Promote Appium driver methods to Object instance methods.
92-
$last_driver.public_methods(false).each do | m |
98+
$driver.public_methods(false).each do | m |
9399
Object.class_eval do
94100
define_method m do | *args, &block |
95-
$last_driver.send m, *args, &block
101+
$driver.send m, *args, &block
96102
end
97103
end
98104
end

‎lib/appium_lib/ios/patch.rb

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# encoding: utf-8
22
module Appium::Ios
3-
# Implement useful features for element.
4-
class Selenium::WebDriver::Element
5-
# Cross platform way of entering text into a textfield
6-
def type text
7-
puts 'ios type'
8-
# enter text then tap window to hide the keyboard.
9-
js = <<-JS
10-
au.getElement('#{self.ref}').setValue('#{text}');
11-
au.lookup('window')[0].tap();
12-
JS
13-
puts js
14-
@driver.execute_script js
3+
# class_eval inside a method because class Selenium::WebDriver::Element
4+
# will trigger as soon as the file is required. in contrast a method
5+
# will trigger only when invoked.
6+
def patch_webdriver_element
7+
Selenium::WebDriver::Element.class_eval do
8+
# Cross platform way of entering text into a textfield
9+
def type text
10+
# enter text then tap window to hide the keyboard.
11+
js = <<-JS
12+
au.getElement('#{self.ref}').setValue('#{text}');
13+
au.lookup('window')[0].tap();
14+
JS
15+
@driver.execute_script js
16+
end
1517
end
1618
end
1719
end

0 commit comments

Comments
 (0)
Please sign in to comment.