您的位置:首页 > 移动开发

Appium 新手贴:Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android (完整版)

2017-06-29 00:25 711 查看
https://testerhome.com/topics/646

前面写了个《新手贴:Windows 平台上的使用 Java 语言实现 appium 自动化程序 for Android(完整版)》的帖子:http://testerhome.com/topics/645 ,针对python语言 也来看看如何实现。还是按照流水账的形式来描述。

一,环境配置篇

在Windows上配置

1)下载安装node.js(http://nodejs.org/download/) 安装的时候有选项,记得把环境变量添加到path路径

2)使用npm安装appium,运行CMD输入 npm install -g appium(有些朋友反应在cmd下运行npm无效,如果这样请把nodejs的目录添加到用户变量的path下重启cmd即可 参考帖子:http://blog.csdn.net/iispring/article/details/8023319
,如下图:





3)下载安装Android SDK(http://developer.android.com/sdk/index.htmlANDROID_HOMESDK路径,PATH变量设定%),设置环境变量指向
ANDROID_HOME%\tools 和% ANDROID_HOME%\platform-tools

4)安装JDK并设置JAVA_HOME环境变量

5)安装ANT,并将%ANT_HOME%\bin路径加到PATH

6)安装MAVEN,设置%M2_HOME%\bin,并添加到PATH中

7)安装Git

8)运行CMD 输入appium-doctor检查你的环境是不是都配置好了 如图:




整体的环境变量已经配置完毕,不过接下来要配置 python+selenium安装。

二,python+selenium安装配置:

1)下载并安装python去这个地址http://www.python.org/27的python版本,发表文章时,我使用的是

2)下载并安装setuptools【这个工具是python的基础包工具】

去这个地址https://pypi.python.org/packages/2.7/s/setuptools/setuptools,对应python了2.7的版下载

3)去这个地址http://pypi.python.org/pypi/pippip,将pip用WINRAR解压到某盘根目录下,我的解压目录为c:\pip下载

4)使用CMD命令进入以上解压后的文件夹c:\pip,然后使用python setup.py install

a、如果python命令使用不成功,请配置下环境变量 就能OK(这个去百度一下吧。。。。)

b、报错no module named setuptools 可以下载一个运行ez_setup.py,运行ez_setup.py:python ez_setup.py ;

如果运行正常,那就安装成功了。)

参考图(运行结果不保证与该图完全一致):





5)再打开CMD命令,进入python的script路径,如本人的C:\Python\Scripts然后输入 命令:easy_install pip (恭喜你这边安装成功后,就可以顺利使用pip命令了)

参考图(运行结果不保证与该图完全一致):




6)直接使用pip安装selenium,命令为:pip install selenium -i http://pypi.douban.com/simple(使用国内地址)
参考图(运行结果不保证与该图完全一致):




7)打开python的shell或者IDEL界面 ,输入from selenium import webdriver 如果不报错那就说明你已经安装selenium for python成功了。

安装过程也可以参考:http://rubygems.org/gems/selenium-webdriver
三,appium启动篇

由于我测试是连接真机的,所以这里需要先通过adb devices -l 命令获得 真机的udid号,详细步骤如下:

1)真机(安卓版本4.2.2)通过USB连接 电脑,驱动装好,打开USB调试模式

2)再在cmd中输入 appium -a 127.0.0.1 -p 4723 (-a表示ip,-p表示端口, 可以通过appium -h查看更多命令)

3)如果如下图所示 就表示 appium服务启动成功了,注意这个窗口不要关闭 因为这是appium的服务 关了就关了服务,后面过程无法执行,而且这个窗口也是 日志输出的窗口用于排错。




四,代码执行篇

这块我主要是执行的是官方的演示代码:通讯录管理app,安装打开app,并添加一个联系人保存的操作

1)首先去下载ContactManager.apk(http://yunpan.cn/QInSWzP2YWgTJ

2)将官网的示例代码 android_contact.py 下载下来 放在 Python的目录

