Appium_课堂笔记04

# 将滑动操作运用类函数封装:
1、运用类属性调用的方式:
class BasePage:

    def __init__(self, driver):
        self.driver = driver
        self.size = driver.get_window_size()
        self.height = self.size['height']
        self.width = self.size['width']
2、不确定滑动的方向运用函数封装:
    def swipe(self, direct='left', offset=0.9):
        if direct == 'left':
            return self.swipe_left(offset)
        elif direct == 'right':
            return self.swipe_right(offset)
        elif direct == 'up':
            return self.swipe_up(offset)
# 多个apk混用运用:
TouchAction注意:
- 一定要.perform()
- 可以进行链式调用。
#--混合应用测试
什么是混合应用。
java / kotlin开发的安卓应用程序
混合应用, 使用 web 技术去开发app,
flutter, react native, weex

driver.switch_to.context()

步骤:
1、按普通的安卓组件进行测试
2、当点击进入 h5 页面以后, weditor, inspect, 不能使用了,
3、切换环境,就变成了 web 测试。

准备:
1、辅助定位工具。 uctool, chrome devtools
2、手机浏览器驱动,(版本和电脑是不一样的)
3、webview h5, 必须找开发开启 webview 调试模式。

代码编写:
1、caps 设置驱动路径 chromedriver.exe
2、在进入网页的时候,当需要操作网页当中的元素,要执行 driver.switch_to.context('')


# 我怎么知道这是一个网页,不是原生的


1、设置等待方式:隐性等待
driver.implicitly_wait(10)
2、点击拖动的地方,TouchAction 记得后后面接.perfrome()
from appium.webdriver.common.touch_action import TouchAction

# 获取现在的环境
print("现在的环境是原生界面", driver.current_context)
print("所有的环境", driver.contexts)

# 定位到师资团队的元素
driver.find_element(By.XPATH, '//*[@text="师资团队"]').click()

# 切换到 webview / h5 环境
driver.switch_to.context('WEBVIEW_com.lemon.lemonban')

# 获取现在的环境
print("现在的环境是h5界面", driver.current_context)
print("所有的环境", driver.contexts)

time.sleep(1)

# 我没有导入selenium,
# 定位柠檬班几个字
driver.find_element(By.XPATH, '//*[text()="柠檬班"]').click()

driver.back()

driver.switch_to.context('NATIVE_APP')

# 九宫格解锁的方法:
1、前置工作:
    首先需要将手机屏幕锁定
# 锁屏
driver.lock()
driver.back()
time.sleep(1)--查看效果
2、向上滑动亮屏:显示解锁方法
# 解开屏幕
# driver.unlock()
time.sleep(1)
# 向上滑动
swipe_up(driver)
3、获取手机的分辨率大小
# 获取元素的属性
# el.rect {"x":56,"y":587,"width":608,"height":608}

# 定位元素
el = driver.find_element(By.XPATH, '//*[@resource-id="com.android.systemui:id/lockPatternView"]')
4、把九宫格平均均分为6份;每个点占用了1/6 计算出第一个点的位置
# 第一个点
# rect = {"x":56,"y":587,"width":608,"height":608}
# 获取元素的x, y, width, height
rect = el.rect
start_x = rect['x']
start_y = rect['y']
width = rect['width']
height = rect['height']

5、得出所有的九宫格点的位置
point_01 = {'x': start_x + width * 1 / 6, 'y': start_y + height * 1 / 6}
point_02 = {'x': start_x + width * 1 / 2, 'y': start_y + height * 1 / 6}
point_03 = {'x': start_x + width * 5 / 6, 'y': start_y + height * 1 / 6}
point_04 = {'x': start_x + width * 1 / 6, 'y': start_y + height * 1 / 2}
point_05 = {'x': start_x + width * 1 / 2, 'y': start_y + height * 1 / 2}
point_06 = {'x': start_x + width * 5 / 6, 'y': start_y + height * 1 / 2}
point_07 = {'x': start_x + width * 1 / 6, 'y': start_y + height * 5 / 6}
point_08 = {'x': start_x + width * 1 / 2, 'y': start_y + height * 5 / 6}
point_09 = {'x': start_x + width * 5 / 6, 'y': start_y + height * 5 / 6}

# 6、调用 touchAction 运用链式调用的方式最后.perfrom()

action = TouchAction(driver)
action.press(**point_01).wait(200).
    move_to(**point_04).wait(200).
    move_to(**point_07).wait(200).
    move_to(**point_08).wait(200).
    move_to(**point_09).wait(200).
    release().perform()

time.sleep(1)
  
原文地址:https://www.cnblogs.com/zhang-ping1205/p/14615816.html