用java和junit编写app自动化测试用例

Posted #天羽Owl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java和junit编写app自动化测试用例相关的知识,希望对你有一定的参考价值。

package myTest;

import static org.junit.Assert.*;

import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities; 
 
import java.io.File;  
import java.net.URL;  
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MyTestCase {
    private AndroidDriver driver;

    @Before
    public void setUp() throws Exception {
        //设置apk的路径
        File classpathRoot = new File(System.getProperty("user.dir"));
        File appDir = new File(classpathRoot, "apps");
        File app = new File(appDir, "calculator_debug_2.3.1_10000_svn80220_200_staging_2017_12_16.apk");
        
        //设置自动化相关参数
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "7N2SSE158P001892");
        capabilities.setCapability("deviceName", "P4M7N15104001008");
                 
        //设置安卓系统版本
        capabilities.setCapability("platformVersion", "4.4.2");
        capabilities.setCapability("platformVersion", "6.0");
        //设置apk路径
        capabilities.setCapability("app", app.getAbsolutePath()); 
                
        //设置app的主包名和主类名
        capabilities.setCapability("appPackage", "com.gomo.calculator");
        capabilities.setCapability("appActivity", ".ui.activity.MainActivity");
                 
        //初始化
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
//        driver.closeApp();
//        driver.launchApp();
    }
    
    @Test
    public void test_Add() {
        driver.findElement(By.id("com.gomo.calculator:id/input_num_1")).click();
        driver.findElement(By.id("com.gomo.calculator:id/display_op_add")).click();
        driver.findElement(By.id("com.gomo.calculator:id/input_num_2")).click();
        driver.findElement(By.id("com.gomo.calculator:id/display_eq")).click();
        System.out.println("test_Add success!!!");
        
    }
    
    @Test
    public void test_Mul() {
        driver.findElement(By.id("com.gomo.calculator:id/input_num_3")).click();
        driver.findElement(By.id("com.gomo.calculator:id/op_mul")).click();
        driver.findElement(By.id("com.gomo.calculator:id/input_num_4")).click();
        driver.findElement(By.id("com.gomo.calculator:id/display_eq")).click();
        System.out.println("test_Mul success!!!");
        
    }

}

 

python版请移步:用python和unittest编写app自动化测试用例

以上是关于用java和junit编写app自动化测试用例的主要内容,如果未能解决你的问题,请参考以下文章

如何使用mockito和junit为Java中的ExecutorService编写测试用例?

Java 单元测试及JUnit的使用

在Maven项目中运行JUnit 5测试用例

selenium 怎么使用java编写测试用例

安卓ios app自动化测试用例模板

Selenium 中的 JUnit 注解