管理应用

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的更多相关文章

  1. Appium+python自动化27-等待activity出现(android特有的wait_activity)

    前言 在启动app的时候,如果直接做下一步点击操作,经常会报错,于是我们会在启动完成的时候加sleep. 那么问题来了,这个sleep时间到底设置多少合适呢?设置长了,就浪费时间,设置短了,就会找不到 ...

  2. Appium Python 四:怎样获取APP的Package以及Activity

    看到一篇很好的博客:[Android测试][随笔]获得App的包名和启动页Activity 除了博客上的方法,我还找到两种方法: 方法一:aapt 前提需要使用SDK Manager.exe 下载 A ...

  3. Appium python API 总结

    Appium python api 根据testerhome的文章,再补充一些文章里面没有提及的API [TOC] [1]find element driver 的方法 注意:这几个方法只能通过sel ...

  4. appium+Python真机运行测试demo的方法

    appium+Python真机运行测试demo的方法 一,    打开手机的USB调试模式 二,    连接手机到电脑 将手机用数据线连接到电脑,并授权USB调试模式.查看连接的效果,在cmd下运行命 ...

  5. python包管理-distutils,setuptools,pip,virtualenv等介绍

    python包管理-distutils,setuptools,pip,virtualenv等介绍 对于每个编程语言来说打包和发布开发包往往非常重要,而作为一个编程者能够快速容易的获得并应用这些由第三方 ...

  6. appium+python做移动端自动化测试

      1 导言 1.1 编制目的 该文档为选用Appium作为移动设备原生(Native).混合(Hybrid).移动Web(Mobile Web)应用UI自动化测试的相关自动化测试人员.开发人员等提供 ...

  7. appium+Python 启动app(二)

    我们上步操作基本完成,下面介绍编写Python脚本启动app 打开我们pycharm新建.py文件 第一步:输入Python脚本代码: #coding=utf-8 from appium import ...

  8. appium+Python 启动app(一)

    当我们appium和Python环境都配置好了,如何启动我们第一个app呢?下面介绍appium+Python启动app的操作步骤,为了能够详细查看,我们这里使用夜游神模拟器进行示范. 测试项目:QQ ...

  9. Appium + Python环境搭建(移动端自动化)

    安装JDK,配置JDK环境    百度搜索下载就行,这里分享一个下载链接:https://pan.baidu.com/s/1snuTOAx 密码:9z8r. 下载好后点击进行安装.安装好后进行环境变量 ...

随机推荐

  1. PYQT控件使用

    QtGui.QComboBox .addItem(string)#添加字符串项到Item.addItems(list)#添加列表或元组元素到Item.clear()#清除所有Item.clearEdi ...

  2. 2、Redis的基础知识

     写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- 主要内容包括: red ...

  3. python开发_tkinter_复选菜单

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  4. centos7 docker安装和使用_入门教程

    说明:本文也是参考互联网上的文章写的,感谢相关作者的贡献. 操作系统 64位CentOS Linux release 7.2.1511 (Core) 配置好IP:192.168.1.160 修改yum ...

  5. 记一次centos7.2下用crontab执行定时任务的过程(初级)

    实验目的:每分钟往某个文件写数据(crontab最小单位是分钟),具体shell命令我是放在一个文件里的.先创建两个空文件:/tmp/a.txt(目标文件)和/tmp/a.sh(脚本文件). 命令如下 ...

  6. ELNEC Programmer

    BeeHive204 Very fast universal 4x 48-pindrive concurrent multiprogramming system with ISP capability ...

  7. IPD概念

    集成产品开发-IPD简介 内训IPD流程,听完后,觉的流程的力量很强大,可以高效的团队几千上万人的研发团队,来正确地为同一个目标前进.因为讲解者是从华为出来的,所以,相关的案例分析以及理解,都是以华为 ...

  8. [MAC OS] NSButton tag 获取

    @IBAction func switchContentLayout(_ sender: Any) { let button : NSButton = sender as! NSButton;}

  9. Python3.6学习笔记(三)

    面向对象编程 面向对象编程 Object Oriented Programming 简称 OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程 ...

  10. 数学图形之Kuen Surface

    Kuen Surface应该又是一个以数学家名字命名的曲面.本文将展示几种Kuen Surface的生成算法和切图,其中有的是标准的,有的只是相似.使用自己定义语法的脚本代码生成数学图形.相关软件参见 ...