Appium+Java笔记(2) 截图,移动控件,双指放大图片

本文记录了使用Java Appium进行自动化测试的过程,包括酷狗App中拖动元素和相机应用中双指放大图片的场景。在尝试Java-client-4.1.2和selenium3.0版本下,通过driver.pinch()和driver.zoom()方法未能成功实现图片放大,最终通过调整等待时间至2000ms实现了双指放大功能。同时介绍了webdriver.getScreenshotAs(OutputType.FILE)用于获取截图。

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

之前用Python试验过的,相同的测试方法使用Java试验一遍,记录记录。

1.酷狗“听”页面拖动“乐库”和“歌单”换位置,动作前后截图

2.打开相机,浏览并放大图片

运行环境 Java1.8 ; Java-client-4.1.2 ; selemium3.0 ; Appium-linux-1.12.1.AppImage

Java中driver.pinch(WebElement l), driver.zoom(WebElement l)可以调用,但是实际使用没实现放大缩小照片的效果。

参考:https://www.cnblogs.com/longronglang/p/10318109.html

            https://www.cnblogs.com/ali-test/p/11221594.html

关于模拟双指拖动放大图片,没按人家的等待时间来,试验时设置模拟点击后等待0.25秒就开始移动,结果只能从移动轨迹上看出确实往两侧拖动了,但就是没放大的效果,明明类似的方法,Python设置250ms后移动就行的,Java这折腾了半天,改成跟人家一模一样的等待2000ms,成功了!( ⊙ o ⊙ )

截图方法关键也是一句 webdriver.getScreenshotAs(OutputType.FILE),File形式,后面需要copy出去。

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

import static junit.framework.TestCase.fail;

public class TestKuGouDemo{
    public static AndroidDriver<?> appiumDriver;

    @Before
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName","Android");
        desiredCapabilities.setCapability("platformVersion","5.1.1");
        desiredCapabilities.setCapability("deviceName","G0B0ME036482001L");
        desiredCapabilities.setCapability("appPackage","com.kugou.android");
        desiredCapabilities.setCapability("appActivity",".app.splash.SplashActivity");
        desiredCapabilities.setCapability("newCommandTimeout","10");//允许无操作的时间
        desiredCapabilities.setCapability("noReset","true");
        appiumDriver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),desiredCapabilities);
        //隐式等待
        appiumDriver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);

        //点击“跳过”
        //WebElement skip=appiumDriver.findElementsByAndroidUIAutomator("new UiSelector().className(\"android.view.View\")").get(1);
        WebElement skip=appiumDriver.findElement(By.xpath("//android.view.View[@index='1']"));
        skip.click();
        //点击左上角的“X”,不注册
        //WebElement XRegister = appiumDriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.kugou.android:id/uv\")");
        WebElement XRegister = appiumDriver.findElementById("com.kugou.android:id/uv");
        XRegister.click();
    }
    @Test
    public void test_drag() throws IOException{
        /**
         * 显示等待
        WebDriverWait expectedWait =new WebDriverWait(appiumDriver, 10);
        WebElement el= expectedWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.kugou.android:anim/dr")));
        //流畅等待
        FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(appiumDriver)
                .withTimeout(10,TimeUnit.SECONDS)
                .pollingEvery(250,TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class)
                .ignoring(TimeoutException.class);
        WebElement ell=fluentWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.kugou.android:anim/dr")));
         */
        WebElement musicBox = appiumDriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.kugou.android:anim/dr\")");
        CaptureScreenShot();
        try{
            TouchAction touchAction = new TouchAction(appiumDriver);
            touchAction.longPress(musicBox);
            touchAction.moveTo(appiumDriver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.kugou.android:anim/ds\")")).release().perform();
        }catch(Exception e){
            e.printStackTrace();
        }
        CaptureScreenShot();
    }
    @Test
    public void test_zoom() throws InterruptedException{
        appiumDriver.startActivity("com.amazon.camera", ".AmazonCameraActivity");//小米:"com.android.camera", ".Camera"
        appiumDriver.findElementById("com.amazon.camera:id/thumbnail").click();//小米:com.android.camera:id/v9_thumbnail_image
        WebElement imageView = appiumDriver.findElementById("com.amazon.photos:id/photo_view");//:小米com.miui.gallery:id/photoview
        //appiumDriver.zoom(imageView);
        int srcHeight = appiumDriver.manage().window().getSize().getHeight();
        int srcWidth = appiumDriver.manage().window().getSize().getWidth();
        /**
        //双击放大
        TouchAction t0 = new TouchAction(appiumDriver);
        t0.tap(srcWidth/2,srcHeight/2).waitAction(100).tap(srcWidth/2+10,srcHeight/2-10);
        t0.perform();
         */
        MultiTouchAction multiTouchAction = new MultiTouchAction(appiumDriver);
        TouchAction t1 = new TouchAction(appiumDriver);
        TouchAction t2 = new TouchAction(appiumDriver);
        t1.press(srcWidth/2,srcHeight/2 - 100).waitAction(2000).moveTo(srcWidth/2,srcHeight/2 - 300).release();
        t2.press(srcWidth/2,srcHeight/2 + 100).waitAction(2000).moveTo(srcWidth/2,srcHeight/2 + 300).release();
        multiTouchAction.add(t1).add(t2);
        multiTouchAction.perform();

    }
    @After
    public void tear_down(){
        appiumDriver.closeApp();
        appiumDriver.quit();
    }
    //截图方法
    public void CaptureScreenShot() throws IOException {
        File srcfile=appiumDriver.getScreenshotAs(OutputType.FILE);//截图
        File location = new File("/home/derik/图片");//存放的电脑路径上
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        String screenShotName = location.getAbsolutePath()+File.separator+dateFormat.format(new Date())+"testKuGou.png";
        FileUtils.copyFile(srcfile,new File(screenShotName));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值