• Java + Selenium + Appium自动化测试


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

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

    三、安装应用到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、编写基本代码如下仅供参考:

    1. package appium_demo;
    2. import java.net.MalformedURLException;
    3. import java.net.URL;
    4. import java.util.concurrent.TimeUnit;
    5. import org.openqa.selenium.By;
    6. import org.openqa.selenium.remote.DesiredCapabilities;
    7. import io.appium.java_client.android.AndroidDriver;
    8. /** * @author 李小卫 E-mail:yyymlxw@163.com @date 创建时间2018211日上午10:10:02 */
    9. public class calc_demo {
    10. public static void main(String[] args) throws MalformedURLException {
    11. AndroidDriver driver;
    12. DesiredCapabilities des = new DesiredCapabilities();
    13. // des.setCapability("automationName", "Appium");//Selendroid //自动化的模式选择
    14. // des.setCapability("app", "C:\\software\\CalcTest.apk");//配置待测试的apk的路径
    15. // des.setCapability("browserName", "chrome"); //h5
    16. des.setCapability("platformName", "Android");//平台名称
    17. des.setCapability("platformVersion", "4.4");//手机操作系统版本
    18. des.setCapability("udid", "192.168.229.101:5555");//连接的物理设备的唯一设备标识
    19. des.setCapability("deviceName", "S4");//使用的手机类型或模拟器类型 UDID
    20. des.setCapability("appPackage", "com.sky.jisuanji");//App安装后的包名,注意与原来的CalcTest.apk不一样
    21. des.setCapability("appActivity", ".JisuanjizixieActivity");//app测试人员常常要获取activity,进行相关测试,后续会讲到
    22. des.setCapability("unicodeKeyboard", "True");//支持中文输入
    23. des.setCapability("resetKeyboard", "True");//支持中文输入
    24. des.setCapability("newCommandTimeout", "10");//没有新命令时的超时时间设置
    25. des.setCapability("nosign", "True");//跳过检查和对应用进行 debug 签名的步骤
    26. driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), des);//虚拟机默认地址
    27. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置超时等待时间,默认250ms
    28. driver.findElement(By.id("com.android.calculator2:id/digit1")).click();//定位'1'
    29. driver.findElement(By.id("com.android.calculator2:id/plus")).click();//定位'+'
    30. driver.findElement(By.id("com.android.calculator2:id/digit6")).click();//定位'6'
    31. driver.findElement(By.id("com.android.calculator2:id/equal")).click();//定位'='
    32. }
    33. }   

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

    1. package appium_operate;
    2. import java.net.MalformedURLException;
    3. import java.net.URL;
    4. import java.util.concurrent.TimeUnit;
    5. import org.openqa.selenium.By;
    6. import org.openqa.selenium.remote.DesiredCapabilities;
    7. import org.testng.Assert;
    8. import org.testng.annotations.BeforeTest;
    9. import org.testng.annotations.DataProvider;
    10. import org.testng.annotations.Test;
    11. import io.appium.java_client.android.AndroidDriver;
    12. /** * @author 李小卫 E-mail:yyymlxw@163.com @date 创建时间2018211日上午10:30:02 */
    13. public class CalcTest {
    14. AndroidDriver driver;
    15. @BeforeTest
    16. public void setUp() throws MalformedURLException{
    17. DesiredCapabilities des = new DesiredCapabilities();
    18. // des.setCapability("app", "c:\\");
    19. des.setCapability("platformName", "Android");
    20. des.setCapability("platformVersion", "4.4");
    21. des.setCapability("udid", "192.168.43.101:5555");
    22. des.setCapability("deviceName", "s4");
    23. des.setCapability("appPackage", "com.android.calculator2");//com.android.contacts
    24. des.setCapability("appActivity", ".Calculator");//.activities.PeopleActivity
    25. des.setCapability("unicodeKeyboard", "True");
    26. des.setCapability("resetKeyboard", "True");
    27. des.setCapability("newCommandTimeout", "15");
    28. des.setCapability("nosign", "True");
    29. driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),des);
    30. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    31. }
    32. @Test(enabled = false)
    33. public void add() {
    34. driver.findElement(By.xpath("//android.widget.Button[@text='5']")).click();
    35. driver.findElement(By.xpath("//android.widget.Button[@text='+']")).click();
    36. driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click();
    37. driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();
    38. String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");
    39. Assert.assertEquals(value, "13");
    40. }
    41. @Test(enabled = false)
    42. public void sub() {
    43. driver.findElement(By.xpath("//android.widget.Button[@text='1']")).click();
    44. driver.findElement(By.xpath("//android.widget.Button[@text='0']")).click();
    45. driver.findElement(By.xpath("//android.widget.Button[@text='-']")).click();
    46. driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click();
    47. driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();
    48. String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");
    49. Assert.assertEquals(value, "2");
    50. }
    51. @Test(enabled = false)
    52. public void mul() {
    53. driver.findElement(By.xpath("//android.widget.Button[@text='5']")).click();
    54. driver.findElement(By.xpath("//android.widget.Button[@text='×']")).click();
    55. driver.findElement(By.xpath("//android.widget.Button[@text='8']")).click();
    56. driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();
    57. String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");
    58. Assert.assertEquals(value, "40");
    59. }
    60. @DataProvider(name="testdata")
    61. public Object[][] getData(){
    62. return new Object[][]{{"20","80","100","+"},{"90","3","270","×"},{"6","2","3","÷"}};
    63. }
    64. @Test(dataProvider = "testdata")
    65. public void calcTestcase(String num1,String num2,String result,String calcType){
    66. for(char num:num1.toCharArray()){
    67. driver.findElement(By.xpath("//android.widget.Button[@text='"+String.valueOf(num)+"']")).click();
    68. }
    69. driver.findElement(By.xpath("//android.widget.Button[@text='"+calcType+"']")).click();
    70. for(char num:num2.toCharArray()){
    71. driver.findElement(By.xpath("//android.widget.Button[@text='"+String.valueOf(num)+"']")).click();
    72. }
    73. driver.findElement(By.xpath("//android.widget.Button[@text='=']")).click();
    74. String value = driver.findElement(By.xpath("//android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");
    75. Assert.assertEquals(value, result);
    76. }
    77. }

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

     

  • 相关阅读:
    [hadoop全分布部署]虚拟机Hadoop集群交换 SSH 密钥与验证SSh无密码登录
    极智Paper | YOLOS 通过目标检测重新思考Vision Transformer
    01-ZooKeeper快速入门
    数据结构——链表
    微前端qiankun嵌入vue项目后iconfont显示方块
    八股文面经整理
    Maven多模块快速升级超好用Idea插件-MPVP
    关于激光雷达传感器分类及简介
    阿里云99元ECS云服务器老用户也能买,续费同价!
    深入理解RunLoop
  • 原文地址:https://blog.csdn.net/qq_73332379/article/details/132977443