1. 程式人生 > >Appium&Java實現自動化實現移動端幾種典型動作

Appium&Java實現自動化實現移動端幾種典型動作

複製程式碼
/*
 * @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;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Test_LongPress {
    AndroidDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(AndroidMobileCapabilityType.VERSION, "4.4");
        caps.setCapability(AndroidMobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as Real Device
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.android.dialer");
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.android.dialer.DialtactsActivity");
        driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @Test
    public void testExample(){
        WebElement dialPad=driver.findElementByAccessibilityId("dial pad");
        dialPad.click();
        TouchAction tAction=new TouchAction(driver);
        tAction.longPress(driver.findElement(By.name("0"))).perform();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}
複製程式碼
三、Appium4.0 Scroll
複製程式碼
/*
 * @FileName Test_Scroll: the script of testing Scroll
 * @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;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Test_Scroll_Swipe {
    AndroidDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.google.android.launcher");
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.google.android.launcher.StubApp");
        driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @Test
    public void testExample(){
        TouchAction tAction=new TouchAction(driver);
        // Frist tap on the screen and swipe it using moveTo Function
        tAction.press(300,500).moveTo(600,500).release().perform();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}
複製程式碼
四、Appium4.0 Drag&Drop
複製程式碼
/*
 * @FileName Test_Drag&Drop: the script of testing drag&drop
 * @author davieyang
 * @create 2018-11-22 10:04
 */
package testscript;

import io.appium.java_client.MobileElement;
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;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Test_Drag_Drop {
    AndroidDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.google.android.launcher");
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.google.android.launcher.StubApp");
        driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @Test
    public void testExample(){
        MobileElement appsIcon=(MobileElement)driver.findElementByAccessibilityId("Apps");
        appsIcon.click();
        MobileElement calculator=(MobileElement)driver.findElementByName("Calculator");
        TouchAction act = new TouchAction(driver);
        act.press(calculator).perform();
        act.moveTo(driver.findElement(By.name("App info"))).release().perform();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}
複製程式碼
五、Appium4.0 Slide Seekbar
複製程式碼
/*
 * @FileName Test_Slide_Seekbar:
 * @author davieyang
 * @create 2018-11-22 14:12
 */
package testscript;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
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.By;
import org.openqa.www.mhylpt.com/   selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.anno www.wanchuang178.cn  tations.AfterClass;
import org.testng.www.mhylpt.com/   annotations.BeforeClass;
import org.testng.annotations.Test;

public class Test_Slide_Seekbar {
    AndroidDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        File app=new File("C:\\mobileapp\\AndroidUI.apk");//You can change it with your app address
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.APP,app);
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
        caps.setCapability("appPackage","com.android.androidui"); // This is package name of your app (you can get it from apk info app)
        caps.setCapability("appActivity","com.android.androidui.MainActivity");
        driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @Test
    public void testExample(){
        WebElement slider=driver.findElementById("com.android.androidui:id/seekBar1");
        int xAxisStartPoint = slider.getLocation().getX();
        int xAxisEndPoint = xAxisStartPoint + slider.getSize().getWidth();
        int yAxis = slider.getLocation().getY();
        TouchAction act=new TouchAction(driver);
        act.press(xAxisStartPoint,yAxis).moveTo(xAxisEndPoint-1,yAxis).release().perform();
        //pressed x axis & y axis of seekbar and move seekbar till the end
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}
複製程式碼
六、Appium4.0 Spinner
複製程式碼
/*
 * @FileName Test_Spinners: the script of testing Spinners
 * @author davieyang
 * @create 2018-11-22 10:04
 */
package testscript;
import io.appium.java_client.android.AndroidDriver;
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.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Test_Spinners {
    public AndroidDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        File app=new File("C:\\mobileapp\\AndroidUI.apk");//You can change it with your app address
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.APP,app);
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as a Real Device
        caps.setCapability("appPackage","com.android.androidui"); // This is package name of your app (you can get it from apk info app)
        caps.setCapability("appActivity","com.android.androidui.MainActivity");
        driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @Test
    public void testExample(){
        WebElement spinner=driver.findElement(By.id("android:id/text1"));
        spinner.click();

        driver.scrollToExact("India");
        WebElement optionIndia=driver.findElement(By.name("India"));
        optionIndia.click();
    }
    @AfterClass
    public void tearDown(){