Appium自动化测试第六篇常见问题处理方式

本文介绍了如何使用Appium进行移动应用的自动化测试,包括滑动、滚动、截图、长按等基本操作,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.1滑动操作

1.2滚动操作

1.3图片截屏处理

1.4长按处理

1.5下拉列表

1.6处理缩放操作

1.7启动其他APP

1.8处理拖动


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();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员路同学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值