目录
一、滑动和拖拽事件
1.1.应用场景
我们在做自动化测试的时候,有些按钮是需要滑动几次屏幕后才会出现,此时,我们需要使用代码来模拟手指的滑动,也就是我们将要学习的滑动和拖拽事件
1.2.swipe滑动事件
1).概念
从一个坐标位置滑动到另一个坐标位置,只能是两个点之间的滑动。
2).方法
2.1).start_x:起点x轴坐标
2.2).start_y:起点Y轴坐标
2.3).end_x:终点x轴坐标
2.4).end_y:终点Y轴坐标
2.5).duration:清动这个操作一共持续的时间长度,单位:mS
2.6).driver.swipe(start_x,start_y,end_x, end_y.,duration=None)
3).核心代码
driver.swipe(100,2000,100,1000)
4).特点
4.1).参数是坐标点
4.2).持续时间短,惯性大
4.3).持续时间长,惯性小
5).代码
import unittest
from time import sleep
from appium import webdriver
from selenium.webdriver.common.by import By
# 打开相册
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url,capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
self.driver.quit()
def test_find_battery(self) -> None:
# 向下滑动一段距离
# 获取窗口的宽和高
height = self.driver.get_window_size().get('height')
weight = self.driver.get_window_size().get('width')
# 执行滑动操作
self.driver.swipe(start_x=weight * 0.5,
start_y=height * 0.8,
end_x=weight * 0.5,
end_y=height * 0.2, duration=2000)
if __name__ == '__main__':
unittest.main()
1.3.scroll滑动事件
1).概念
从一个元素滑动到另一个元素,直到页面自动停止。
2).方法
2.1).origin_el:滑动开始的元素
2.2).destination_el:滑动结束的元素
2.3).driver.scroll(origin_el, destination_el)
3).核心代码
save_button=driver.find_element_by_xpath(//°[@text='存f者']")more_button =driver.find_element_by_xpath("//*[@text='更多']")driver.scro11(save_button,more_button)
4).小结
4.1).参数是元素
4.2).不能设置持续时间,惯性很大
5).代码
import unittest
from time import sleep
from appium import webdriver
from selenium.webdriver.common.by import By
# 打开相册
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url,capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(2)
self.driver.quit()
def test_find_battery(self) -> None:
# 向下滑动一段距离
# 获取battery
Battery = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
Storage = self.driver.find_element(By.XPATH, '//*[@text="Storage"]')
self.driver.scroll(Storage,Battery)
if __name__ == '__main__':
unittest.main()
1.4.drag_and_drop拖拽事件
1).概念
从一个元素滑动到另一个元素,第二个元素替代第一个元素原本屏幕上的位置。
2).方法
2.1).origin_el:滑动开始的元素
2.2).destination_el:滑动结束的元素
2.3).driver.drag_and_drop(origin_el,destination_el)
3).核心代码
save_button = driver.find_element_by_xpath(//[@text='存f’]")more_button =driver.find_element_by_xpath("//*[@text='更多’]")driver.drag_and_drop(save_button,more_button)
4).小结
4.1).参数是元素
4.2).不能设置持续时间,没有惯性
5).代码
import unittest
from time import sleep
from appium import webdriver
from selenium.webdriver.common.by import By
# 打开相册
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url,capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(2)
self.driver.quit()
def test_find_battery(self) -> None:
# 向下滑动一段距离
# 获取battery
Battery = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
Storage = self.driver.find_element(By.XPATH, '//*[@text="Storage"]')
self.driver.drag_and_drop(Storage,Battery)
if __name__ == '__main__':
unittest.main()
1.5.滑动和拖拽事件的选择
1).滑动和拖拽无非就是考虑是否有“惯性”,以及传递的参数是“元素”还是“坐标”。
2).可以分成以下四种情况
2.1).有“惯性”,传入“元素”--->scroll
2.2).无“惯性”,传入“元素”"----> drag_and_drop
2.3).有“惯性”,传入“坐标”-”---->Swipe,并且设置较短的duration时间
2.4).无“惯性”,传入“坐标”---->swipe,并且设置较长的duration时间
二、高级手势ActionChains
2.1.应用场景
ActionChains可以实现一些针对手势的操作,比如滑动、长按、拖动等。我们可以将这些基本手势组合成一个相对复杂的手势。比如,我们解锁手机或者一些应用软件都有手势解锁的这种方式。
2.2.使用步骤
1).创建ActionChains对象
2).通过对象调用想执行的手势
3).通过perform()执行动作
2.3.注意点
所有手势都要通过执行perform()函数才会运行。
2.4.方法
1).手指轻敲操作 (掌握)
1.1).应用场景
模拟手指对某个元素或坐标按下并快速抬起。比如,固定点击(100,100)的位置。
1.2).方法
1.element:元素
2.X:X坐标
3.y:y坐标
4.ActionChains(driver).click(element=None,x=None,y=None).perform()
1.3).核心代码
1.e1 = driver.find_element_by_xpath("//*[contains(@text, *wLAn')]")
2.ActionChains(driver).click(e1).perform()
1.4).代码
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
capabilities = {
'platformName': 'Android',
'automationName': 'uiautomator2',
'deviceName': 'Android',
'appPackage': 'com.android.settings',
'appActivity': '.Settings',
'language': 'en',
'locale': 'US',
'browserName': '', # 添加这一行
}
appium_server_url = 'http://localhost:4723'
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
# 其他代码保持不变
def test_find_battery(self) -> None:
# 创建touchaction对象
el = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
actions = ActionChains(self.driver) # 使用ActionChains替换TouchAction
actions.click(el).perform() # 使用actions.tap()替换TouchAction().tap()
2).手势按下和抬起操作(掌握)
2.1).应用场景
模拟手指一直按下,模拟指抬起。可以用来组合成轻敲或长按的操作
2.2).方法
1.按下操作---->ActionChains(driver).click_and_hold(el=None,x=None,y=None).perform()
2.抬起操作---->ActionChains(driver).release().perform()
2.3).核心代码
1.ActionChains(driver).click_and_hold(x=650,y=650).perform()
2.time.sleep(2)
3.ActionChains(driver) .press(x=650, y=650).perform()
2.4).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(2)
self.driver.quit()
def test_find_battery(self) -> None:
# 创建touchaction对象
el = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
tc = ActionChains(self.driver)
# 使用click_and_hold方法按住元素并保持
tc.click_and_hold(el)
# 使用pause方法指定按住的停顿时间2s
tc.pause(2)
# 使用release方法松开
tc.release()
# 最后使用perform方法执行以上操作。
tc.perform()
if __name__ == '__main__':
unittest.main()
3).等待操作 (掌握)
3.1).应用场景
模拟手指等待
3.2).方法
ActionChains(driver).pause(time)--->time:秒
3.3).核心代码
ActionChains(driver).pause(2)
3.4).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(2)
self.driver.quit()
def test_find_battery(self) -> None:
# 创建touchaction对象
el = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
tc = ActionChains(self.driver)
# 使用click_and_hold方法按住元素并保持
tc.click_and_hold(el)
# 使用pause方法指定按住的停顿时间2s
tc.pause(2)
# 使用release方法松开
tc.release()
# 最后使用perform方法执行以上操作。
tc.perform()
if __name__ == '__main__':
unittest.main()
4).长按操作 (掌握)
4.1).应用场景
模拟手指对元素或坐标的长按操作。比如,长按某个按钮弹出菜单。
4.2).方法
ActionChains(driver).click_and_hold(e1=None, x=None, y=None, duration=1000) .perform()
4.3).核心代码
1.ActionChains(driver).click_and_hold(el=None,x=None,y=None).perform()
2.time.sleep(2)
3.AtionChains(driver).click_and_hold(el=None,x=None,y=None).release.perform()
4.4).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(2)
self.driver.quit()
def test_find_battery(self) -> None:
# 创建touchaction对象
el = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
tc = ActionChains(self.driver)
# 使用click_and_hold方法按住元素并保持
tc.click_and_hold(el)
tc.perform()
if __name__ == '__main__':
unittest.main()
5).手指移动操作(掌握)
5.1).应用场景
模拟手指移动操作,比如,手势解锁需要先按下,再移动
5.2).方法
ActionChains(driver).move_to_element_with_offset (x=None,y=None)
5.3).核心代码
AtionChains(driver).move_to_element_with_offset (x=None,y=None)
三、手机操作API
3.1.获取手机分辨率 (掌握)
1).应用场景
自动化测试可能会需要根据当前设置的屏幕分辨率来计算一些点击或者滑动的坐标
2).方法
driver.get_window_size()
3).核心代码
print(self.driver.get_window_size())
4).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(2)
self.driver.quit()
def test_find_battery(self) -> None:
# 获取手机分辨率
print(self.driver.get_window_size())
if __name__ == '__main__':
unittest.main()
3.2.手机截图 (掌握)
1).应用场景
有些自动化的操作可能没有反应,但并不报错,此时我们就可以将操作过后的关键情况,截图留存,后期也可以根据图片发现问题
2).方法
driver.get_screenshot_as_file(filename)--->filename 指定路径下,指定格式的图片
3).核心代码
driver.get_screenshot_as_file(./phone.png")
4).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(2)
self.driver.quit()
def test_find_battery(self) -> None:
# 等待两秒钟
sleep(2)
# 截图
self.driver.get_screenshot_as_file('./phone.png')
if __name__ == '__main__':
unittest.main()
3.3.获取和设置手机网络 (掌握)
1).应用场景
视频应用在使用流量看视频的时候,大部分都会提示用户正在是否继续播放。作为测试人员,我们可能需要用自动化的形式来判断是否有对应的提示。即,用流量的时候应该有提示,不用流量的时候应该没有提示。
2).获取手机网络
2.1).方法
driver.network_connection
2.2).核心代码
print(driver.network_connection)
2.3).底层数字表示
3).设置手机网络
3.1).方法
driver.set_network_connection(connection)
3.2).核心代码
driver.set_network_connection(2)
4).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
self.driver.quit()
def test_find_battery(self) -> None:
# 获取手机网络
print(self.driver.network_connection)
# 设置手机网络
self.driver.set_network_connection(2)
print(self.driver.network_connection)
if __name__ == '__main__':
unittest.main()
5).注意
5.1).网络的类型,建议使用系统提供的类型
5.2).fromappium.webdriver.connectiontype import ConnectionType
3.4.发送键到设备 (掌握)
1).应用场景
模拟按“返回键”“home键”等等操作,比如,很多应用有按两次返回键退出应用的功能,如果这个功能需要我们做自动化,那么一定会用到这个方法
2).方法
2.1).driver.press_keycode(keycode,metastate=None)
2.2).keycode:发送给设备的关键代码
2.3).metastate:关于被发送的关键代码的元信息,一般为默认值
3).注意
按键对应的编码可以搜索androidkeycode
4).核心代码
4.1).driver.press_keycode(24)
4.2).driver.press_keycode(24)
4.3).driver.press_keycode(24)
4.4).driver.press_keycode(4)
4.5).driver.press_keycode(25)
4.6).driver.press_keycode(25)
5).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(5)
self.driver.quit()
def test_find_battery(self) -> None:
# 点击三次音量加,再点击返回,再点击两次音量减
self.driver.press_keycode(24)
self.driver.press_keycode(24)
self.driver.press_keycode(24)
sleep(2)
self.driver.press_keycode(4)
sleep(2)
self.driver.press_keycode(25)
self.driver.press_keycode(25)
if __name__ == '__main__':
unittest.main()
3.5.操作手机通知栏 (掌握)
1).应用场景
测试即时通信类软件的时候,如果A给B发送一条消息,B的通知栏肯定会显示对应的消息。我们想通过通知栏来判断B是否收到消息,一定要先操作手机的通知栏
2).方法
driver.open_notifications()
3).注意点
appium官方并没有为我们提供关闭通知的api,那么现实生活中怎么关闭,就怎样操作就行,比如,手指从下往上滑动,或者,按返回键
4).核心代码
4.1).driver.open_notifications()--->打开通知栏
4.2).driver.press_keycode(4)---->关闭通知栏
5).代码
import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
# 打开设置
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'
capabilities['browserName'] = ''
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
# 获取driver
self.driver = webdriver.Remote(appium_server_url, capabilities)
self.driver.implicitly_wait(30)
def tearDown(self) -> None:
if self.driver:
# 退出程序
sleep(5)
self.driver.quit()
def test_find_battery(self) -> None:
# 打开通知栏
self.driver.open_notifications()
# 等待两秒钟
sleep(2)
# 关闭通知栏
self.driver.press_keycode(4)
if __name__ == '__main__':
unittest.main()