做网站microsoft,初中生做网站挣钱,个人网站域名取名,东莞营销型网站设计一、press_keycode
1#xff09;方法说明 press_keycode方法是appium的键盘相关函数#xff0c;可以实现键盘的相关操作#xff0c;比如返回、按键、音量调节等等。也可以使用keyevent方法#xff0c;功能与press_keycode方法类似。 常见按键编码#xff1a;https://www.…一、press_keycode
1方法说明 press_keycode方法是appium的键盘相关函数可以实现键盘的相关操作比如返回、按键、音量调节等等。也可以使用keyevent方法功能与press_keycode方法类似。 常见按键编码https://www.cnblogs.com/bluestorm/p/4886662.html。
# KeyCode各种操作对应的键值码
driver.press_keycode(KeyCode)
2使用示例
# 返回键
driver.press_keycode(4)# 回车键
driver.press_keycode(66)# 回车键
driver.keyevent(66)# 退格键
driver.keyevent(67)
二、scroll方法
1方法说明 scroll方法是滑动页面不过不是滑动滚动条而是获取两个元素然后从一个元素滚动到另一个元素。 要求两个元素都在界面上可见否则会报错。而且滑动持续时间设置越短滑动越快滑动效果会不太准确。所以使用scroll方法时滑动持续时间尽量设置大一些。
# origin_el滚动的起始元素
# destination_el滚动的结束元素
# duration滑动的持续时间默认是600ms时间越大滑动越慢
driver.scroll(origin_el, destination_el, duration)
2使用示例 这里通过代码将设置页从 “应用兼容性” 滑动到 “更多”。 start_element driver.find_element(xpath, //*[text应用兼容性])
end_element driver.find_element(xpath, //*[text更多])
driver.scroll(start_element, end_element, 5000)
三、drag_and_drop方法
1方法说明 drag_and_drop方法是也是滑动页面从一个元素滑动到另一个元素第二个元素代替第一个元素原本屏幕上的位置。 要求两个元素都在界面上可见否则会报错。但是drag_and_drop方法不能设置滑动持续时间但滑动效果比scroll方法更加精确几乎没有惯性。drag_and_drop方法有点类似于慢速版的scroll方法。
# origin_el滚动的起始元素
# destination_el滚动的结束元素
driver.drag_and_drop(origin_el, destination_el)
2使用示例 这里通过代码将设置页从 “应用兼容性” 滑动到 “更多” start_element driver.find_element(xpath, //*[text应用兼容性])
end_element driver.find_element(xpath, //*[text更多])
driver.drag_and_drop(start_element, end_element)
四、swipe方法
1方法说明 swipe方法是从一个坐标点滑动到另一个坐标点也就是说是两点之间的滑动。
# start_x起始坐标点的横坐标
# start_y起始坐标点的纵坐标
# end_x结束坐标点的横坐标
# end_y结束坐标点的纵坐标
# duration滑动的持续时间
driver.swipe(start_x, start_y, end_x, end_y, duration)
2使用示例 这里通过代码将设置页从第一个坐标点6601483滑动到第二个坐标点660533。 driver.swipe(660, 1483, 660, 533, 5000)
五、TouchAction
1TouchAction使用步骤 TouchAction可以实现一些针对手势的操作比如滑动、拖动、长按等我们可以将这些基本手势组合成一个相对复杂的手势比如我们解锁手机或者一些应用软件都有手势解锁的这种方式。
使用TouchAction具体方法之前需要导入TouchAction库。 from appium.webdriver.common.touch_action import TouchAction 创建TouchAction对象。通过对象调用想要执行的手势操作的方法。通过perform方法执行动作所有手势必须通过perform方法来触发。
2tap方法 tap()方法用来模拟手指对某个元素或坐标按下并快速抬起。 tap和click方法的作用差不多但tap可以接收一个坐标值作为点击的区域而click只能接收元素对象作为参数。另外tap()方法还可以设置count参数表示点击次数count2用来表示双击。
def tap(self,element: Optional[WebElement] None,x: Optional[int] None,y: Optional[int] None,count: int 1,
) - TouchAction:Perform a tap action on the elementArgs:element: the element to tapx : x coordinate to tap, relative to the top left corner of the element.y : y coordinate. If y is used, x must also be set, and vice versa
TouchAction(driver).tap(user_name).perform()
3press方法 press()方法用来模拟手指对某个元素或坐标一直按下。
def press(self,el: Optional[WebElement] None,x: Optional[int] None,y: Optional[int] None,pressure: Optional[float] None,
) - TouchAction:Begin a chain with a press down action at a particular element or pointArgs:el: the element to pressx: x coordiate to press. If y is used, x must also be sety: y coordiate to press. If x is used, y must also be set
TouchAction(driver).press(x100, y200).perform()
4release方法 release()方法用来模拟手指抬起。
def release(self) - TouchAction:End the action by lifting the pointer off the screen
Returns:TouchAction: Self instance
self._add_action(release, {})return self
TouchAction(driver).press(x100, y200).release().perform()
5wait方法 wait()方法用来模拟手指等待。 def wait(self, ms: int 0) - TouchAction:Pause for ms milliseconds.
Args:ms: The time to pauseReturns:TouchAction: Self instanceTouchAction(driver).press(x100, y200).wait(3000).release().perform()
6move_to方法 wait()方法用来模拟手指移动到某个元素或坐标。
def move_to(self, el: Optional[WebElement] None, x: Optional[int] None, y: Optional[int] None
) - TouchAction:Move the pointer from the previous point to the element or point specified
Args:el: the element to be moved tox: x coordiate to be moved to. If y is used, x must also be sety: y coordiate to be moved to. If x is used, y must also be setReturns:TouchAction: Self instanceTouchAction(driver).press(x400, y500).move_to(500, 600).perform()
7使用示例 通过代码绘制出下图中的图案。 8报错分析
from appium import webdriver
import time# 设置启动参数
desired_cap {}
desired_cap[platformName] Android
desired_cap[platformVersion] 6.0.1
desired_cap[deviceName] 127.0.0.1:7555
# 必须参数指定被测软件的包名
desired_cap[appPackage] com.android.settings
# 必须参数指定要打开app的哪个页面
desired_cap[appActivity] .ChooseLockPattern
desired_cap[automationName] Uiautomator2
desired_cap[noReset] True
desired_cap[newCommandTimeout] 6000
desired_cap[unicodeKeyboard] True
desired_cap[resetKeyboard] Truedriver webdriver.Remote(http://localhost:4723/wd/hub, desired_cap) 通过adb命令获取到绘制图案页面的activity为.ChooseLockPattern但是通过执行上面代码不能打开设置app报错Original error: Cannot start the com.android.settings application. Original error: The permission to start .ChooseLockPattern activity has been denied. 原因是app应用没开权限对于activity是禁止外部调用的。
9解决办法
将AndroidManifest.xml文件中将Activity设置成允许调用Android:exported”true”加上权限后再重新打apk包。通过代码打开app应用的启动页然后通过定位菜单元素逐层点击进入下一层界面直到打开绘制图案界面为止。
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time# 设置启动参数
desired_cap {}
desired_cap[platformName] Android
desired_cap[platformVersion] 6.0.1
desired_cap[deviceName] 127.0.0.1:7555
# 必须参数指定被测软件的包名
desired_cap[appPackage] com.android.settings
# 必须参数指定要打开app的哪个页面
desired_cap[appActivity] .Settings
desired_cap[automationName] Uiautomator2
desired_cap[noReset] True
desired_cap[newCommandTimeout] 6000
desired_cap[unicodeKeyboard] True
desired_cap[resetKeyboard] Truedriver webdriver.Remote(http://localhost:4723/wd/hub, desired_cap)
time.sleep(5)# 1.获取手机设备宽、高信息
x driver.get_window_size()[width]
y driver.get_window_size()[height]# 2.将设置app的启动界面向上滑动半屏使 “安全” 菜单项可见
driver.swipe(x * 0.5, y * 0.5, x * 0.5, 0)# 3.依次点击菜单 “安全” - “屏幕锁定方式” - “图案”
driver.find_element(xpath, //*[text安全]).click()
time.sleep(1)
driver.find_element(xpath, //*[text屏幕锁定方式]).click()
time.sleep(1)
driver.find_element(xpath, //*[text图案]).click()
time.sleep(1)# 4.绘制图案
ta TouchAction(driver)
# 按住第一个点
ta.press(x145, y564).wait(1000)
ta.move_to(x449, y564).wait(1000)
ta.move_to(x748, y564).wait(1000)
ta.move_to(x748, y863).wait(1000)
ta.move_to(x748, y1165).wait(1000)
# 移动到最后一个点之后松手
ta.release().perform()
10如何定位不可见元素 在上述示例中打开app应用的启动页后需要先打开 “安全” 菜单但界面菜单项过多屏幕太小导致 “安全” 菜单不可见。所以需要先将屏幕进行滑动使得 “安全” 菜单可见后再进行操作。 在上述示例中将屏幕向上滑动半屏后就能看见 “安全” 菜单所以在代码中将滑动屏幕操作直接写死了直接滑动半屏然后进行定位、操作等等。 但有时候要定位的元素可能需要滑动好多屏才能看见所以不能直接在代码中写死而应该在while循环中边滑动屏幕、边对元素进行定位当定位到目标元素后跳出循环。
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time# 设置启动参数
desired_cap {platformName: android,platformVersion: 6.0.1,deviceName: 127.0.0.1:7555,appPackage: com.android.settings,appActivity: .Settings,automationName: Uiautomator2,noReset: True,unicodeKeyboard: True,resetKeyboard: True
}
driver webdriver.Remote(http://localhost:4723/wd/hub, desired_cap)
time.sleep(5)# 1.获取手机设备的宽、高信息
x driver.get_window_size()[width]
y driver.get_window_size()[height]# 2.不断向上滑动半屏查找“开发者选项”菜单
while True:try:driver.find_element(xpath, //*[text开发者选项]).click()breakexcept NoSuchElementException:print(当前屏幕未找到“开发者选项”菜单!)# 每次向上滑动半屏driver.swipe(0.5 * x, 0.5 * y, 0.5 * x, 0)
六、使用appium在脚本中启动其他app
# appPackage被启动应用的包名
# appActivity被启动应用的activity名字
driver.start_activity(appPackage, appActivity)
七、使用appium获取包名和Activity的名字
# 获取当前打开的app应用包名
driver.current_package# 获取当前打开的app应用所显示界面的activity名
driver.current_activity
八、使用appium关闭当前打开的app应用
# 关闭当前打开的app应用
driver.close_app()# 退出driver对象
driver.quit()
九、使用appium安装和卸载app应用 安装、卸载app应用以及判断app是否安装返回值为bool类型True表示已安装False表示未安装。
# 1.安装app应用
# app_pathapk文件的路径
driver.install_app(app_path)# 2.卸载app应用
# app_idapp应用包名
driver.remove_app(app_id)# 3.判断app是否安装
# app_idapp应用包名
driver.is_app_installed(app_id)
十、使用appium将应用置于后台 模拟home键将app应用置于后台。注意停留秒数到达后app应用会自动回到前台。 某些应用在进入后台一段时间后重新回到前台时会要求输入密码如果自动化需要测试这种功能可以使用这个api来测试。这里涉及两个概念
热启动应用从后台回到前台叫热启动。冷启动第一次打开某个应用叫冷启动。
# seconds在后台停留多少秒
driver.background_app(seconds)
十一、获取手机设备分辨率
x driver.get_window_size()[width]
y driver.get_window_size()[height]
十二、获取手机截图
# filename截图保存的位置
driver.get_screenshot_as_file(filename)
driver.get_screenshot_as_file(rE:\AndroidSDK\test.png)
十三、获取和设置手机设备网络状态
from appium.webdriver.connectiontype import ConnectionType# 1.获取手机网络状态
driver.network_connection# 2.设置手机网络状态
# connectionType网络状态码
driver.set_network_connection(ConnectionType.NO_CONNECTION | ConnectionType.AIRPLANE_MODE | ConnectionType.WIFI_ONLY | ConnectionType.DATA_ONLY | ConnectionType.ALL_NETWORK_ON)
1飞行模式、 2只开wifi、4只开流量、6网络全开。
Value (Alias) DataWifiAirplane Mode0 (None) 0001 (Airplane Mode)0012 (Wifi only) 0104 (Data only) 1006 (All network on) 110
十四、操作手机通知栏 测试某些应用进行消息推送时需要检查通知栏是否有消息通知。
# 打开通知栏
driver.open_notifications()