3)对python代码进行部分修改

import os
from selenium import webdriver

# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)

desired_caps = {}
desired_caps['device'] = 'Android'
desired_caps['browserName'] = ''
desired_caps['version'] = '4.2.2'
desired_caps['app'] = PATH('C:\Users\Stephen\Desktop\ContactManager.apk')
desired_caps['app-package'] = 'com.example.android.contactmanager'
desired_caps['app-activity'] = '.ContactManager'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

el = driver.find_element_by_name("Add Contact")
el.click()

textfields = driver.find_elements_by_tag_name("textfield")
textfields[0].send_keys("My Name")
textfields[2].send_keys("someone@somewhere.com")

driver.find_element_by_name("Save").click()

driver.quit()

4)运行CMD,进入python目录,输入命令python android_contact.py 此时会自动安装apk并完成相应的添加联系人的操作

OK整个配置执行就算完成了

21 个赞

转载文章时务必注明原作者及原始链接,并注明「发表于 TesterHome 」,并不得对作品进行修改。

共收到 34 条回复





qddegtya
· #1 ·
2014年04月02日


好帖,好贡献





mingyuwang
· #2 ·
2014年04月04日


顶一下好帖···

来一个nodejs版本的楼主··





cylboy
· #3 ·
2014年04月04日


@young 最后一步运行CMD,进入python目录,输入命令python android_contact.py 此时会自动安装apk并完成相应的添加联系人的操作

我提示报错,请大侠帮忙看看是为何?

C:\Python27\Scripts>python C:\Python27\Scripts\android_contact.py Traceback (most recent call last): File "C:\Python27\Scripts\android_contact.py", line 17, in driver = webdriver.Remote('http://localhost:4723/wd/hub',
desired_caps) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 72, in
init self.start_session(desired_capabilities, browser_profile) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 115, in start_session 'desiredCapabilities': desired_capabilities, File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 164, in execute response = self.command_executor.execute(driver_command, params) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 347, in execute return self._request(command_info[0], url, body=data) File "C:\Python27\lib\site
packages\selenium\webdriver\remote\remote_connection.py", line 429, in _request body = data.decode('utf-8').replace('\x00', '').strip() File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError:
'utf8' codec can't decode byte 0xb4 in position 193: invalid start byte





cylboy
· #4 ·
2014年04月04日


python android_contact.py的代码如下:

import os

from selenium import webdriver

PATH = lambda p: os.path.abspath(

os.path.join(os.path.dirname(file), p)

)

desired_caps = {}

desired_caps['device'] = 'android'#android selendroid

desired_caps['browserName'] = ''

desired_caps['version'] = '4.1.2'

desired_caps['app'] = PATH('D:\test\ContactManager.apk')

desired_caps['app-package'] = 'com.example.android.contactmanager'

desired_caps['app-activity'] = '.ContactManager'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

el = driver.find_element_by_name("Add Contact")

el.click()

textfields = driver.find_elements_by_tag_name("textfield")

textfields[0].send_keys("My Name")

textfields[2].send_keys("someone@somewhere.com")

driver.find_element_by_name("Save").click()

driver.quit()





barry
· #5 ·
2014年04月04日


good





yunduo1220
· #6 ·
2014年04月05日


回答3楼的问题 :

desired_caps['version'] = '4.1.2'

真机系统版本要>=4.2...





Anikikun
· #7 ·
2014年04月08日


可行~~已跑上。





cylboy
· #8 ·
2014年04月08日


@young 全部按要求配置完成,运行android_contact.py报如下错误,请大侠帮忙看看?谢谢!

C:\Users\cylboy>python D:\test\android_contact.py

Traceback (most recent call last):

File "D:\test\android_contact.py", line 17, in

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l

ine 72, in init

self.start_session(desired_capabilities, browser_profile)

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l

