- 连接模拟器操作
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import _find_element
from appium.webdriver.common.touch_action import TouchAction
desired_caps={}
desired_caps['platformName']='Android'
desired_caps['deviceName']='127.0.0.1:62025'
desired_caps['platforVersion']='5.1.1'
desired_caps['automationName']='uiautomator2'
desired_caps['app']=r'C:\Users\Shuqing\Desktop\kaoyan3.1.0.apk'
desired_caps['appPackage']='com.tal.kaoyan'
desired_caps['appActivity']='com.tal.kaoyan.ui.activity.SplashActivity'
desired_caps['noReset']='False'
desired_caps['unicodeKeyboard']="True"
desired_caps['resetKeyboard']="True"
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(2)
- 通过ID定位
def check_cancleBtn():
print("check cancleBtn")
try:
cancleBtn=driver.find_element(By.ID,"todo")
except NoSuchElementException as msg:
print(msg)
else:
cancleBtn.click()
def check_skipBtn():
print('check skip')
try:
skipBtn=driver.find_element(By.ID,"todo")
except NoSuchElementException as msg:
print(msg)
else:
skipBtn.click()
check_cancleBtn()
check_skipBtn()
- 元素等待,显示等待
WebDriverWait(driver,5).until(lambda x:x.find_element(By.ID,"todo"))
driver.find_element(By.ID,"todo").click()
- 通过classname定位
driver.find_element(By.CLASS_NAME,"todo").click()
ele = driver.find_element(By.CLASS_NAME,"todo")
ele.clear()
ele.send_keys("51zxw")
-
通过xpath定位
driver.find_element(By.XPATH,"todo").click() -
通过list定位
ele=driver.find_element(By.ID, "todo")
ele[10].click()
- 通过relative定位
root_element=driver.find_element(By.CLASS_NAME,"TODO1")
root_element.find_element(By.ID, "todo")
- 通过uiautomator定位
driver.find_element_by_android_uiautomator('new UiSelector().className("todo")').send_keys("51zxw")
driver.find_element_by_android_uiautomator('new UiSelector().reSourceId("todo")').send_keys("123456")
- toast元素定位
error_message="todo"
message='todo{}'.format(error_message)
toast_element=WebDriverWait(driver,5).until(lambda x:x.find_element(By.XPATH,message))
print(toast_element.text)
- 切换到h5页面操作
- website的操作
contexts=driver.contexts
print(contexts)
driver._switch_to.context('WEBVIEW_com.wondershare.drfone')
driver.find_element(By.ID, 'todo')#切换到h5页面之后的操作同selelnium
driver.switch_to.context('NATIVE_APP')#切换会app
driver.find_element_by_class_name('android.widget.ImageButton').click()
- 截图操作
driver.save_screenshot('login.png')
driver.get_screenshot_as_file('./image.png')
- 上下两点左右滑动
def get_size():
x=driver.get_window_size()
y=driver.get_window_size()
return x,y
l=get_size()
print(l)
def swipeLeft():
l=get_size()
x1=int(l[0]*0.9)
y1=int(l[1]*0.5)
x2=int(l[0]*0.1)
driver.swipe(x1, y1, x2, y1, 1000)
def swipUp():
l=get_size()
x1=int(l[0]*0.5)
y1=int(l[1]*0.9)
y2=int(l[1]*0.3)
driver.swipe(x1,y1,x1,y2,1000)
def swipeDown():
l=get_size()
x1=int(l[0]*0.5)
y1=int(l[1]*0.3)
y2=int(l[1]*0.9)
driver.swipe(x1,y1,x1,y2,1000)
def swipRight():
l=get_size()
x1=int(l[0]*0.3)
x2=int(l[0]*0.9)
y1=int(l[1]*0.5)
driver.swipe(x1,y1,x2,y2,1000)
- 上下左右拖动
ele=TouchAction(driver).press(x=262,y=385).wait(2000)
ele.move_to(x=451,y=388).wait(1000)
ele.move_to('todo').wait(1000)
ele.move_to('todo').wait(1000)
ele.release().perform()
- 地图缩放
def pinch():
action1=TouchAction(driver)
action2=TouchAction(driver)
pinch_action=MultiAction(driver)
action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).wait(1000).release()
action2.press(x=x*0.8,y=y*0.8).wait(1000).move_to(x=x*0.6,y=y*0.6).wait(1000).release()
pinch_action.add(action1,action2)
print('start pinch...')
pinch_action.perform()
def zoom():
action1=TouchAction(driver)
action2=TouchAction(driver)
zoom_action=MultiAction(driver)
action1.press(x=x*0.2,y=y*0.2).wait(1000).move_to(x=x*0.4,y=y*0.4).wait(1000).release()
action2.press(x=x*0.8,y=y*0.8).wait(1000).move_to(x=x*0.6,y=y*0.6).wait(1000).release()
zoom_action.add(action1,action2)
print('start zoom...')
zoom_action.perform()
本文详细介绍了使用Appium进行自动化测试的基本操作,包括连接模拟器、元素定位(如ID、class、xpath、list、relative和uiautomator)、toast元素处理、H5页面及网站交互、截图功能以及滑动和拖动操作,为Appium测试提供了全面的操作方法。
315

被折叠的 条评论
为什么被折叠?



