快捷按键与多指操作、toast弹框

本文介绍了使用Appium进行移动应用自动化测试的一些实用技巧,包括如何模拟物理按键操作、实现多指触控放大与缩小功能及定位toast弹框等。

手机上输入快捷键 —— 按键

物理按键 Home, 音量加, 音量-, 电源键, 返回, 菜单。
https://www.jianshu.com/p/f7ec856ff56f

import time

from appium.webdriver import Remote
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

caps = {
    'platformName': 'Android',
    'deviceName': 'emulator-5554',
    'appPackage': 'com.lemon.lemonban',
    'appActivity': 'com.lemon.lemonban.activity.WelcomeActivity',
    'chromedriverExecutable': 'D:\chromedriver\chromedriver_242.exe'
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
                desired_capabilities=caps)
driver.implicitly_wait(10)


class Keys:
    # 音量+
    VOLUME_UP = 24
    VOLUME_DOWN = 25
    ENTER = 66

# 按下物理按键
driver.press_keycode(26)

=============================================================

2 层封装:

1、封装 Keys 类,类属性表示 keycode : ENTER = 66
2、用的很多的物理按键,直接在 basepage 再次封装。

import time

from appium.webdriver import Remote
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

caps = {
    'platformName': 'Android',
    'deviceName': 'emulator-5554',
    'appPackage': 'com.lemon.lemonban',
    'appActivity': 'com.lemon.lemonban.activity.WelcomeActivity',
    'chromedriverExecutable': 'D:\chromedriver\chromedriver_242.exe'
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
                desired_capabilities=caps)
driver.implicitly_wait(10)


class Keys:
    # 音量+
    VOLUME_UP = 24
    VOLUME_DOWN = 25
    ENTER = 66


class BasePage():
    def __init__(self, driver):
        self.driver = driver

    def volume_up(self):
        self.driver.press_keycode(Keys.VOLUME_UP)

    def volume_down(self):
        self.driver.press_keycode(Keys.VOLUME_DOWN)



# 按下物理按键
driver.press_keycode(26)

# 按下音量加
driver.press_keycode(Keys.VOLUME_UP)
driver.press_keycode(Keys.VOLUME_UP)
driver.press_keycode(Keys.VOLUME_UP)
driver.press_keycode(Keys.VOLUME_UP)

==================================================================

多指操作

放大和缩小的封装:
TouchAction 是一个手指的操作
MultiAction

加载:add()
add(action1,action2,…)将touchaction的对象添加到MultiAction中
action1,action2描述一个手指要执行的动作

例如:
action1=TouchAction(driver)
Action1.press(x,y)wait(1000).move_to(x1,y1).release()
MultiAction(driver).add(action1)

from appium.webdriver.common.multi_action import MultiAction

from appium.webdriver.common.touch_action import TouchAction
import time

from appium.webdriver import Remote
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

caps = {
    'platformName': 'Android',
    'deviceName': 'emulator-5554',
    'appPackage': 'com.lemon.lemonban',
    'appActivity': 'com.lemon.lemonban.activity.WelcomeActivity',
    'chromedriverExecutable': 'D:\chromedriver\chromedriver_242.exe'
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
                desired_capabilities=caps)
driver.implicitly_wait(10)

class BasePage():
    def __init__(self, driver):
        self.driver = driver

    def zoom(self, offset):
        """放大"""
        action_1 = TouchAction(self.driver)
        action_1.press(x=self.width / 2, y=self.height / 2).move_to(
            x=self.width / 2, y=self.height / 2 - offset).release()
        action_2 = TouchAction(self.driver)
        action_2.press(x=self.width / 2, y=self.height / 2).move_to(
            x=self.width / 2, y=self.height / 2 + offset).release()

        m = MultiAction(self.driver)
        m.add(action_1, action_2)
        m.perform()


    def pinch(self, offset):
        """缩小"""
        action_1 = TouchAction(self.driver)
        action_1.press(x=self.width / 2, y=self.height / 2 - offset).move_to(
            x=self.width / 2, y=self.height / 2).release()
        action_2 = TouchAction(self.driver)
        action_2.press(x=self.width / 2, y=self.height / 2 + offset).move_to(
            x=self.width / 2, y=self.height / 2).release()

        m = MultiAction(self.driver)
        m.add(action_1, action_2)
        m.perform()


a1 = TouchAction(driver)
a2 = TouchAction(driver)
a1.press().move_to().release()
a2.press().move_to().release()
MultiAction(driver).add(a1, a2).perform()

=========================================================

toast弹框

from appium.webdriver.common.multi_action import MultiAction

from appium.webdriver.common.touch_action import TouchAction
import time

from appium.webdriver import Remote
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

caps = {
    'platformName': 'Android',
    'deviceName': 'emulator-5554',
    'appPackage': 'com.lemon.lemonban',
    'appActivity': 'com.lemon.lemonban.activity.WelcomeActivity',
    'chromedriverExecutable': 'D:\chromedriver\chromedriver_242.exe'
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
                desired_capabilities=caps)
driver.implicitly_wait(10)

# toast 弹框定位文本, 方法一:通过 text 文本定位 toast 弹框
text = '用户名或者密码不正确'
driver.find_element(By.XPATH, f'//*[contains(@text, "{text}")]')

# 方法二://android.widget.Toast
driver.find_element(By.XPATH, '//android.widget.Toast')

==========================================================

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值