目录
1.1滑动操作
Appium通过swipe函数处理滑动问题,代码如下:
public void swipe(int startx,int starty,int endx,int endy,int duration){
TouchAction touchAction=new TouchAction(this);
touchAction.press(startx,starty).waitAction(duration).moveTo(endx,endy).release();
touchAction.perform();
}
1.2滚动操作
使用findElementByAndroidUIAutomator()方法来实现同样的功能
public void scroolToElement(String str){
((AndroidDriver<MobileElement>)driver).findElementByAndroidUIAutomator("
new UiScrollable(new UiScrollable(new UiSelector().scrollable(true).instance(0).ScrollIntiView(new UiScrollable().textContains(\""+str+"\").instance(0)) ))")
}
1.3图片截屏处理
File screfile=driver.getScreenshotAs(OutputType.File);
FileUtils.copyFile(screfile,new File("路径"));
1.4长按处理
TouchAction touchAction=new TouchAction(driver);
touchAction.longPress(weblement元素).perform();
1.5下拉列表
List<MobileElement>checkedText=(List<MobileElement>)driver.findElementsByClassNmae("");
for(MobileElement check:checkedText){
if(check.getAttribute("name").equals("text内容")){
if(check.isSelected()){
check.click();
}
}
}
1.6处理缩放操作
Public void Zoom(int x,int y){
MultiTouchAction multitouch=new MultiTouchAction(driver);
int screnHeight=driver.manage().window().getSize().getHeight();
int Yoffset=100;
if(y-100<0){
Yoffset=y;
}else if(y+100 >screnHeight){
Yoffset=screnHeight-y;
}
TouchAction touchAction0=new TouchAction(driver).press(x,y).moveTo(x,y-Yoffset).release();
TouchAction touchAction1=new TouchAction(driver).press(x,y).moveTo(x,y+Yoffset).release();
multitouch.add(touchAction0.touchAction1);
multitouch.perform();
}
1.7启动其他APP
driver.startActivity(new Activity(andriodPackage,andriodActivity));
1.8处理拖动
MobileElement startElement=(MobileElement)driver.findElement(By);
MobileElement endElement=(MobileElement)driver.findElement(By);
TouchAction touchAction=new TouchAction(driver);
touchAction.press(startElement).perform();
touchAction.moveTo(endElement).release().perform();
code:
public class AppiumUtils {
// 滑动操作 向上滑动
public void swipeToUp(AppiumDriver<WebElement> driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
int startx = width / 4;
int starty = 3 * height / 4;
int endx = startx;
int endy = height / 4;
@SuppressWarnings("rawtypes")
TouchAction touchAction = new TouchAction(driver);
touchAction.press(startx, starty).waitAction(Duration.ofSeconds(during)).moveTo(endx, endy).release().perform();
}
// 向下滑动
public void swipeToDown(AppiumDriver<WebElement> driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
int startx = width / 4;
int starty = height / 4;
int endx = startx;
int endy = 3 * height / 4;
@SuppressWarnings("rawtypes")
TouchAction touchAction = new TouchAction(driver);
touchAction.press(startx, starty).moveTo(endx, endy).release().perform();
}
// 向右滑
public void swipeToRight(AppiumDriver<WebElement> driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
int startx = width / 4;
int starty = height / 3;
int endx = 3 * width / 4;
int endy = height / 3;
@SuppressWarnings("rawtypes")
TouchAction touchAction = new TouchAction(driver);
touchAction.press(startx, starty).moveTo(endx, endy).release().perform();
}
// 向左滑
public void swipeToLeft(AppiumDriver<WebElement> driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
int startx = 3 * width / 4;
int starty = height / 3;
int endx = width / 4;
int endy = height / 3;
@SuppressWarnings("rawtypes")
TouchAction touchAction = new TouchAction(driver);
touchAction.press(startx, starty).moveTo(endx, endy).release().perform();
}
// 点击 按钮 release() 结束的行动取消屏幕上的指针。
public void Press(AppiumDriver<WebElement> driver, int x, int y) {
new TouchAction(driver).press(x, y).release().perform();
}
// 长按
public void longPress(AppiumDriver<WebElement> driver , WebElement webElement) {
new TouchAction(driver).longPress(webElement).perform().release();
}
//点击按钮控件
public void tap(AppiumDriver<WebElement> driver, WebElement webElement) {
new TouchAction(driver).tap(webElement).perform().release();
}
//暂停脚本执行 单位毫秒
public void wait(AppiumDriver<WebElement> driver, int duration) {
try {
driver.wait(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//滚动操作
public void scroolToElement(AppiumDriver<WebElement> driver,String str){
((FindsByAndroidUIAutomator<WebElement>) driver).findElementByAndroidUIAutomator(
"new UiScrollable(new UiScrollable(new UiSelector().scrollable(true).instance(0).ScrollIntiView("
+ "new UiScrollable().textContains(\""+str+"\").instance(0)) ))");
}
//截图操作
public void takeShot(AppiumDriver<WebElement> driver,String filePath ) {
File screfile=driver.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screfile,new File(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//处理拖动
public void drag(MobileElement startElement,MobileElement endElement,AppiumDriver<WebElement> driver) {
TouchAction touchAction=new TouchAction(driver);
touchAction.press(startElement).perform();
touchAction.moveTo(endElement).release().perform();
}
//放大处理
public void Zoomout(AppiumDriver<WebElement> driver,int x,int y){
MultiTouchAction multitouch=new MultiTouchAction(driver);
int screnHeight=driver.manage().window().getSize().getHeight();
int Yoffset=100;
if(y-100<0){
Yoffset=y;
}else if(y+100 >screnHeight){
Yoffset=screnHeight-y;
}
TouchAction touchAction0=new TouchAction(driver).press(x,y).moveTo(x,y-Yoffset).release();
TouchAction touchAction1=new TouchAction(driver).press(x,y).moveTo(x,y+Yoffset).release();
multitouch.add(touchAction0);
multitouch.add(touchAction1);
multitouch.perform();
}
}