ine 115, in start_session

'desiredCapabilities': desired_capabilities,

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l

ine 164, in execute

response = self.command_executor.execute(driver_command, params)

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connectio

n.py", line 347, in execute

return self._request(command_info[0], url, body=data)

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connectio

n.py", line 429, in _request

body = data.decode('utf-8').replace('\x00', '').strip()

File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode

return codecs.utf_8_decode(input, errors, True)

UnicodeDecodeError: 'utf8' codec can't decode byte 0xb4 in position 193: invalid

start byte





Lihuazhang
· #9 ·
2014年04月08日


#8楼

@cylboy 你不贴个代码 真很难帮你。





jikunsishen
· #10 ·
2014年04月15日


Traceback (most recent call last):

File "C:\android_contact.py", line 17, in

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 71, in
init

self.start_session(desired_capabilities, browser_profile)

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 113, in start_session

'desiredCapabilities': desired_capabilities,

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 164, in execute

self.error_handler.check_response(response)

File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response

raise exception_class(message, screen, stacktrace)

WebDriverException: Message: u"A new session could not be created. (Original error: Bad app: C:\C:\test\ContactManager.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause:
Error: Error locating the app: ENOENT, stat 'C:\C:\test\ContactManager.apk')"





jikunsishen
· #11 ·
2014年04月15日


import os

from selenium import webdriver

Returns abs path relative to this file and not cwd

PATH = lambda p: os.path.abspath(

os.path.join(os.path.dirname(file), p)

)

desired_caps = {}

desired_caps['device'] = 'Android'

desired_caps['browserName'] = ''

desired_caps['version'] = '4.2.2'

desired_caps['app'] = PATH('C:\test\ContactManager.apk')

desired_caps['app-package'] = 'com.example.android.contactmanager'

desired_caps['app-activity'] = '.ContactManager'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

el = driver.find_element_by_name("Add Contact")

el.click()

textfields = driver.find_elements_by_tag_name("textfield")

textfields[0].send_keys("My Name")

textfields[2].send_keys("someone@somewhere.com")

driver.find_element_by_name("Save").click()

driver.quit()





jikunsishen
· #12 ·
2014年04月15日


不知道怎么弄了~





jikunsishen
· #13 ·
2014年04月15日


根据你的教程做的~





jikunsishen
· #14 ·
2014年04月15日


import os
from selenium import webdriver

# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)

desired_caps = {}
desired_caps['device'] = 'Android'
desired_caps['browserName'] = ''
desired_caps['version'] = '4.2.2'
desired_caps['app'] = PATH('C:\test\ContactManager.apk')
desired_caps['app-package'] = 'com.example.android.contactmanager'
desired_caps['app-activity'] = '.ContactManager'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

el = driver.find_element_by_name("Add Contact")
el.click()

textfields = driver.find_elements_by_tag_name("textfield")
textfields[0].send_keys("My Name")
textfields[2].send_keys("someone@somewhere.com")

driver.find_element_by_name("Save").click()

driver.quit()






jikunsishen
· #15 ·
2014年04月15日










squall
· #16 ·
2014年04月21日


请问楼主,下载ContactManager.apk所需要的密码是多少?





miting
· #17 ·
2014年04月21日


@jikunsishen 我也遇到了selenium.common.exceptions.WebDriverException: Message: u"A new session could not be created. (Original error: Parameter
'appPackage' is required for launching application)"





ljdfdd
· #18 ·
2014年04月24日


加这个群156413673,群共享有ContactManager.apk





scott
· #19 ·
2014年04月24日


Parameter 'appPackage' is required for launching application怎么解决?





miting
· #20 ·
2014年04月25日


#19楼

@scott

http://testerhome.com/topics/729

可以参考这个。是版本的问题





careysucci
· #21 ·
2014年04月27日


