以最右App为例

.apk文件网盘地址:

链接:https://pan.baidu.com/s/1L4MYkhpb5ECe8XeaneTx_Q

提取码:0jqm

操作类API
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 21:05 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_operate_api(self):
self.driver.implicitly_wait(60)
time.sleep(8) # 控件点击:click()
# 场景:点击关注按钮
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/title")
ele.click()
time.sleep(2) # 点击搜索按钮
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/ic_search_b").click()
time.sleep(3)
# 点击搜索输入框
ele_input = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/search_input")
ele_input.click()
# 控件输入
ele_input.send_keys("123456")
time.sleep(2)
print(ele_input.text)
ele_input.clear()
time.sleep(2) # 获取手机分辨率
height = self.driver.get_window_size()["height"]
width = self.driver.get_window_size()["width"]
print("手机分辨率是:" + str(height) + "*" + str(width)) if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
属性获取
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 21:05 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_attribute(self):
self.driver.implicitly_wait(60)
time.sleep(8) # 获取当前活动页面
current_activity = self.driver.current_activity
print(current_activity) # 定位搜索按钮,赋值给ele_search
ele_search = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/ic_search_b") # 获取控件的属性值
# 获取控件的className
print(ele_search.get_attribute("className"))
# 获取控件的resourceId
print(ele_search.get_attribute("resourceId")) # 判断控件是否显示
print(ele_search.is_displayed())
# 判断控件是否可用
print(ele_search.is_enabled()) """
.ui.home.page.PageMainActivity
android.widget.ImageView
cn.xiaochuankeji.tieba:id/ic_search_b
True
True
执行结束!
""" if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
swipe
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 21:05 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_swipe(self):
self.driver.implicitly_wait(60)
time.sleep(8) self.driver.swipe(500, 1000, 500, 2000) # 手势下拉
time.sleep(3)
self.driver.swipe(500, 2000, 500, 1000) # 手势上滑
time.sleep(3)
self.driver.swipe(500, 2000, 500, 2000, 100) # 点击
time.sleep(3)
self.driver.swipe(500, 2000, 500, 2000, 3000) # 长按
time.sleep(3) # 获取手机分辨率
height = self.driver.get_window_size()["height"]
width = self.driver.get_window_size()["width"]
print("手机分辨率是:" + str(height) + "*" + str(width))
# 按照手机分辨率定位
self.driver.swipe(width * 0.463, height * 0.448, width * 0.463, height * 0.896, 100) if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
TouchAction
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:26 import unittest
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_touch_action(self):
self.driver.implicitly_wait(60)
time.sleep(8) # 点击控件
# 点击搜索控件
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/ic_search_b")
TouchAction(self.driver).tap(ele).perform()
time.sleep(3) # 坐标点击
# 点击坐标(150,150)
# 点击搜索框,出现键盘
TouchAction(self.driver).tap(x=150, y=150).perform()
time.sleep(2) # 按压某个点
# 按压搜索框以外的空白处,键盘消失
TouchAction(self.driver).press(x=150, y=1040).release().perform()
time.sleep(2) # 返回到推荐页面
self.driver.keyevent(4)
time.sleep(3) # 长按某个点
# 长按第一条帖子
TouchAction(self.driver).long_press(x=500, y=600).release().perform()
time.sleep(2) TouchAction(self.driver).press(x=500, y=600).release().perform()
time.sleep(2) # 长按控件
# 长按第一条帖子
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/holder_flow_rmdv")
TouchAction(self.driver).long_press(ele).perform() if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
keyevent

Android KEYCODE键值对:

参考地址:https://blog.csdn.net/shililang/article/details/14449527

# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:26 import unittest
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_key_event(self):
time.sleep(8)
# 返回键
self.driver.keyevent(4)
self.driver.implicitly_wait(5)
# toast提示框
ele = WebDriverWait(self.driver, 20, 0.1).until(
EC.presence_of_element_located((By.XPATH, './/*[contains(@text,"再按一次返回键")]')))
print(ele.text)
# 再按一次返回键,退出程序
self.driver.keyevent(4) """
KEYCODE_MENU 菜单键82
KEYCODE_BACK 返回键4
KEYCODE_SEARCH 搜索键84
KEYCODE_ENTER 回车键66
KEYCODE_DEL 退格键67
KEYCODE_FORWARD_DEL 删除键112
......
"""
截图
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:18 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_screen_shot(self):
self.driver.implicitly_wait(60) # 场景:点击关注按钮,切换至关注页面,截图
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/title")
ele.click()
time.sleep(2)
self.driver.get_screenshot_as_file(r"D:\Desktop\Testman_Study\图库\screen\test.png") if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
中文输入法
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 18:44 import unittest
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait class AndroidTest(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "10"
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' # 输入中文
desired_caps['unicodeKeyboard'] = 'True' # 安装appium自带输入法
desired_caps['resetKeyboard'] = 'True' # 重置键盘,修改默认输入法 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) def tearDown(self):
print("执行完成!") def test_chinese(self):
time.sleep(8)
# 点击第一条帖子的评论按钮
el = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/middle_view").click()
time.sleep(5)
# 点击评论输入框
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").click()
time.sleep(4)
# 输入评论内容12346
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").send_keys('真不错!')
time.sleep(2)
# 点击发送
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/send").click()
# 通过xpath,text属性定位toast提示框
# toast_loc = ("xpath", './/*[contains(@text,"评论发送成功")]')
# 显式等待
ele = WebDriverWait(self.driver, 20, 0.1).until(
expected_conditions.presence_of_element_located((By.XPATH, './/*[contains(@text,"评论发送成功")]')))
# 输出toast提示框的文本内容
print(ele.text)
time.sleep(2)
self.driver.keyevent(4)
重置App
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:06 import os
import unittest
from appium import webdriver PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
) class AndroidTest(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "10"
desired_caps['deviceName'] = 'Android Emulator' # 重置app
desired_caps['fullReset'] = "true"
# .apk文件的路径
desired_caps['app'] = r"D:\Desktop\Testman_Study\apk\zuiyou518.apk"
# desired_caps['app'] = PATH("D:\Desktop\Testman_Study\apk\zuiyou518.apk") desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) def tearDown(self):
print("执行完成!") def test_resetApp(self):
pass
toast
import os
import unittest
import time
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By # Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
) class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity'
# Appium1.6.3版本后才支持toast,之前封装的是Uiautomator,之后才是Uiautomator2
desired_caps['automationName'] = 'Uiautomator2'
desired_caps['newCommandTimeout'] = 200 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
# self.driver.quit()
print("执行结束!") def test_toast(self):
"""
toast:
Android中的Toast是一种简易的消息提示框;
告知用户任务状态,操作结果,例如:发送成功,加载中,删除成功;
Toast会在屏幕所有层的最上方;
显示时间有限,1s+左右消失。 场景:评论帖子->点击发送->提示评论发送成功的灰黑框->1-2s后消失
"""
# self.driver.implicitly_wait(160)
time.sleep(8)
# 点击第一条帖子的评论按钮
el = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/middle_view").click()
time.sleep(5)
# 点击评论输入框
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").click()
time.sleep(4)
# 输入评论内容12346
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").send_keys('123456')
time.sleep(2)
# 点击发送
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/send").click()
# 通过xpath,text属性定位toast提示框
# toast_loc = ("xpath", './/*[contains(@text,"评论发送成功")]')
# 显式等待
ele = WebDriverWait(self.driver, 20, 0.1).until(
EC.presence_of_element_located((By.XPATH, './/*[contains(@text,"评论发送成功")]')))
# 输出toast提示框的文本内容
print(ele.text)
time.sleep(2)
self.driver.keyevent(4) if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)

