From c3e9b215544b90e2bf96b983106b5a55b454a1e4 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Mon, 17 Oct 2011 15:40:41 +0800 Subject: [PATCH 01/14] removed debug messages --- py/selenium/webdriver/android/service.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/py/selenium/webdriver/android/service.py b/py/selenium/webdriver/android/service.py index 6654a5d..a581e46 100644 --- a/py/selenium/webdriver/android/service.py +++ b/py/selenium/webdriver/android/service.py @@ -73,7 +73,7 @@ def initDevice(deviceID=None): if deviceID: # check if device with given deviceID is connected if deviceID in [i[0] for i in deviceInfo]: - print "Connected to %s..." % deviceID + #print "Connected to %s..." % deviceID return deviceID else: raise WebDriverException("""No device with serial ID '%s' found. @@ -81,7 +81,7 @@ def initDevice(deviceID=None): else: for i in deviceInfo: if i[1] =='device': - print "Connected to %s..." % i[0] + #print "Connected to %s..." % i[0] return i[0] else: raise WebDriverException("""No devices found. @@ -93,16 +93,17 @@ def start(self): WebDriverException : Raised either when it can't start the service or when it can't connect to the service""" - print 'start tcp port 8080 forwarding' + #print 'start tcp port 8080 forwarding' subprocess.call('%s forward tcp:8080 tcp:8080'%self.adbCmd,shell=True) - print 'stop existing android server by sending back key' + + #print 'stop existing android server by sending back key' # this is not mandatory as we already killed adb server, but could this # decrease the webview created in andriod server application. maybe # it's a bug to create one webview per launch of app? for i in xrange(4): subprocess.call(r'%s shell input keyevent 4'%self.adbCmd,shell=True) - print 'start android server activity' + #print 'start android server activity' output=subprocess.check_output(r'%s shell am start -n %s'%(self.adbCmd, Service.ANDROID_DRIVER_CLIENT_APP_CMP), stderr=subprocess.STDOUT,shell=True).split() @@ -112,6 +113,7 @@ def start(self): http://code.google.com/p/selenium/downloads/list""") # wait for WebDriver Client to be launched completely time.sleep(2) + #print "Device %s is ready." % repr(self.device) @property def service_url(self): From e0d1b9f790706ebe4c12dcaf0926d2fcf321b54d Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Mon, 17 Oct 2011 16:40:41 +0800 Subject: [PATCH 02/14] added a more detailed example according to AndroidDriver Wiki example --- README | 2 ++ demo/demo.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 demo/demo.py diff --git a/README b/README index 9662f2f..6daede4 100644 --- a/README +++ b/README @@ -59,6 +59,8 @@ driver= webdriver.Android() driver.get("http://www.symbio.com") driver.quit() +If you want more detailed example, plz see demo/demo.py + ============ Documentation ============ diff --git a/demo/demo.py b/demo/demo.py new file mode 100644 index 0000000..03d495c --- /dev/null +++ b/demo/demo.py @@ -0,0 +1,38 @@ +""" +@author: Sean Wang: xiao.wang@symbio.com + +This demo is written according to the two demos in selenium AndroidDriver wiki: + +http://code.google.com/p/selenium/wiki/AndroidDriver#Run_the_Tests +and +http://code.google.com/p/selenium/wiki/AndroidDriver#Using_the_Android_Test_Framework + +to demostrate that why I implement Selenium AndroidDriver for Python Client: +Python is so simple and elegant +""" + +import unittest +from selenium import webdriver + +class Test(unittest.TestCase): + + def setUp(self): + self.driver=webdriver.Android() + self.driver.implicitly_wait(30) + self.driver.get("http://www.google.com.hk") + + def tearDown(self): + self.driver.quit() + + def testDemo(self): + searchBox=self.driver.find_element_by_name('q') + searchBox.send_keys("Symbio") + searchBox.submit() + print "Page title is: %s"%self.driver.title + #Ensure the title contains "Google" + self.assertTrue("Google" in self.driver.title) + #Ensure that there is at least one link with the keyword "Symbio" + self.assertTrue(len(self.driver.find_elements_by_partial_link_text("Symbio"))>0) + +if __name__ == "__main__": + unittest.main() From ccb24def84c9d2c203422b56493a8a9161f3b4b6 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Mon, 17 Oct 2011 17:14:29 +0800 Subject: [PATCH 03/14] added encoding of the demo file to avoid incompatability; renamed the file name to be more specified --- example/example.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 example/example.py diff --git a/example/example.py b/example/example.py new file mode 100644 index 0000000..bab0e9e --- /dev/null +++ b/example/example.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +""" +@author: Sean Wang: xiao.wang@symbio.com + +This demo is written according to the two demos in selenium AndroidDriver wiki: + +http://code.google.com/p/selenium/wiki/AndroidDriver#Run_the_Tests +and +http://code.google.com/p/selenium/wiki/AndroidDriver#Using_the_Android_Test_Framework + +to demostrate that why I implement Selenium AndroidDriver for Python Client: +Python is so simple and elegant +""" + +import unittest +from selenium import webdriver + +class Test(unittest.TestCase): + + def setUp(self): + self.driver=webdriver.Android() + self.driver.implicitly_wait(30) + self.driver.get("http://www.google.com.hk") + + def tearDown(self): + self.driver.quit() + + def testDemo(self): + searchBox=self.driver.find_element_by_name('q') + searchBox.send_keys("Symbio") + searchBox.submit() + print "Page title is: %s"%self.driver.title + #Ensure the title contains "Google" + self.assertTrue("Google" in self.driver.title) + #Ensure that there is at least one link with the keyword "Symbio" + self.assertTrue(len(self.driver.find_elements_by_partial_link_text("Symbio"))>0) + +if __name__ == "__main__": + unittest.main() From e07e14007ddc56dade1b77ec4cd43051f1156908 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Mon, 17 Oct 2011 17:16:52 +0800 Subject: [PATCH 04/14] remove demo directory --- demo/demo.py | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 demo/demo.py diff --git a/demo/demo.py b/demo/demo.py deleted file mode 100644 index 03d495c..0000000 --- a/demo/demo.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -@author: Sean Wang: xiao.wang@symbio.com - -This demo is written according to the two demos in selenium AndroidDriver wiki: - -http://code.google.com/p/selenium/wiki/AndroidDriver#Run_the_Tests -and -http://code.google.com/p/selenium/wiki/AndroidDriver#Using_the_Android_Test_Framework - -to demostrate that why I implement Selenium AndroidDriver for Python Client: -Python is so simple and elegant -""" - -import unittest -from selenium import webdriver - -class Test(unittest.TestCase): - - def setUp(self): - self.driver=webdriver.Android() - self.driver.implicitly_wait(30) - self.driver.get("http://www.google.com.hk") - - def tearDown(self): - self.driver.quit() - - def testDemo(self): - searchBox=self.driver.find_element_by_name('q') - searchBox.send_keys("Symbio") - searchBox.submit() - print "Page title is: %s"%self.driver.title - #Ensure the title contains "Google" - self.assertTrue("Google" in self.driver.title) - #Ensure that there is at least one link with the keyword "Symbio" - self.assertTrue(len(self.driver.find_elements_by_partial_link_text("Symbio"))>0) - -if __name__ == "__main__": - unittest.main() From 175f8fbab70abcb1b0313297508f3ec2fe324f25 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Tue, 18 Oct 2011 14:09:11 +0800 Subject: [PATCH 05/14] rewrited the README file to GFM format --- README | 74 ------------------------------------------------------- README.md | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 74 deletions(-) delete mode 100644 README create mode 100644 README.md diff --git a/README b/README deleted file mode 100644 index 6daede4..0000000 --- a/README +++ /dev/null @@ -1,74 +0,0 @@ -============ -Introduction -============ - -:Author: Sean Wang - -AndroidWebDriver4Python is an add-on to Selenium Python Client Driver. -Its source code could be found from -http://code.google.com/p/selenium/source/browse/trunk/py - -As I could not find Android WebDriver implementation in it, -and I really like Python as opposed to Java. So I want to implement one. - -More infomation about Selenium, plz check http://code.google.com/p/selenium/ - -============ -Installing -============ - -As I am a newbie, I do not expect to commit in the Selenium project. -To install this AndroidDriver for Python, you need: -1. download and extract Python client from http://pypi.python.org/pypi/selenium#downloads -2. download AndroidWebDriver4Python from using git clone git://github.com/truebit/AndroidWebDriver4Python.git -4. copy the entire 'py' folder under AndroidWebDriver4Python to merge the -same one in root directory of AndroidDriver for Python -5. back to the root directory of Selenium Python Client, to install this -modified version using command: - python setup.py install - -Here you have installed this AndroidWebDriver4Python add-on. -There are some prerequisites to use AndroidWebDriver4Python. - -6. Install Android SDK and set 'tools' and 'platform-tools' in your PATH: -http://developer.android.com/sdk/installing.html - -7. Install Android server side application on your device: - Download android-server-2.x.x.apk from - http://code.google.com/p/selenium/downloads/list - -8. enable 'USB Debugging' in your device, which normally could found from: - Settings>Applications>Development>USB debugging - -9. connect USB cable between device and PC, install adb drivers - -For more information you could check -http://code.google.com/p/selenium/downloads/list,but you do not need to do all -those steps. Above 3 steps is all you need to do. - - -============ -Example -============ - -from selenium import webdriver - -driver= webdriver.Android() -#another way is to explicitly specify the serial id of the device -#driver=webdriver.Android('emulator-5554') -driver.get("http://www.symbio.com") -driver.quit() - -If you want more detailed example, plz see demo/demo.py - -============ -Documentation -============ - -The latest Selenium Python documentation covers all So plz check latest documentation -at http://http://readthedocs.org/docs/selenium-python/en/latest - -Use The Source Luke! -==================== - -http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webdriver.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..ba2d03d --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# Introduction + +_Author: Sean Wang xiao.wang@symbio.com_ + +AndroidWebDriver4Python is an add-on to [Selenium Python Client Driver](http://code.google.com/p/selenium/source/browse/trunk/py) + +As I could not find Android WebDriver implementation in it, +and I really like Python as opposed to Java. So I want to implement one. +I know that I am a newbie, I do not expect to commit in the Selenium project. + +More infomation about *Selenium*, plz check http://code.google.com/p/selenium/ + +# Installing + +To install this AndroidDriver for Python, you need: +1. download AndroidWebDriver4Python using + git clone git://github.com/truebit/AndroidWebDriver4Python.git +2. download and extract [Python client](http://pypi.python.org/pypi/selenium#downloads) +3. copy the entire 'py' folder under AndroidWebDriver4Python to merge the +same one in root directory of AndroidDriver for Python +4. back to the root directory of Selenium Python Client, to install this +modified version using command: + $ python setup.py install + +Here you have installed this AndroidWebDriver4Python add-on. +There are some prerequisites to use AndroidWebDriver4Python. + +* [Install Android SDK](http://developer.android.com/sdk/installing.html) and set its 'tools' and 'platform-tools' in your PATH +* Install Android server side application [android-server-2.x.x.apk](http://code.google.com/p/selenium/downloads/list) on your device +* enable 'USB Debugging' in your device and disable autolock, which normally could found from +> Home>Settings>Applications>Development>USB debugging + +* connect USB cable between device and PC, install adb drivers + +# Example +```python +from selenium import webdriver + +driver= webdriver.Android() +#another way is to explicitly specify the serial id of the device +#driver=webdriver.Android('emulator-5554') +driver.get("http://www.symbio.com") +driver.quit() +``` + +If you want more detailed example, plz check [example.py](exmaple/example.py) + +# Documentation + +The latest [Selenium Python Bindings documentation](http://readthedocs.org/docs/selenium-python/en/latest) + +# Use The Source Luke! + +As this Android Driver I implemented is herited from [webdriver.py](http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webdriver.py), so plz see its source code to use Android driver just like other WebDriver. From 84046a5fc042b969710b808a98ede41b5290dc82 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Tue, 18 Oct 2011 14:24:41 +0800 Subject: [PATCH 06/14] reslove the ordered list display problem --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ba2d03d..38635f0 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ More infomation about *Selenium*, plz check http://code.google.com/p/selenium/ # Installing To install this AndroidDriver for Python, you need: + 1. download AndroidWebDriver4Python using git clone git://github.com/truebit/AndroidWebDriver4Python.git 2. download and extract [Python client](http://pypi.python.org/pypi/selenium#downloads) From 0ad5c867b4eb2ae04404d46676de120e17ceb1a9 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Tue, 18 Oct 2011 14:35:14 +0800 Subject: [PATCH 07/14] reslove code blocks problem --- README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 38635f0..59c283d 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,7 @@ _Author: Sean Wang xiao.wang@symbio.com_ AndroidWebDriver4Python is an add-on to [Selenium Python Client Driver](http://code.google.com/p/selenium/source/browse/trunk/py) -As I could not find Android WebDriver implementation in it, -and I really like Python as opposed to Java. So I want to implement one. +As I could not find Android WebDriver implementation in it,and I really like Python as opposed to Java. So I want to implement one. I know that I am a newbie, I do not expect to commit in the Selenium project. More infomation about *Selenium*, plz check http://code.google.com/p/selenium/ @@ -15,13 +14,17 @@ More infomation about *Selenium*, plz check http://code.google.com/p/selenium/ To install this AndroidDriver for Python, you need: 1. download AndroidWebDriver4Python using - git clone git://github.com/truebit/AndroidWebDriver4Python.git -2. download and extract [Python client](http://pypi.python.org/pypi/selenium#downloads) +``` +$ git clone git://github.com/truebit/AndroidWebDriver4Python.git +``` +2. download and extract [Selenium Python client](http://pypi.python.org/pypi/selenium#downloads) 3. copy the entire 'py' folder under AndroidWebDriver4Python to merge the same one in root directory of AndroidDriver for Python 4. back to the root directory of Selenium Python Client, to install this modified version using command: - $ python setup.py install +``` +$ python setup.py install +``` Here you have installed this AndroidWebDriver4Python add-on. There are some prerequisites to use AndroidWebDriver4Python. @@ -44,7 +47,7 @@ driver.get("http://www.symbio.com") driver.quit() ``` -If you want more detailed example, plz check [example.py](exmaple/example.py) +If you want more detailed example, plz check [example.py](AndroidWebDriver4Python/example/example.py) # Documentation From 0ab0c11ed6e90763ec731618e8b575c78270819f Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Tue, 18 Oct 2011 14:49:51 +0800 Subject: [PATCH 08/14] reslove code blocks problem --- README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 59c283d..88b0e20 100644 --- a/README.md +++ b/README.md @@ -13,17 +13,19 @@ More infomation about *Selenium*, plz check http://code.google.com/p/selenium/ To install this AndroidDriver for Python, you need: -1. download AndroidWebDriver4Python using -``` -$ git clone git://github.com/truebit/AndroidWebDriver4Python.git -``` +1. download AndroidWebDriver4Python using command: + +~~~~~ + $ git clone git://github.com/truebit/AndroidWebDriver4Python.git +~~~~~ + 2. download and extract [Selenium Python client](http://pypi.python.org/pypi/selenium#downloads) 3. copy the entire 'py' folder under AndroidWebDriver4Python to merge the same one in root directory of AndroidDriver for Python -4. back to the root directory of Selenium Python Client, to install this -modified version using command: +4. back to the root directory of Selenium Python Client, to install this modified version using command: + ``` -$ python setup.py install + $ python setup.py install ``` Here you have installed this AndroidWebDriver4Python add-on. @@ -37,7 +39,7 @@ There are some prerequisites to use AndroidWebDriver4Python. * connect USB cable between device and PC, install adb drivers # Example -```python +~~~~~ python from selenium import webdriver driver= webdriver.Android() @@ -45,7 +47,7 @@ driver= webdriver.Android() #driver=webdriver.Android('emulator-5554') driver.get("http://www.symbio.com") driver.quit() -``` +~~~~~ If you want more detailed example, plz check [example.py](AndroidWebDriver4Python/example/example.py) From 902cc8010035b0fcff76f6fb6998e186e14996a2 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Tue, 18 Oct 2011 15:32:11 +0800 Subject: [PATCH 09/14] corrected the address of example.py --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 88b0e20..10a5302 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,9 @@ To install this AndroidDriver for Python, you need: 1. download AndroidWebDriver4Python using command: -~~~~~ - $ git clone git://github.com/truebit/AndroidWebDriver4Python.git -~~~~~ +``` +$ git clone git://github.com/truebit/AndroidWebDriver4Python.git +``` 2. download and extract [Selenium Python client](http://pypi.python.org/pypi/selenium#downloads) 3. copy the entire 'py' folder under AndroidWebDriver4Python to merge the @@ -25,7 +25,7 @@ same one in root directory of AndroidDriver for Python 4. back to the root directory of Selenium Python Client, to install this modified version using command: ``` - $ python setup.py install +$ python setup.py install ``` Here you have installed this AndroidWebDriver4Python add-on. @@ -39,7 +39,7 @@ There are some prerequisites to use AndroidWebDriver4Python. * connect USB cable between device and PC, install adb drivers # Example -~~~~~ python +```python from selenium import webdriver driver= webdriver.Android() @@ -47,9 +47,9 @@ driver= webdriver.Android() #driver=webdriver.Android('emulator-5554') driver.get("http://www.symbio.com") driver.quit() -~~~~~ +``` -If you want more detailed example, plz check [example.py](AndroidWebDriver4Python/example/example.py) +If you want more detailed example, plz check [example.py](AndroidWebDriver4Python/blob/master/example/example.py) # Documentation From 199382e24c49bd6ba6552569e5ac4a4685868ad0 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Tue, 18 Oct 2011 17:42:31 +0800 Subject: [PATCH 10/14] changed cmd1 logic and subprocess cmd in stop() --- py/selenium/webdriver/android/service.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/py/selenium/webdriver/android/service.py b/py/selenium/webdriver/android/service.py index a581e46..764a2dd 100644 --- a/py/selenium/webdriver/android/service.py +++ b/py/selenium/webdriver/android/service.py @@ -49,9 +49,12 @@ def initDevice(deviceID=None): cmd1= 'adb kill-server' cmd2= 'adb start-server' cmd3= 'adb devices' - ret=subprocess.call(cmd1,stdout=PIPE, stderr=PIPE,shell=True) - if ret: - raise WebDriverException(Service.CMD_NOT_IN_PATH) + err=subprocess.Popen(cmd1,stdout=PIPE, stderr=PIPE,shell=True).communicate()[1] + if err and 'server not running' not in err: + raise WebDriverException("ERR: %s MSG: %s"%(err,Service.CMD_NOT_IN_PATH)) + #ret=subprocess.call(cmd1,stdout=PIPE, stderr=PIPE,shell=True) + #if ret: + # raise WebDriverException(Service.CMD_NOT_IN_PATH) p=subprocess.Popen(cmd2,stdout=PIPE, stderr=PIPE,shell=True) count=0 while count<30: @@ -94,7 +97,7 @@ def start(self): or when it can't connect to the service""" #print 'start tcp port 8080 forwarding' - subprocess.call('%s forward tcp:8080 tcp:8080'%self.adbCmd,shell=True) + subprocess.call(r'%s forward tcp:8080 tcp:8080'%self.adbCmd,shell=True) #print 'stop existing android server by sending back key' # this is not mandatory as we already killed adb server, but could this @@ -113,7 +116,7 @@ def start(self): http://code.google.com/p/selenium/downloads/list""") # wait for WebDriver Client to be launched completely time.sleep(2) - #print "Device %s is ready." % repr(self.device) + print "AndroidDriver started on device %s" % repr(self.device) @property def service_url(self): @@ -125,7 +128,7 @@ def stop(self): try: print 'stopping AndroidDriver' subprocess.Popen(r'%s shell input keyevent 4'%self.adbCmd, - stdout=PIPE, stderr=PIPE) + stdout=PIPE, stderr=PIPE,shell=True) except: print """AndroidDriver was not closed. Close by yourself by tapping back key to exit AndroidDriver on device.""" From ecb2c246d1be357ff3d01d99427faa5417ed4d1d Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Thu, 20 Oct 2011 14:36:50 +0800 Subject: [PATCH 11/14] changed to a proper path of the python client driver --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 10a5302..2ae698c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ _Author: Sean Wang xiao.wang@symbio.com_ -AndroidWebDriver4Python is an add-on to [Selenium Python Client Driver](http://code.google.com/p/selenium/source/browse/trunk/py) +AndroidWebDriver4Python is an add-on to [Selenium Python Client Driver](http://pypi.python.org/pypi/selenium) As I could not find Android WebDriver implementation in it,and I really like Python as opposed to Java. So I want to implement one. I know that I am a newbie, I do not expect to commit in the Selenium project. From c514856fd4d0a8f51c6e9894c6875d554766e3a1 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Thu, 20 Oct 2011 14:38:16 +0800 Subject: [PATCH 12/14] android-py-1.1:supports multiple Android devices simultaneously --- py/selenium/webdriver/android/service.py | 35 ++++++++++------------ py/selenium/webdriver/android/webdriver.py | 21 +++++++++++-- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/py/selenium/webdriver/android/service.py b/py/selenium/webdriver/android/service.py index 764a2dd..a192efa 100644 --- a/py/selenium/webdriver/android/service.py +++ b/py/selenium/webdriver/android/service.py @@ -30,7 +30,7 @@ class Service(object): ANDROID_DRIVER_CLIENT_APP_CMP= r'org.openqa.selenium.android.app/.MainActivity' - def __init__(self,device=None): + def __init__(self,device=None,port=0): """ Creates a new instance of the Service Args: device: serial ID of the Android device. @@ -39,6 +39,9 @@ def __init__(self,device=None): """ self.device= Service.initDevice(device) self.adbCmd= r'adb -s %s '%self.device + self.port = port + if self.port == 0: + self.port = utils.free_port() @staticmethod def initDevice(deviceID=None): @@ -46,16 +49,9 @@ def initDevice(deviceID=None): # as adb server launching process made the script stuck, # so I use hard-coded wait time to make it through # I do not know why subprocess could not get output in such situation - cmd1= 'adb kill-server' - cmd2= 'adb start-server' - cmd3= 'adb devices' - err=subprocess.Popen(cmd1,stdout=PIPE, stderr=PIPE,shell=True).communicate()[1] - if err and 'server not running' not in err: - raise WebDriverException("ERR: %s MSG: %s"%(err,Service.CMD_NOT_IN_PATH)) - #ret=subprocess.call(cmd1,stdout=PIPE, stderr=PIPE,shell=True) - #if ret: - # raise WebDriverException(Service.CMD_NOT_IN_PATH) - p=subprocess.Popen(cmd2,stdout=PIPE, stderr=PIPE,shell=True) + cmd1= 'adb start-server' + cmd2= 'adb devices' + p=subprocess.Popen(cmd1,stdout=PIPE, stderr=PIPE,shell=True) count=0 while count<30: time.sleep(1) @@ -63,14 +59,14 @@ def initDevice(deviceID=None): break else: raise WebDriverException("adb could not get device info after 30 seconds.") - output=subprocess.check_output(cmd3,shell=True).split()[4:] + output=subprocess.check_output(cmd2,shell=True).split()[4:] for i, v in enumerate(output): if i + 1 < len(output) and i % 2 == 0: deviceInfo.append((v, output[i + 1])) if deviceInfo: # check if all devices are online if 'device' not in [i[1] for i in deviceInfo]: - raise WebDriverException( """No device is good to go. + raise WebDriverException( """No device is online. Reconnect devices and retry. Only a deviceID followed with 'device' would work.""") if deviceID: @@ -79,7 +75,7 @@ def initDevice(deviceID=None): #print "Connected to %s..." % deviceID return deviceID else: - raise WebDriverException("""No device with serial ID '%s' found. + raise WebDriverException("""Could not find device with serial id %r. Plz make sure you got the right ID."""%deviceID) else: for i in deviceInfo: @@ -97,12 +93,13 @@ def start(self): or when it can't connect to the service""" #print 'start tcp port 8080 forwarding' - subprocess.call(r'%s forward tcp:8080 tcp:8080'%self.adbCmd,shell=True) + subprocess.call(r'%s forward tcp:%d tcp:8080'%(self.adbCmd,self.port),shell=True) - #print 'stop existing android server by sending back key' - # this is not mandatory as we already killed adb server, but could this + + # this is not mandatory as we already killed adb server, but this could # decrease the webview created in andriod server application. maybe # it's a bug to create one webview per launch of app? + #print 'stop existing android server by sending back key' for i in xrange(4): subprocess.call(r'%s shell input keyevent 4'%self.adbCmd,shell=True) @@ -120,8 +117,8 @@ def start(self): @property def service_url(self): - """ Gets the url of the ChromeDriver Service """ - return "http://127.0.0.1:8080/wd/hub" + """ Gets the url of the AndroidDriver Service """ + return "http://127.0.0.1:%d/wd/hub"%self.port def stop(self): """ Close AndroidDriver by sending BACK keyevent to device""" diff --git a/py/selenium/webdriver/android/webdriver.py b/py/selenium/webdriver/android/webdriver.py index 8d117f7..7553bf2 100644 --- a/py/selenium/webdriver/android/webdriver.py +++ b/py/selenium/webdriver/android/webdriver.py @@ -1,3 +1,20 @@ +#!/usr/bin/python +# +# Copyright 2011 Webdriver_name committers +# Copyright Sean Wang : xiao.wang@symbio.com +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.remote.command import Command @@ -5,8 +22,8 @@ class WebDriver(RemoteWebDriver): - def __init__(self,deviceID=None): - self.service = Service(deviceID) + def __init__(self,deviceID=None,port=0): + self.service = Service(deviceID,port=port) self.service.start() RemoteWebDriver.__init__(self, command_executor=self.service.service_url, From 6b0cdf2e3ec6043e45cd75506d2fde4e2a4c297e Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Thu, 20 Oct 2011 15:28:06 +0800 Subject: [PATCH 13/14] rewrote the example for multiple devices --- README.md | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2ae698c..c816127 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,15 @@ _Author: Sean Wang xiao.wang@symbio.com_ -AndroidWebDriver4Python is an add-on to [Selenium Python Client Driver](http://pypi.python.org/pypi/selenium) +AndroidWebDriver4Python is an addon to [Selenium Python Client Driver](http://pypi.python.org/pypi/selenium) + +This addon supports multiple real devices simultaneously from release 1.1, but not supports emulators. I have not figured out yet. + As I could not find Android WebDriver implementation in it,and I really like Python as opposed to Java. So I want to implement one. I know that I am a newbie, I do not expect to commit in the Selenium project. + More infomation about *Selenium*, plz check http://code.google.com/p/selenium/ # Installing @@ -20,7 +24,7 @@ $ git clone git://github.com/truebit/AndroidWebDriver4Python.git ``` 2. download and extract [Selenium Python client](http://pypi.python.org/pypi/selenium#downloads) -3. copy the entire 'py' folder under AndroidWebDriver4Python to merge the +3. copy the entire `py` folder under AndroidWebDriver4Python to merge the same one in root directory of AndroidDriver for Python 4. back to the root directory of Selenium Python Client, to install this modified version using command: @@ -42,11 +46,12 @@ There are some prerequisites to use AndroidWebDriver4Python. ```python from selenium import webdriver -driver= webdriver.Android() -#another way is to explicitly specify the serial id of the device -#driver=webdriver.Android('emulator-5554') -driver.get("http://www.symbio.com") -driver.quit() +driver1= webdriver.Android('HT1234567') +driver2=webdriver.Android('091012345601E00D') +driver1.get("http://www.symbio.com") +driver2.get("http://www.google.com.hk") +driver1.quit() +driver2.quit() ``` If you want more detailed example, plz check [example.py](AndroidWebDriver4Python/blob/master/example/example.py) From d3edb5d2910c95d311abe54e67173c15cc1ebdee Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Thu, 20 Oct 2011 17:13:36 +0800 Subject: [PATCH 14/14] make clear about the example --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c816127..0129d8d 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,9 @@ $ python setup.py install Here you have installed this AndroidWebDriver4Python add-on. There are some prerequisites to use AndroidWebDriver4Python. -* [Install Android SDK](http://developer.android.com/sdk/installing.html) and set its 'tools' and 'platform-tools' in your PATH +* [Install Android SDK](http://developer.android.com/sdk/installing.html) and set its `tools` and `platform-tools` in your PATH * Install Android server side application [android-server-2.x.x.apk](http://code.google.com/p/selenium/downloads/list) on your device -* enable 'USB Debugging' in your device and disable autolock, which normally could found from -> Home>Settings>Applications>Development>USB debugging +* enable `USB Debugging` in your device and disable autolock, which normally could found from `Home>Settings>Applications>Development>USB debugging` * connect USB cable between device and PC, install adb drivers @@ -46,6 +45,8 @@ There are some prerequisites to use AndroidWebDriver4Python. ```python from selenium import webdriver +# if only one device connected, you do not need to specifiy the serial id. +# exmaple driver=webdriver.Android() driver1= webdriver.Android('HT1234567') driver2=webdriver.Android('091012345601E00D') driver1.get("http://www.symbio.com")