appium基本操作方法

本文详细介绍了使用Appium进行自动化测试的基本操作,包括连接模拟器、元素定位(如ID、class、xpath、list、relative和uiautomator)、toast元素处理、H5页面及网站交互、截图功能以及滑动和拖动操作,为Appium测试提供了全面的操作方法。
  • 连接模拟器操作
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()
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小静砸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值