Java + Selenium + Appium自动化测试

一、启动测试机或者Android模拟器(Genymotion俗称世界上最快的模拟器,可自行百度安装)

二、启动Appium(Appium环境安装可自行百度)

三、安装应用到Genymotion上,如下图我安装一个计算机的小应用,包名为CalcTest.apk

安装步骤:(基于Android SDK已经配置好了环境变量,可自行百度)

1、Win + R

2、CMD

3、adb devices --检查操作,列出存在的设置名称

4、adb install F:\Appium\CalcTest.apk --正式安装App

测试apk下载地址:https://files.cnblogs.com/files/yyym/CalcTest.apk

如下图:192.168.229.101:5555就是我刚开启的Genymotion虚拟机

四、安装成功之后回到Genymotiong可以看到已经安装成功了

打开该应用,可以看到实际是个简单的计算器

五、打开Eclipse创建Maven项目并使用uiautomatorviewer工具(Android SDK工具包自带的)进行基本元素定位操作,元素定位方式前面我们已经详细讲解过了。

1、打开Android SDK可找到路径:android-sdks\tools如下(获取App包名可反编译:aapt dump badging apk路径)

2、打开uiautomatorviewr.bat

3、编写基本代码如下仅供参考:

复制代码
package appium_demo;

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 io.appium.java_client.android.AndroidDriver;

/** * @author 李小卫 E-mail:yyymlxw@163.com @date 创建时间2018年2月11日上午10:10:02 */

public class calc_demo {

    public static void main(String[] args) throws MalformedURLException {

        AndroidDriver driver;

        DesiredCapabilities des = new DesiredCapabilities();

  //    des.setCapability("automationName", "Appium");//Selendroid //自动化的模式选择

 //     des.setCapability("app", "C:\\software\\CalcTest.apk");//配置待测试的apk的路径

//      des.setCapability("browserName", "chrome");  //h5

        des.setCapability("platformName", "Android");//平台名称

        des.setCapability("platformVersion", "4.4");//手机操作系统版本

        des.setCapability("udid", "192.168.229.101:5555");//连接的物理设备的唯一设备标识

        des.setCapability("deviceName", "S4");//使用的手机类型或模拟器类型  UDID

         

        des.setCapability("appPackage", "com.sky.jisuanji");//App安装后的包名,注意与原来的CalcTest.apk不一样

        des.setCapability("appActivity", ".JisuanjizixieActivity");//app测试人员常常要获取activity,进行相关测试,后续会讲到

         

        des.setCapability("unicodeKeyboard", "True");//支持中文输入

        des.setCapability("resetKeyboard", "True");//支持中文输入

        des.setCapability("newCommandTimeout", "10");//没有新命令时的超时时间设置

        des.setCapability("nosign", "True");//跳过检查和对应用进行 debug 签名的步骤

         

        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), des);//虚拟机默认地址

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置超时等待时间,默认250ms

        driver.findElement(By.id("com.android.calculator2:id/digit1")).click();//定位'1'

        driver.findElement(By.id("com.android.calculator2:id/plus")).click();//定位'+'

        driver.findElement(By.id("com.android.calculator2:id/digit6")).click();//定位'6'

        driver.findElement(By.id("com.android.calculator2:id/equal")).click();//定位'='

    }

}   

六、使用TestNG编写正式测试用例并开始执行测试了

复制代码
package appium_operate;

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.Assert;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;

/** * @author 李小卫 E-mail:yyymlxw@163.com @date 创建时间2018年2月11日上午10:30:02 */

public class CalcTest {

    AndroidDriver driver;

    @BeforeTest

    public void setUp() throws MalformedURLException{

        DesiredCapabilities des = new DesiredCapabilities();

//      des.setCapability("app", "c:\\");

        des.setCapability("platformName", "Android");

        des.setCapability("platformVersion", "4.4");

        des.setCapability("udid", "192.168.43.101:5555");

        des.setCapability("deviceName", "s4");

        des.setCapability("appPackage", "com.android.calculator2");//com.android.contacts

        des.setCapability("appActivity", ".Calculator");//.activities.PeopleActivity

        des.setCapability("unicodeKeyboard", "True");

        des.setCapability("resetKeyboard", "True");

        des.setCapability("newCommandTimeout", "15");

        des.setCapability("nosign", "True");

        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),des);

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }

    @Test(enabled = false)

    public void add() {

        driver.findElement(By.xpath("//android.widget.Button[@text='5']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='+']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();

        String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");

        Assert.assertEquals(value, "13");      

    }

    @Test(enabled = false)

    public void sub() {

        driver.findElement(By.xpath("//android.widget.Button[@text='1']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='0']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='-']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();

        String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");

        Assert.assertEquals(value, "2");       

    }

    @Test(enabled = false)

    public void mul() {

        driver.findElement(By.xpath("//android.widget.Button[@text='5']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='×']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click();

        driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();

        String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");

        Assert.assertEquals(value, "40");      

    }

     

    @DataProvider(name="testdata")

    public Object[][] getData(){

        return new Object[][]{{"20","80","100","+"},{"90","3","270","×"},{"6","2","3","÷"}};

    }

     

    @Test(dataProvider = "testdata")

    public void calcTestcase(String num1,String num2,String result,String calcType){

        for(char num:num1.toCharArray()){

            driver.findElement(By.xpath("//android.widget.Button[@text='"+String.valueOf(num)+"']")).click();

        }

        driver.findElement(By.xpath("//android.widget.Button[@text='"+calcType+"']")).click();

        for(char num:num2.toCharArray()){

            driver.findElement(By.xpath("//android.widget.Button[@text='"+String.valueOf(num)+"']")).click();

        }

        driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();

        String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");

        Assert.assertEquals(value, result);    

    }

}

下面是配套学习资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

相关推荐
xy34531 小时前
谷歌Lighthouse安装指南(Chrome扩展程序可视化操作)
前端·chrome·测试工具·lighthouse
AngusKit1 小时前
AngusTester 是什么:AI 原生软件测试
自动化测试·人工智能·测试工具·性能测试·web测试·api测试·llm测试
xfilm埃利测量3 小时前
平面显示导电膜方阻精准表征:四探针助力靶材高效反向设计
功能测试·测试工具·平面·四探针·薄膜电阻测量
flower_drop11 小时前
基于 WebUSB 与 CDP:在浏览器端实现 Android 设备通信与无证书抓包实践
android·chrome·测试工具·adb·bug·edge浏览器
2601_9622982716 小时前
Python与Selenium结合的Web自动化测试全流程实践教程
自动化测试·python·selenium·web测试·pageobjectmodel
chuntian_tester18 小时前
AI自动化第3步【用例设计】
人工智能·测试工具·ai·自动化
广州文明机电有限公司20 小时前
LM‑2 实操|宽域氧传感器安装位置 + Free‑Air 自由空气校准完整流程
功能测试·测试工具
PhotonixBay20 小时前
共聚焦显微镜如何实现表面形貌测量
图像处理·测试工具·共聚焦显微镜·表面粗糙度·激光共聚焦显微镜·表面形貌
chuntian_tester20 小时前
AI自动化第1步【系统探索】
人工智能·测试工具·ai·自动化
2601_962298271 天前
【自动化测试】基于Selenium + Python的web自动化框架
自动化测试·python·selenium·测试用例·web框架