appium示例代码python_appium+python手机app自动化测试配置和测试代码示例

前提是配置好了adb环境变量(安卓),安装了python

1. 安装appium server

429541-20190927200057341-853736043.png

2. 安装appium client和selenium

在cmd中输入 pip install selenium

pip install Appium-Python-Client

如果出现retrying问题, 使用带pip源的命令,如

pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip install Appium-Python-Client -i https://pypi.mirrors.ustc.edu.cn/simple/

3. 编写脚本, 代码中需要包含对appium server的设置, 可以根据实际需要增/删设置项, 如

# -*- coding: utf-8 -*-

from appium importwebdriverfrom time importsleep

CAPS={"deviceName": "MEIZU_E3","platformName": "Android","platformVersion": "7.1.1",#'app' = 'E:/autotestingPro/app/UCliulanqi_701.apk' #指向.apk文件,如果设置appPackage和appActivity,那么这项会被忽略

"appPackage": "com.meizu.flyme.flymebbs","appActivity": ".ui.LoadingActivity",#"noReset": True,

}

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

sleep(3)

4. 打开appium server, 设置主机为 127.0.0.1,设置端口为 4723, 启动server

5. 连接手机,安装应用,运行脚本。完整测试脚本如下例(用Unittest):

# coding: utf-8

importunittestfrom appium importwebdriverfrom selenium.webdriver.support.ui importWebDriverWaitfrom selenium.webdriver.support importexpected_conditions as ECfrom selenium.webdriver.common.by importBy

desired_caps= {'platformName': 'Android','platformVersion': '5.1.1','deviceName': 'MEIZU_E3', #设备名来自adb devices"appPackage": "com.meizu.flyme.flymebbs","appActivity": ".ui.LoadingActivity",}

appium_server= 'http://localhost:4723/wd/hub'

classLearnAppiumTest(unittest.TestCase):defsetUp(self):

self.driver=webdriver.Remote(appium_server, desired_caps)deftearDown(self):

self.driver.quit()deftest_01(self):

text_view= self.driver.find_element_by_id("text_view")assert text_view.text == 'Hello World! Hello World!' #测试应该不通过

deftest_02(self):

wait= WebDriverWait(self.driver, 6)

wait.until(EC.element_to_be_clickable((By.ID,'button')))

button= self.driver.find_element_by_id("button")

button.click()

wait= WebDriverWait(self.driver, 6)

wait.until(EC.presence_of_element_located((By.ID,'text_view')))

text_view= self.driver.find_element_by_id("text_view")assert text_view.text == '3' #测试应该通过

if __name__ == '__main__':

unittest.main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值