常用Appium API的更多相关文章

  1. 移动端自动化测试(二)之 Appium常用的API(python)函数介绍

    上一章节已经介绍了Appium的环境搭建,其实只要掌握了Appium的工作原理,前期的准备工作和安装过程是比较简单的.那么当我们搭建好Appium环境后接下来做些什么呢?通常思路是开始appium的第 ...

  2. Appium常用的API函数

    在学习应用一个框架之前,应该了解一下这个框架的整体结构或是相应的API函数.这篇文章还不错:http://blog.sina.com.cn/s/blog_68f262210102vzf9.html,就 ...

  3. Appium 常用的API函数

    常用的API函数[转] http://blog.sina.com.cn/s/blog_68f262210102vzf9.html 获取信息类API (1)获取默认系统语言对应的Strings.xml文 ...

  4. 篇4 安卓app自动化测试-Appium API进阶

    篇4                 安卓app自动化测试-Appium API进阶 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),今天是<安卓app自动化测试& ...

  5. (转载)中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  6. appium API接口

    appium API接口 标签(空格分隔): appium常用api 1.contexts contexts(self) 返回当前会话的上下文,使用可以识别H5页面的控件: driver.contex ...

  7. 中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  8. React常用的API说明

    楼主刚开始学习react,感受到了他的博大精深,看到很多莫名的用法,不知云云,找了很多没有找到参考手册,只有在中文社区和react官方看了一些,收集了一些比较常用的API,有补充的可以楼下评论补充.后 ...

  9. elasticsearch中常用的API

    elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...

随机推荐

  1. 项目开发中的git简单使用

    原文地址: https://www.zhuyilong.fun/tech/the-blog-git.html 示例远程仓库地址: https://github.com/zhu-longge/gitWo ...

  2. Java继承的两道实验题目

    设计一个表示二维平面上点的类Point,包含有表示坐标位置的Protect类型的成员变量 获取和设置x和y值的public方法 package classwork_6; public class Po ...

  3. 区块链V1版本实现之四

    部分程序代码(添加区块): //添加区块 func (bc *BlockChain) AddBlock(data string) { //创建一个区块 //bc.Block的最后一个区块的Hash值就 ...

  4. 2. 三数之和(数组、hashset)

    思路及算法: 该题与第一题的"两数之和"相似,三数之和为0,不就是两数之和为第三个数的相反数吗?因为不能重复,所以,首先进行了一遍排序:其次,在枚举的时候判断了本次的第三个数的值是 ...

  5. SAD DNS--新型DNS缓存中毒攻击

    一.DNS基础知识: 1.DNS简介: DNS 域名服务,用于建立 域名与 ip地址的 一对一 映射.DNS 将域名转换为 IP地址,以便浏览器能够加载 Internet 资源. 类似于一个翻译系统, ...

  6. CSP2020复赛游记

    CSP2020复赛游记 由于本蒟蒻侥幸通过PJ和TG的分数线并且侥幸的拿了一等,所以侥幸的来参加复赛 11.04~11.05 期中考,挂 11.06 对答案,炸 11.07 开始了第一次CSP复赛 坐 ...

  7. 喝完可乐桶后程序员回归本源,开源Spring基础内容

    周六了,又是摸鱼的一天,今天还有点不在状态,脑瓜子迷迷糊糊的,昨晚出去喝可乐桶喝的脑子到现在都不是很正常(奉劝各位可以自己小酌:450ml威士忌+1L多一点可乐刚刚好,可能是我酒量不好),正好没啥事就 ...

  8. 欢迎使用CSDN-markdown编辑器(这个只能看到一次保存一下)

    欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接 ...

  9. jdk版本下载

    oracleJDK oracle各版本下载地址:https://www.oracle.com/technetwork/java/archive-139210.html openJDK 编译好的 ojd ...

  10. pom文件中<dependencies>和<dependencyManagement>的区别

    在父pom中,如果使用了<dependencies>标签,那么在该标签体中的所有jar包,即使子工程中没有写这些依赖,依旧会引用. 如果使用了<dependencyManagemen ...