Appium&Java自动化实现移动端几种典型动作

admin 2018-12-04 阅读


一、Appium4.0 Pinch&Zoom

/*
 * @FileName Pinch_Zoom: Pinch_Zoom
 * @author davieyang
 * @create 2018-11-22 10:04
 */
package testscript;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Test_Pinch_Zoom {
    IOSDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        File app=new File("/Users/mhans/appium/ios/Zoom.app");//You can change it with your app address
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.APP,app);
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"8.1");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad");
        caps.setCapability("udid","Real Device Id");
        driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @Test
    public void testExample(){
        int scrHeight = driver.manage().window().getSize().getHeight();
        int scrWidth = driver.manage().window().getSize().getWidth();
        MultiTouchAction multiTouch = new MultiTouchAction(driver);
        TouchAction tAction0 = new TouchAction(driver);
        TouchAction tAction1 = new TouchAction(driver);
        tAction0.press(scrWidth/2, scrHeight >> 1).waitAction(1000).moveTo(0,60).release();
        tAction1.press(scrWidth/2,scrHeight/2+40).waitAction(1000).moveTo(0,80).release();
        multiTouch.add(tAction0).add(tAction1);
        multiTouch.perform();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}

二、Appium4.0 Long Press Action

/*
 * @FileName Test_LongPress: the script of testing long press
 * @author davieyang
 * @create 2018-11-22 10:04
 */
package testscript;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;