主要是搭建python+appium+pyCharm环境进行自动化测试,该脚本主要是对酷狗音乐进行自动化测试:
其中坐标主要是取的bounds[x1,y1][x2,y2],参考:(251条消息) Appium自动化测试(六)之坐标介绍_洛书测试-CSDN博客_appium bounds
# This sample code uses the Appium python client v2
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
import time
import os
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
# For W3C actions
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
caps = {}
caps["platformName"] = "Android"
caps["appium:platformVersion"] = "9.0.0"
caps["appium:deviceName"] = "spm8666p1_64_car"
caps["appium:appPackage"] = "com.chinatsp.music"
caps["appium:appActivity"] = ".MusicMainActivity"
caps["appium:ensureWebviewsHavePages"] = True
caps["appium:nativeWebScreenshot"] = True
caps["appium:newCommandTimeout"] = 3600
caps["appium:connectHardwareKeyboard"] = True
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
def quick_press(size, location):
ele1_size = size
# 元素的宽
width = ele1_size['width']
# 元素的高
height = ele1_size['height']
ele1_coordinate = location
# 元素左上角横坐标
x = ele1_coordinate['x']
# 元素左上角纵坐标
y = ele1_coordinate['y']
el1_center_x = x + int(width / 2)
el1_center_y = y + int(height / 2)
driver.tap([(el1_center_x, el1_center_y)], 500)
def press_top500():
el1 = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/androidx.recyclerview.widget.RecyclerView/androidx.appcompat.widget.LinearLayoutCompat[1]")
ele1_size = el1.size
ele1_coordinate = el1.location
quick_press(ele1_size, ele1_coordinate)
#el1.click()
#driver.implicitly_wait(2)
time.sleep(1)
el2 = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.TextView[2]")
el2.click()
time.sleep(1)
#el3 = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.widget.LinearLayout[1]")
#ele3_size = el3.size
#ele3_coordinate = el3.location
#quick_press(ele3_size, ele3_coordinate)
driver.tap([(700, 250)], 500)
time.sleep(1)
#driver.implicitly_wait(1)
#el4 = driver.find_element(by=AppiumBy.ID, value="com.chinatsp.music:id/ivNext")
continue_press()
driver.keyevent(4)
time.sleep(1)
driver.keyevent(4)
def continue_press():
i = 0
while i < 100:
try:
#next_btn = driver.find_element(by=AppiumBy.ID, value="com.chinatsp.music:id/ivNext")
#adb = "adb shell input tap 700 635"
#os.system(adb)
driver.tap([(700, 635)], 500)
except StaleElementReferenceException:
time.sleep(2)
break
except NoSuchElementException:
time.sleep(2)
break
i = i+1
print("count:",i)
press_top500()
driver.quit()
重复执行在获取界面元素的时候超时比较验证,全部改为坐标点击,性能完美提升;
# This sample code uses the Appium python client v2
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
import time
import os
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
# For W3C actions
from selenium.common.exceptions import StaleElementReferenceException, NoSuchElementException, \
InvalidElementStateException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
caps = {}
caps["platformName"] = "Android"
caps["appium:platformVersion"] = "9.0.0"
caps["appium:deviceName"] = "spm8666p1_64_car"
caps["appium:appPackage"] = "com.chinatsp.music"
caps["appium:appActivity"] = ".MusicMainActivity"
caps["appium:ensureWebviewsHavePages"] = True
caps["appium:nativeWebScreenshot"] = True
caps["appium:newCommandTimeout"] = 3600
caps["appium:connectHardwareKeyboard"] = True
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
def quick_press(size, location):
ele1_size = size
# 元素的宽
width = ele1_size['width']
# 元素的高
height = ele1_size['height']
ele1_coordinate = location
# 元素左上角横坐标
x = ele1_coordinate['x']
# 元素左上角纵坐标
y = ele1_coordinate['y']
el1_center_x = x + int(width / 2)
el1_center_y = y + int(height / 2)
driver.tap([(el1_center_x, el1_center_y)], 500)
def press_top500():
#el1 = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/androidx.recyclerview.widget.RecyclerView/androidx.recyclerview.widget.RecyclerView/androidx.appcompat.widget.LinearLayoutCompat[1]")
#ele1_size = el1.size
#ele1_coordinate = el1.location
#quick_press(ele1_size, ele1_coordinate)
driver.tap([(1200, 250)], 500)
#el1.click()
#driver.implicitly_wait(2)
time.sleep(1)
#el2 = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.TextView[2]")
#el2.click()
driver.tap([(100, 150)], 500)
time.sleep(1)
#el3 = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/androidx.recyclerview.widget.RecyclerView/android.widget.LinearLayout[1]")
#ele3_size = el3.size
#ele3_coordinate = el3.location
#quick_press(ele3_size, ele3_coordinate)
driver.tap([(700, 250)], 500)
time.sleep(1)
#driver.implicitly_wait(1)
#el4 = driver.find_element(by=AppiumBy.ID, value="com.chinatsp.music:id/ivNext")
continue_press()
driver.keyevent(4)
time.sleep(3)
driver.keyevent(4)
time.sleep(3)
def continue_press():
i = 0
while i < 100:
try:
#next_btn = driver.find_element(by=AppiumBy.ID, value="com.chinatsp.music:id/ivNext")
#adb = "adb shell input tap 700 635"
#os.system(adb)
driver.tap([(700, 635)], 500)
except StaleElementReferenceException:
time.sleep(2)
break
except NoSuchElementException:
time.sleep(2)
break
i = i+1
print("count:",i)
n = 0
while n < 1000:
try:
press_top500()
except InvalidElementStateException:
time.sleep(2)
break
n = n + 1
print("continue count:", n)
driver.quit()
参考:
安装;https://blog.csdn.net/weixin_39906130/article/details/111439362 https://www.cnblogs.com/jyd0124/p/appium.html https://shimo.im/docs/D9rvq8kc8GgH9QgH/read 配置: { ? "platformName": "Android", ? "appium:platformVersion": "9.0.0", ? "appium:deviceName": "spm8666p1_64_car", ? "appium:appPackage": "com.chinatsp.music", ? "appium:appActivity": ".MusicMainActivity" }
?
|