JavaApp自动化测试系列[v1.0.0][几种常见APP类型测试代码实例附源码]

2023-12-25 08:31:12

通常APP分为:原生APP、WebApp、混合型APP,其自动化测试代码稍有不同

Java结合TestNG单元测试框架,实现移动端IOS_WebApp测试【源码】

/*
 * @FileName Test_iOS_WebApp:
 * @author davieyang
 * @create 2018-11-22 14:20
 */
package testscript;
import io.appium.java_client.ios.IOSDriver;
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_iOS_WebApp {
    IOSDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad");
        caps.setCapability("udid","Real Device Identifier");
        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(){
        driver.get("https://www.google.com");
        WebElement searchBox=driver.findElement(By.name("q"));
        searchBox.sendKeys("Appium for mobile automation");
    }
    @AfterClass
    public void tearDown(){
        driver.quit();
    }
}

Java结合TestNG单元测试框架,实现移动端ios_hybridapp测试【源码】

/*
 * @FileName Test_iOS_HybridApp:
 * @author davieyang
 * @create 2018-11-22 14:18
 */
package testscript;
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.Set;
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_iOS_HybridApp {
    IOSDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        File app=new File("/Users/mhans/appium/ios/WebViewApp.app");
        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 Identifier");
        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(){
        WebElement editBox=driver.findElement(By.className("UIATextField"));
        editBox.sendKeys("https://www.google.com");
        WebElement goButton=driver.findElement(By.name("Go"));
        goButton.click();
        Set<String> contexts = driver.getContextHandles();
        for (String context : contexts) {
            System.out.println(context); //it will print NATIVE_APP \n WEBVIEW_com.example.testapp
        }
        driver.context((String) contexts.toArray()[1]);
        WebElement images=driver.findElement(By.linkText("Images"));
        images.click();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}

Java结合TestNG单元测试框架,实现移动端ios_nativeapp测试【源码】

/*
 * @FileName Test_iOS_NativeApp:
 * @author davieyang
 * @create 2018-11-22 14:19
 */
package testscript;
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.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_iOS_NativeApp {
    IOSDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        File app=new File("/Users/mhans/appium/ios/BmiCalc.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(){
        WebElement height=driver.findElement(By.xpath("(//UIATextField)[2]"));
        height.sendKeys("1.82");
        WebElement weight=driver.findElement(By.xpath("(//UIATextField)[4]"));
        weight.sendKeys("75");
        WebElement calculateBMI=driver.findElement(By.name("Calculate BMI"));
        calculateBMI.click();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}



Java结合TestNG单元测试框架,实现移动端andriod_webapp测试【源码】

/*
 * @FileName Test_Android_WebApp:
 * @author davieyang
 * @create 2018-11-22 14:17
 */
package testscript;
import io.appium.java_client.android.AndroidDriver;
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_Android_WebApp {
    AndroidDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");
        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(){
        driver.get("https://www.gmail.com");
        WebElement username=driver.findElement(By.name("Email"));
        username.sendKeys("test");
        WebElement password=driver.findElement(By.name("Passwd"));
        password.sendKeys("test");
        WebElement signIn=driver.findElement(By.name("signIn"));
        signIn.click(); }
    @AfterClass
    public void tearDown(){
        driver.quit();
    }
}


Java结合TestNG单元测试框架,实现移动端Android_HybridApp测试【源码】

/*
 * @FileName Test_Android_HybridApp:
 * @author davieyang
 * @create 2018-11-22 14:15
 */
package testscript;

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.Set;
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_Android_HybridApp {
    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");
        caps.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Appium");//Use Selendroid in case of <4.4 android version
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.example.hybridtestapp");
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.example.hybridtestapp.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(){
        Set<String> contexts = driver.getContextHandles();
        for (String context : contexts) {
            System.out.println(context); //it will print NATIVE_APP \n WEBVIEW_com.example.hybridtestapp
        }
        driver.context((String) contexts.toArray()[1]);
        WebElement firstName=driver.findElement(By.name("fname"));
        firstName.sendKeys("test");
        WebElement lastName=driver.findElement(By.name("lname"));
        lastName.sendKeys("test");
        WebElement age=driver.findElement(By.name("age"));
        age.sendKeys("26");
        WebElement username=driver.findElement(By.name("username"));
        username.sendKeys("appiumTester");
        WebElement password=driver.findElement(By.id("psw"));
        password.sendKeys("appium@123");
        WebElement registerButton=driver.findElement(By.id("register"));
        registerButton.click();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}

Java结合TestNG单元测试框架,实现移动端Android_NativeApp测试【源码】

/*
 * @FileName Test_Android_NativeApp:
 * @author davieyang
 * @create 2018-11-22 14:17
 */
package testscript;

import io.appium.java_client.android.AndroidDriver;
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_Android_NativeApp {
    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 Real Device  caps.setCapability(MobileCapabilityType.APP_PACKAGE,"com.android.dialer"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY,"com.android.dialer.DailtactsActivity");
        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();
        for(int n=0;n<10;n++){
            driver.findElement(By.name(""+n+"")).click();
        }
        WebElement dial=driver.findElementByAccessibilityId("dial");
        dial.click();
    }
    @AfterClass
    public void tearDown(){
        driver.closeApp();
    }
}


文章来源:https://blog.csdn.net/dawei_yang000000/article/details/135190469
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。