Appium常用元素定位方式
可以通过id定位元素
el1 = driver.find_element_by_id("com.android.settings:id/title")
可以通过xpath定位
el1=driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.view.View/android.widget.FrameLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.TextView")
可以通过class
appium元素事件操作API
swipe滑动事件
swipe(start_x,start_y,end_x,end_y,duration=None)
driver.swipe(96,720,96,166,1000)
scroll滑动事件
el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
el3 = driver.find_element_by_xpath("//*[contains(@text,'应用')]")
driver.scroll(el3,el2)
drag拖拽事件
el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
el3 = driver.find_element_by_xpath("//*[contains(@text,'应用')]") driver.drag_and_drop(el3,el2)
手指轻敲操作
el2 = driver.find_element_by_xpath("//[contains(@text,'应用')]")
TouchAction(driver).tap(el2).perform()
按压press
每一个press之后都要一个releasw()和Perform
开始按压一个元素或坐标点(x,y)。通过手指按压手机屏幕的某个位置。
press(WebElement el, int x, int y)
比如TouchAction(driver).press(x=0,y=308).release().perform()
release() 结束的行动取消屏幕上的指针。释放,相当于松手
Perform() 执行的操作发送到服务器的命令操作。
长按控件
longPress(WebElement el, int x, int y, Duration duration)
开始按压一个元素或坐标点(x,y)。 相比press()方法,longPress()多了一个入参,既然长按,得有按的时间吧。duration以毫秒为单位。1000表示按一秒钟。其用法与press()方法相同。
TouchAction action = new TouchAction(driver);
action.longPress(names.get( 200),1000).perform().release();
action.longPress( 200 ,200,1000).perform().release();
点击控件tap
tap(WebElement el, int x, int y)
TouchAction action = new TouchAction(driver);
action.tap(names.get(200)).perform().release();
action.tap( 200,200).perform().release();
移动 move_to
moveTo(WebElement el, int x, int y) TouchAction action = new
TouchAction(driver);action.moveTo(names.get(200)).perform().release();
action.moveTo( 200,200).perform().release();
等待wait()
action.wait(1000); 单位为毫秒
练习
设置安全的屏幕锁定图案
从WLAN滑动到应用
el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
el3 = driver.find_element_by_xpath("//*[contains(@text,'应用')]")
driver.scroll(el3,el2)
点击安全
el4 = driver.find_element_by_xpath("//*[contains(@text,'安全')]")
el4.click()
点击屏幕锁定方式
el5 = driver.find_element_by_xpath("//*[contains(@text,'屏幕锁定方式')]")
el5.click()
设置图案
TouchAction(driver).press(x=88,y=366).wait(100).move_to(x=265,y=366).wait(100).move_to(x=450,y=520).wait(100).move_to(x=450,y=700).release().perform()