如何捏 - 放大并缩小在Appium中的Android应用程序中打开的图像

时间:2016-10-04 12:50:28

标签: android automation appium

这是测试场景我试图在应用程序上自动化但是卡住了,

  • 打开应用程序&转到照片部分(工作)
  • 点击照片查看(工作)
  • 要确认照片已加载,我想放大并缩小(不工作)

Appium版本:1.4.16.1 API等级19

以下是代码段:

@Test
 public void openph() throws InterruptedException {
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
        driver.findElement(By.xpath("//android.widget.ImageButton[@content-desc='Navigate up']")).click();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
        driver.findElement(By.xpath("//android.widget.RelativeLayout[@index='3']")).click();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
        driver.findElement(By.xpath("//android.widget.ImageView[@index='1']")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//android.widget.LinearLayout[@index='2']")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//android.widget.CheckedTextView[@text='Photos only']")).click();
        List<WebElement> ops = driver.findElements(By.xpath("//android.widget.ImageView"));
        ops.get(2).click();
        driver1.zoom(25, 45);
        driver1.pinch(25, 45);
        driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);

 }

任何人都可以帮我解决这个问题。

目前它正在抛出nullpointer异常

2 个答案:

答案 0 :(得分:0)

可能是因为你有两个不同的驱动程序(driver and driver1)? Appium目前一次可以在一个会话上运行一个驱动程序。

另外,我不知道你在用这些线路做什么: driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

你只需要为每个驱动程序启动一次(它也不是很好,因为它会大大减慢你的测试速度,最好使用WebDriverWait(Driver, <defined wait time>

答案 1 :(得分:0)

 //Use this below the code it work fine.
 import io.appium.java_client.android.AndroidDriver;

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

 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.remote.DesiredCapabilities;

 public class Zoom_and_pinch {

public static void main(String[] args) throws InterruptedException, MalformedURLException {
    // TODO Auto-generated method stub

    File app= new  File("D:\\Testing\\com.davemorrissey.labs.subscaleview.sample.apk");
    //Launch app
            DesiredCapabilities capabilities= new DesiredCapabilities();

            //device details
            capabilities.setCapability("deviceName", "GT-I9300I");
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("platformVersion", "4.4.4");

            //app details
            capabilities.setCapability("app",app.getAbsolutePath());


            //appium server details
            AndroidDriver driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

            //wait
            Thread.sleep(4000);

            driver.findElementById("com.davemorrissey.labs.subscaleview.sample:id/basicFeatures").click();
            Thread.sleep(4000);

            WebElement ele_image = driver.findElementById("com.davemorrissey.labs.subscaleview.sample:id/imageView");


            int x1= ele_image.getLocation().getX();
            int y1= ele_image.getLocation().getY();

            System.out.println("x is "+x1+"y1 is "+y1);

            int x=ele_image.getLocation().getX()+ele_image.getSize().getWidth()/2;
            int y= ele_image.getLocation().getY()+ele_image.getSize().getHeight()/2;

            //Zoom
            TouchAction finger1= new TouchAction(driver);
            finger1.press(ele_image, x, y-20).moveTo(ele_image, x, y-200);

            TouchAction finger2= new TouchAction(driver);
            finger2.press(ele_image, x, y+20).moveTo(ele_image, x, y+200);

            MultiTouchAction action= new MultiTouchAction(driver);
            action.add(finger1).add(finger2).perform();

            Thread.sleep(8000);

            //Pinch
            TouchAction finger3= new TouchAction(driver);
            finger3.press(ele_image, x, y-200).moveTo(ele_image, x, y-20);

            TouchAction finger4= new TouchAction(driver);
            finger4.press(ele_image, x, y+200).moveTo(ele_image, x, y+20);

            MultiTouchAction action2= new MultiTouchAction(driver);
            action2.add(finger3).add(finger4).perform();

   }

 }