Appium_课堂笔记05
1 # TouchAction: 2 1、链式调用,支持的方法有 press,longpress,moveto,release 3 2、perform 调用 4 5 # 九宫格只是TouchAction的一个用法 6 swipe,appium 官方实现了swipe 7 8 # APP H5 测试/webview 9 1、首先必须要开启APP的webview 调试模式,找开发 10 2、必须要切换 context 环境 NATIVE_APP,WEBVIEW_ 11 3、chromefriver 手机浏览器的驱动 确定手机浏览器的版本 --> uctool,68 12 4、指定驱动的位置,caps 参数:chromedriverExecutable 13 14 # 手机上输入快捷键 15 物理按键 Home ,音量+,音量-,电源键,返回键,菜单键 16 # https://www.jianshu.com/p/f7ec856ff56f--指导说明 17 # 层级封装 18 1、封装Keys 类 类属性表示keycode :ENTER -66 19 2、用的很多物理按键,直接basepage 再次封装 20 # 放大和缩小的封装: 21 class BasePage(): 22 def __init__(self, driver): 23 self.driver = driver 24 25 def zoom(self, offset): 26 """放大""" 27 action_1 = TouchAction(self.driver) 28 action_1.press(x=self.width / 2, y=self.height / 2).move_to( 29 x=self.width / 2, y=self.height / 2 - offset).release() 30 action_2 = TouchAction(self.driver) 31 action_2.press(x=self.width / 2, y=self.height / 2).move_to( 32 x=self.width / 2, y=self.height / 2 + offset).release() 33 34 m = MultiAction(self.driver) 35 m.add(action_1, action_2) 36 m.perform() 37 38 39 def pinch(self, offset): 40 """缩小""" 41 action_1 = TouchAction(self.driver) 42 action_1.press(x=self.width / 2, y=self.height / 2 - offset).move_to( 43 x=self.width / 2, y=self.height / 2).release() 44 action_2 = TouchAction(self.driver) 45 action_2.press(x=self.width / 2, y=self.height / 2 + offset).move_to( 46 x=self.width / 2, y=self.height / 2).release() 47 48 m = MultiAction(self.driver) 49 m.add(action_1, action_2) 50 m.perform() 51 52 53 a1 = TouchAction(driver) 54 a2 = TouchAction(driver) 55 a1.press().move_to().release() 56 a2.press().move_to().release() 57 MultiAction(driver).add(a1, a2).perform() 58 # 物理按键 59 class Keys: 60 # 音量+ 61 VOLUME_UP = 24 62 VOLUME_DOWN = 25 63 ENTER = 66 64 65 66 class BasePage(): 67 def __init__(self, driver): 68 self.driver = driver 69 70 def volume_up(self): 71 self.driver.press_keycode(Keys.VOLUME_UP) 72 73 def volume_down(self): 74 self.driver.press_keycode(Keys.VOLUME_DOWN) 75 76 77 78 # 按下物理按键 79 driver.press_keycode(26) 80 81 # 按下音量加 82 driver.press_keycode(Keys.VOLUME_UP) 83 driver.press_keycode(Keys.VOLUME_UP) 84 driver.press_keycode(Keys.VOLUME_UP) 85 driver.press_keycode(Keys.VOLUME_UP) 86 # toast弹窗 87 from appium.webdriver import Remote 88 from appium.webdriver.common.mobileby import MobileBy 89 from appium.webdriver.common.touch_action import TouchAction 90 from selenium.webdriver.common.by import By 91 92 caps = { 93 'platformName': 'Android', 94 'deviceName': 'emulator-5554', 95 'appPackage': 'com.lemon.lemonban', 96 'appActivity': 'com.lemon.lemonban.activity.WelcomeActivity', 97 'chromedriverExecutable': 'D:\chromedriver\chromedriver_242.exe' 98 } 99 driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub', 100 desired_capabilities=caps) 101 driver.implicitly_wait(10) 102 103 # toast 弹框定位文本, 方法一:通过 text 文本定位 toast 弹框 104 text = '用户名或者密码不正确' 105 driver.find_element(By.XPATH, f'//*[contains(@text, "{text}")]') 106 107 # 方法二://android.widget.Toast 108 el = driver.find_element(By.XPATH, '//android.widget.Toast') 109 el.text 110 conftest.py 111 import pytest 112 from appium.webdriver import Remote 113 114 @pytest.fixture() 115 def driver(): 116 """启动和结束appium 对象""" 117 caps = { 118 'platformName': 'Android', 119 'automationName': 'Uiautomator2', # 必须要加才能定位 toast 120 'deviceName': 'emulator-5554', 121 'appPackage': 'com.lemon.lemonban', 122 'appActivity': 'com.lemon.lemonban.activity.WelcomeActivity', 123 'chromedriverExecutable': 'D:\chromedriver\chromedriver_242.exe' 124 } 125 session = Remote(command_executor='http://127.0.0.1:4723/wd/hub', 126 desired_capabilities=caps) 127 session.implicitly_wait(10) 128 yield session 129 session.quit() 130 # TouchAction 是一个手指的操作 131 # MultiAction 132 133 # 实战: 134 1、APP自动化和web自动化吃,几乎所有的自动化测试思维都是通用的 135 2、因为APP和web都是UI测试,测试界面内容 136 3、app是的的一些方法不一样 137 138 # 测试项目 139 1、柠檬班app 调试版的apk 140 2、自己公司的app 141 # APP自动化的区别 142 一、 143 1、就是普通用户无法直接进入指定页面 app 当中有路径依赖关系 144 2、当然如果你只想测试页面的行为,可以跳跳转 start_activity 145 146 二、 147 po 模式一般不以 acitivity 页面,而是使用功能相关的管理 148 149 # 怎样快速入手APP自动化: 150 1、运用自己学习到的代码;首页自我吸收;实践运用; 151 2、在编写脚本,运行,调试报错,运用翻译工具;查看,解决;自己记录问题的解决方法;多处理问题;多上手;效果更明显 152 3、多使用几个APP去调试写脚本;首先是自己公司的,业务逻辑自己清楚; 153 4、在自己面试前找自己对用的公司APP来写脚本;比如:直播app,贷款app,电商app等多练手; 154 5、自我培养编程的思维习惯;熟能生巧,多想想自己的代码是否还能进行优化?
# 框架结构几乎与selenium框架通用:
但是框架在迁移的时候:需要修改一些配置文件:ini的参数
没有截图的功能模块需要删除;page目录下的;或者有的路径拼接不一样的地方也要适当的做修改
测试用例:写在tests里面:
可以把测试用例的步骤输出:
"""测试步骤:app 当中按正常流程有没
1、点击我的柠檬
2、点击头像登录
3、输入用户名和密码
4、点击登录
把定位的方式用函数封装起来
比如:
page = BasePage(driver)
# 点击我的柠檬额
my_locator = (By.XPATH, '//*[@resource-id="com.lemon.lemonban:id/navigation_my"]')
page.click(my_locator)
断言 toast 弹框
assert page.get_toast('用户名或者密码错误')