Appium Python 六:管理应用和Activity
管理应用
1. 将当前应用放到后台
执行之后,应用会被放到后台特定时间。比如这里就是5秒,5秒过后,应用会重新回到前台。
driver.background_app(5)
官网示例:
driver.background_app(1)
sleep(2)
el = driver.find_element_by_name('Animation')
assertIsNotNone(el)
2. 检查应用是否已经安装
检查设备目前是否安装了某个应用,这里检查的是知乎APP。
这里需要的参数是该应用的包名,下面就是知乎的包名。该方法会返回True 或者 False 。
driver.is_app_installed('com.zhihu.android')
3. 安装应用
在设备上安装某个应用。参数是该应用APK文件的路径。
driver.install_app('zhihu_521.apk')
其实执行的就是:adb install zhihu_521.apk
官网示例:
assertFalse(driver.is_app_installed('io.selendroid.testapp'))
driver.install_app('/Users/isaac/code/python-client/test/apps/selendroid-test-app.apk')
assertTrue(driver.is_app_installed('io.selendroid.testapp'))
4. 卸载应用
在设备上卸载某个应用。参数是该应用的包名。
driver.remove_app('com.zhihu.android')
其实执行的就是: adb uninstall com.zhihu.android
官网示例:
assertTrue(driver.is_app_installed('com.example.android.apis'))
driver.remove_app('com.example.android.apis')
assertFalse(driver.is_app_installed('com.example.android.apis'))
5. 关闭应用
关闭 desired_caps 定义的应用。
driver.close_app()
6. 启动应用
启动 desired_caps 定义的应用。
driver.launch_app()
官网示例:
el = driver.find_element_by_name('Animation')
assertIsNotNone(el)
driver.close_app();
try:
driver.find_element_by_name('Animation')
except Exception as e:
pass # should not exist
driver.launch_app()
el = driver.find_element_by_name('Animation')
assertIsNotNone(el)
7. 获取应用的字符串
实际操作,发现返回的就是该次会话的 session id 。
driver.app_strings
打印出来,类似下面的结果:
<bound method WebDriver.app_strings of <appium.webdriver.webdriver.WebDriver (session="xxxxxxxxxxxxxxxxxxxxxxxxxxx")>>
8. 重置
driver.reset()
官网示例:
el = driver.find_element_by_name('App')
el.click()
driver.reset()
sleep(5)
el = driver.find_element_by_name('App')
assertIsNotNone(el)
Activity
1. 获取当前Activity
driver.current_activity
比如下面的程序:
from appium import webdriver
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.2.2'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.zhihu.android'
desired_caps['appActivity'] = 'com.zhihu.android.app.ui.activity.MainActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
b=driver.current_activity
print(b)
运行结果如下:
.app.ui.activity.MainActivity
2. 启动Activity
在当前应用中打开一个Activity ,或者启动一个新应用并打开一个Activity。
这里第一个参数是要启动的Activity的包名,第二个参数是要启动的Activity名。
driver.start_activity('com.example.android.apis', '.Foo')
比如下面的程序:
#coding=utf-8
from appium import webdriver desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.2.2'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.zhihu.android'
desired_caps['appActivity'] = 'com.zhihu.android.app.ui.activity.MainActivity' driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) print(driver.current_activity) driver.start_activity('com.android.calculator2', '.Calculator') print(driver.current_activity)
打印结果如下:
.app.ui.activity.MainActivity
.Calculator
可以看到一开始当前Activity还是 知乎的 Activity,启动计算器的Activity之后,当前Activity就变成计算器的Activity。
Appium Python 六:管理应用和Activity的更多相关文章
- Appium+python自动化27-等待activity出现(android特有的wait_activity)
前言 在启动app的时候,如果直接做下一步点击操作,经常会报错,于是我们会在启动完成的时候加sleep. 那么问题来了,这个sleep时间到底设置多少合适呢?设置长了,就浪费时间,设置短了,就会找不到 ...
- Appium Python 四:怎样获取APP的Package以及Activity
看到一篇很好的博客:[Android测试][随笔]获得App的包名和启动页Activity 除了博客上的方法,我还找到两种方法: 方法一:aapt 前提需要使用SDK Manager.exe 下载 A ...
- Appium python API 总结
Appium python api 根据testerhome的文章,再补充一些文章里面没有提及的API [TOC] [1]find element driver 的方法 注意:这几个方法只能通过sel ...
- appium+Python真机运行测试demo的方法
appium+Python真机运行测试demo的方法 一, 打开手机的USB调试模式 二, 连接手机到电脑 将手机用数据线连接到电脑,并授权USB调试模式.查看连接的效果,在cmd下运行命 ...
- python包管理-distutils,setuptools,pip,virtualenv等介绍
python包管理-distutils,setuptools,pip,virtualenv等介绍 对于每个编程语言来说打包和发布开发包往往非常重要,而作为一个编程者能够快速容易的获得并应用这些由第三方 ...
- appium+python做移动端自动化测试
1 导言 1.1 编制目的 该文档为选用Appium作为移动设备原生(Native).混合(Hybrid).移动Web(Mobile Web)应用UI自动化测试的相关自动化测试人员.开发人员等提供 ...
- appium+Python 启动app(二)
我们上步操作基本完成,下面介绍编写Python脚本启动app 打开我们pycharm新建.py文件 第一步:输入Python脚本代码: #coding=utf-8 from appium import ...
- appium+Python 启动app(一)
当我们appium和Python环境都配置好了,如何启动我们第一个app呢?下面介绍appium+Python启动app的操作步骤,为了能够详细查看,我们这里使用夜游神模拟器进行示范. 测试项目:QQ ...
- Appium + Python环境搭建(移动端自动化)
安装JDK,配置JDK环境 百度搜索下载就行,这里分享一个下载链接:https://pan.baidu.com/s/1snuTOAx 密码:9z8r. 下载好后点击进行安装.安装好后进行环境变量 ...
随机推荐
- python数据库操作——sqlite3模块
# -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 ''' import sqlit ...
- UVALive 6886 Golf Bot FFT
Golf Bot 题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129724 Description Do ...
- 模板 快速询问GCD
快速询问两个数的GCD 我觉得只有智障会卡这个玩意儿-- const int maxn = 1e6; const int Sqrt_N = 1e3; int pre[maxn + 1] , decom ...
- 记一次centos7.2下用crontab执行定时任务的过程(初级)
实验目的:每分钟往某个文件写数据(crontab最小单位是分钟),具体shell命令我是放在一个文件里的.先创建两个空文件:/tmp/a.txt(目标文件)和/tmp/a.sh(脚本文件). 命令如下 ...
- Python如何import文件夹下的文件
Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...
- Mysql 5.6 慢日志配制
一.配制my.cnf(/etc/my.cnf) slow_query_log=onlong_query_time=1slow_query_log_file=/var/log/slow_query.lo ...
- GCC 对C语言的扩展
http://www.cnblogs.com/emituofo/archive/2012/07/20/2600995.html http://blog.csdn.net/andyhuabing/art ...
- oracle 锁系列
http://www.cnblogs.com/lhrbest/p/6091277.html
- [Node.js]Path模块
摘要 path模块提供了一些处理文件路径问题的工具. path模块 引入模块 var path=require("path"); 方法 1 path.normalize(p)规范化 ...
- python文本 拼接或合并字符串
python文本 拼接.合并字符串 场景: 拼接.合并字符串 在这个场景中,我们首先想到的当然是使用+或者+=将两个字符串连接起来 >>> a='a' >>> ...