楼主你好!用代码编译后,textfields[0].send_keys("My Name")报index:error,请问怎么解决呀?试过很多方法了,自行解决不了,谢谢

import os

from selenium import webdriver

Returns abs path relative to this file and not cwd

PATH = lambda p: os.path.abspath(

os.path.join(os.path.dirname(file), p)

)

desired_caps = {}

desired_caps['device'] = 'Android'

desired_caps['browserName'] = ''

desired_caps['version'] = '4.2.2'

desired_caps['app'] = PATH('C:\Users\Succi\Desktop\ContactManager.apk')

desired_caps['app-package'] = 'com.example.android.contactmanager'

desired_caps['app-activity'] = '.ContactManager'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

el = driver.find_element_by_name("Add Contact")

el.click()

textfields = driver.find_elements_by_tag_name("textfield")

print("debug:",textfields)#input list

textfields[0].send_keys("My Name")

textfields[2].send_keys("someone@somewhere.com")

driver.find_element_by_name("Save").click()

driver.quit()

错误信息:

('debug:', [])

Traceback (most recent call last):

File "android_contact.py", line 24, in

textfields[1].send_keys("My Name")

IndexError: list index out of range





tonytam
· #22 ·
2014年05月13日


当我用tag_name定位输入框的时候:textfields = driver.find_elements_by_tag_name("textfield")

为什么会提示定位错误呢?

错误信息:selenium.common.exceptions.WebDriverException: Message: u'Invalid locator strate gy: tag name‘

用xpath定位的时候,出现和楼上仁兄一样的问题,打印出来是空的!





tonytam
· #23 ·
2014年05月13日


我这边发现问题了,使用1.0版本的,会出现上述情况,使用0.14版本的,可以用tag name定位,奇怪了,1.0版本的怎么用呢?希望有高手回答下!多谢





xianjin
· #24 ·
2014年05月15日


from selenium import webdriver

from appium import webdriver两种方式有什么区别吗?





xianjin
· #25 ·
2014年05月15日


二python+selenium安装配置:第2、3、4步可以简化成下面一步到位

进入https://pypi.python.org/pypi/setuptools/#windows-7-or-graphical-install 直接下载ez_setup.py双击运行即可。安装成功后会发现在Python目录多了script文件夹。然后进入
第5步





hopecao
· #26 ·
2014年12月31日


desired_caps['app'] = PATH('C:\Users\Stephen\Desktop\ContactManager.apk')

是必须的吗?

如果不需要安装,直接运行机器里已经安装好的应用,要怎么处理?





lovelorn0327
· #27 ·
2015年01月09日
1 个赞

请问下:运行以后出现:

raise URLError(err)

urllib2.URLError:

这个哪里出了问题呀,请指教





xinxjxjxj
· #28 ·
2015年01月20日






二python+selenium安装配置 第五步一直不行,求解啊





simon_jiang
· #29 ·
2015年01月29日


#27楼

@lovelorn0327 同问





tlbin
· #30 ·
2015年05月28日


-楼主 我已安装py3,然后按照你的步骤来 cmd 命令npm 来安装appium 报错,说不支持python3....有何解决方案呢...





jacky9947
· #31 ·
2015年06月15日


楼主大大,这个apk怎么下载,云盘不能用了。可以给新地址不,谢谢





pys_moving
· #32 ·
2015年07月09日


APK地址找不到了,能否重新提供一个?新手来混论坛,多多关照!




henryztong

使用 Python 运行 Appium 测试的示例 中提及了此贴03月19日 18:42





WANGWENCHENG
· #34 ·
3 月前





jikunsishen
#10 回复

同样的错误请问怎样解决的?driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)这个东西是干嘛的?





815475897
· #35 ·
13 天前


[debug] [MJSONWP] Bad parameters: BadParametersError: Parameters were incorrect. We wanted {"required":["value"]} and you sent ["text","sessionId","id","value"]

请教下,运行之后报这个错误,不知道什么原因
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