• Appium+Maven+TestNG(ReportNG)环境搭建(详细过程)


        最近群里经常有人会问到关于maven构建Appium测试项目以及使用testNG生成测试报告的问题,试着搭建了一下,下面是过程:

    jdk安装过程我这里就不说了

    一、下载eclipse,推荐下载Eclipse Luna java EE版本,自带maven插件,比较稳定。Eclipse Luna java EE版64位下载地址:http://ftp.yz.yamagata-u.ac.jp/pub/eclipse//technology/epp/downloads/release/luna/SR2/eclipse-jee-luna-SR2-win32-x86_64.zip

    二、下载Maven并配置环境变量

    从maven官网下载maven程序包:http://apache.opencas.org/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip,下载后解压,例如我的目录结构是:

    配置环境变量MAVEN_HOME,变量值为maven的根目录:D:Program Filesapache-maven-3.3.9,在PATH中添加bin的路径:D:Program Filesapache-maven-3.3.9in,

    三、配置settings.xml文件,打开maven conf目录下的settings.xml文件,配置maven本地仓库地址,我配置的是F: epository

     

    我还配置了镜像的地址,因为从默认的中央仓库下载依赖包太慢了,在settings.xml中加入下面的代码:

     1 <mirrors>
     2     <!-- mirror | Specifies a repository mirror site to use instead of a given
     3         repository. The repository that | this mirror serves has an ID that matches
     4         the mirrorOf element of this mirror. IDs are used | for inheritance and direct
     5         lookup purposes, and must be unique across the set of mirrors. | -->
     6     <mirror>
     7         <id>nexus-osc</id>
     8         <mirrorOf>central</mirrorOf>
     9         <name>Nexus osc</name>
    10         <url>http://maven.oschina.net/content/groups/public/</url>
    11     </mirror>
    12     <mirror>
    13         <id>nexus-osc-thirdparty</id>
    14         <mirrorOf>thirdparty</mirrorOf>
    15         <name>Nexus osc thirdparty</name>
    16         <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
    17     </mirror>
    18 </mirrors>
    mirrors

    推荐几个国内的镜像地址:

    http://maven.oschina.net/content/groups/public/

    http://maven.oschina.net/content/repositories/thirdparty/

    http://repo2.maven.org/maven2/

    如果在下载的时候还是觉得慢的话,请自行百度其他地址。

    打开Eclipse,在菜单选择:windows->Preference->Maven->User Settings,在右侧User Settings选择刚才的settings.xml文件:

    配置后,下面步骤在pom.xml配置的依赖包就会从远程仓库下载到本地配置的仓库:F: epository

    四、安装Eclipse testNG插件,在菜单栏选择help->Eclipse Maketplace,搜索testng,安装,重启Eclipse

    五、开始创建Maven工程,选择Archetype,输入Group Id、Artifact Id,finish创建完成,如下图:

     

    创建完成后是这样的:

     

    六、配置依赖包,Appium需要三个包:appium client、selenium client、selenium server,如果我们不用maven构建工程,那么我们需要去下载这三个包,然后添加到我们的工程中,而maven不需要了,只需要配置依赖关系就可以了,我们可以在maven仓库中搜索我们需要的包,把他们的依赖关系写到pom.xml文件中:

    maven仓库地址:http://mvnrepository.com/,例如搜索appium,就可以搜索到appium java client的包,选择最新的包,把依赖关系粘贴打pom.xml中,selenium client和selenium server只需要配置一个selenium依赖就行了,配置后的pom.xml文件如下:

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3   <modelVersion>4.0.0</modelVersion>
     4 
     5   <groupId>com.zdx.appium</groupId>
     6   <artifactId>num1</artifactId>
     7   <version>0.0.1-SNAPSHOT</version>
     8   <packaging>jar</packaging>
     9 
    10   <name>num1</name>
    11   <url>http://maven.apache.org</url>
    12 
    13   <properties>
    14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    15   </properties>
    16 
    17   <dependencies>
    18     <dependency>
    19       <groupId>junit</groupId>
    20       <artifactId>junit</artifactId>
    21       <version>3.8.1</version>
    22       <scope>test</scope>
    23     </dependency>
    24     <dependency>
    25     <groupId>io.appium</groupId>
    26     <artifactId>java-client</artifactId>
    27     <version>3.4.0</version>
    28 </dependency>
    29 <dependency>
    30     <groupId>org.testng</groupId>
    31     <artifactId>testng</artifactId>
    32     <version>6.9.10</version>
    33 </dependency>
    34     
    35   </dependencies>
    36 </project>
    pom.xml

    配置后保存,会自动更新依赖包,如果没有更新也可以在右键选中工程,选择Maven-Update Project,进行更新。更新完成后,你就发现你的Maven Dependencies多了很多jar包

    七、创建TestNG测试类,因为是Appium项目,所以我把源代码放在了src/test/java目录下,选中目录,右键选择New->Other->TestNG->TestNG Class

    创建完成后的目录结构:

    八、下面就开始写Appium代码吧,下面是我写的一个demo,app是我自己写的一个测试demo

    APP截图:

              

    测试代码:

      1 package com.zdx.appium.num1;
      2 
      3 import org.testng.Assert;
      4 import org.testng.annotations.Test;
      5 import org.testng.annotations.BeforeTest;
      6 import org.testng.annotations.AfterTest;
      7 
      8 import io.appium.java_client.AppiumDriver;
      9 import io.appium.java_client.android.AndroidDriver;
     10 import io.appium.java_client.android.AndroidElement;
     11 
     12 import java.io.File;
     13 import java.io.UnsupportedEncodingException;
     14 import java.net.URL;
     15 import java.sql.Driver;
     16 import java.sql.Time;
     17 import java.util.List;
     18 import java.util.Random;
     19 
     20 import mx4j.log.Log;
     21 
     22 import org.apache.bcel.generic.RETURN;
     23 import org.junit.After;
     24 import org.junit.Before;
     25 import org.openqa.selenium.By;
     26 import org.openqa.selenium.WebElement;
     27 import org.openqa.selenium.logging.LogEntry;
     28 import org.openqa.selenium.logging.LoggingHandler;
     29 import org.openqa.selenium.logging.LoggingPreferences;
     30 import org.openqa.selenium.logging.Logs;
     31 import org.openqa.selenium.remote.DesiredCapabilities;
     32 import org.openqa.selenium.remote.RemoteWebDriver;
     33 import org.openqa.selenium.remote.server.log.LoggingManager;
     34 import org.openqa.selenium.remote.server.log.LoggingOptions;
     35 import org.testng.asserts.LoggingAssert;
     36     
     37 
     38 public class NumFirst {
     39     private AndroidDriver<AndroidElement> driver;
     40   @BeforeTest
     41   public void setUp() throws Exception{
     42       /* File classpathRoot = new File(System.getProperty("user.dir"));
     43       System.out.println(classpathRoot);
     44       File appDir = new File(classpathRoot, "/apps");
     45       File app = new File(appDir, "");*/
     46       DesiredCapabilities capabilities = new DesiredCapabilities();
     47       
     48       capabilities.setCapability("deviceName","udid");
     49       capabilities.setCapability("platformVersion", "4.4");
     50       capabilities.setCapability("udid","YT91050HXZ");
     51       //capabilities.setCapability("app", app.getAbsolutePath());
     52       capabilities.setCapability("appPackage", "com.example.yadudemo");
     53       capabilities.setCapability("appActivity", ".MainActivity");
     54       //capabilities.setCapability("appActivity", ".ui.activity.UPlusMainActivity");
     55   driver = new AndroidDriver<>(new URL("http://127.0.0.1:4726/wd/hub"), capabilities);
     56   }
     57   @Test
     58   public void test() throws UnsupportedEncodingException {
     59       //验证APP启动成功,并且MainActivity打开
     60       if(activityVerify(8000, driver, ".MainActivity")==true)
     61       {
     62           System.out.println(driver.currentActivity().toString());
     63           Assert.assertEquals(driver.currentActivity().toString(), ".MainActivity");
     64           AndroidElement bt_config = driver.findElementByName("配置");
     65           bt_config.click();
     66           //验证ConfigActivity打开
     67           if(activityVerify(3000, driver, ".ConfigActivity")==true)
     68           {
     69               Assert.assertEquals(driver.currentActivity().toString(), ".ConfigActivity");
     70               List<AndroidElement> et_List = driver.findElementsByClassName("android.widget.EditText");
     71               //输入密码
     72               et_List.get(1).clear();
     73               et_List.get(1).sendKeys("12345678");
     74               //验证输入密码正确
     75               Assert.assertEquals(et_List.get(1).getText(), "12345678");
     76               //输入超时时间
     77               et_List.get(2).clear();
     78               et_List.get(2).sendKeys("12");
     79               //验证超时时间输入正确
     80               Assert.assertEquals(et_List.get(2).getText(), "12");
     81               //如果button被键盘遮挡,关闭键盘
     82               if(driver.findElementsByName("开始配置").isEmpty())
     83               {
     84                   driver.pressKeyCode(4);
     85               }
     86               //点击开始配置
     87               AndroidElement bt_start_config = driver.findElementByName("开始配置");
     88               bt_start_config.click();
     89           }
     90       }
     91       //关闭APP
     92       closeApp();
     93       //验证APP已经关闭
     94       Assert.assertNotEquals(driver.currentActivity().toString(), ".ConfigActivity");
     95       Assert.assertNotEquals(driver.currentActivity().toString(), ".MainActivity");       
     96   }
     97 
     98   @AfterTest
     99   public void tearDown() {
    100       driver.quit();
    101   }
    102   //线程等待方法
    103   public void appWait(long time)
    104   {
    105       try {
    106           Thread.sleep(time);
    107       } catch (InterruptedException e) {
    108           // TODO Auto-generated catch block
    109           e.printStackTrace();
    110       }
    111   }
    112   //关闭APP方法
    113   public void closeApp()
    114   {
    115       for(int h=0;h<6;h++)
    116       {
    117           driver.pressKeyCode(4);
    118       }
    119   }
    120   //验证activity方法
    121  public Boolean activityVerify(long time,AndroidDriver<AndroidElement> tdriver,String activity)
    122  {
    123          for(int i=1;i<time/1000;i++){
    124              appWait(1000);
    125              if(tdriver.currentActivity().equals(activity))
    126              {
    127                  return true;
    128              }    
    129          }
    130          System.out.println("can not find "+activity);
    131          return false;
    132      
    133  }
    134 }
    NumFirst.java

    启动Appium Server,连接手机(我手机已经安装了测试demo APP),右键选中num1工程,选择Run As->TestNG Test,运行后,会在工程目录下创建一个test-output的文件夹,打开文件夹下的index.html可以查看测试结果,也可以在Eclipse的Console中查看结果,如下图:

    这个报告很简陋,下面开始用ReportNG替换TestNG的报告

    九、使用ReportNG生成报告

    1、添加依赖,在pom.xml文件中添加下列依赖关系:

     1         <dependency>
     2             <groupId>org.uncommons</groupId>
     3             <artifactId>reportng</artifactId>
     4             <version>1.1.4</version>
     5             <scope>test</scope>
     6             <exclusions>
     7                 <exclusion>
     8                     <groupId>org.testng</groupId>
     9                     <artifactId>testng</artifactId>
    10                 </exclusion>
    11             </exclusions>
    12         </dependency>
    13         <dependency>
    14             <groupId>com.google.inject</groupId>
    15             <artifactId>guice</artifactId>
    16             <version>4.0</version>
    17             <scope>test</scope>
    18         </dependency>        
    reportNG依赖

    2、配置maven-surefire-plugin并加入reportNG listenser

     1     <build>
     2         <plugins>
     3         <plugin>
     4                 <groupId>org.apache.maven.plugins</groupId>
     5                 <artifactId>maven-surefire-plugin</artifactId>
     6                 <version>2.17</version>
     7                 <configuration>
     8                     <suiteXmlFiles>
     9                         <suiteXmlFile>xmlfile/testng.xml</suiteXmlFile>
    10                     </suiteXmlFiles>
    11                 </configuration>
    12             </plugin>
    13             <plugin>
    14                 <groupId>org.apache.maven.plugins</groupId>
    15                 <artifactId>maven-surefire-plugin</artifactId>
    16                 <version>2.19.1</version>
    17                 <configuration>
    18                     <properties>
    19                         <property>
    20                             <name>usedefaultlisteners</name>
    21                             <value>false</value>
    22                         </property>
    23                         <property>
    24                             <name>listener</name>
    25                             <value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value>
    26                         </property>
    27                     </properties>
    28                     <workingDirectory>target/</workingDirectory>
    29                 </configuration>
    30             </plugin>
    31         </plugins>
    32     </build>
    配置build

    注意这句:<suiteXmlFile>xmlfile/testng.xml</suiteXmlFile>,我在工程目录下新建了一个文件夹,把testng的xml文件都放在这个目录下,如果有多个testng.xml文件的话,这样更直观,如果只有一个的话,不建文件夹也可以。

    如果看不懂的话,请百度maven-surefire-plugin参数说明

    3、更新一下Maven Project,然后右键选中Maven工程,选择Run As->Maven Test,运行成功后,在target目录下创建了一个surefire-reports目录,打开它下面的html文件夹,打开indel.xml,就可以看到相对美观的报告形式了:

    reportNG报告样式是可以自定义的,我的报告中还有乱码,以后会慢慢研究。

  • 相关阅读:
    [CTF]Capture The Flag -- 夺旗赛
    [DesignPattern]Builder设计模式
    [Git]Git 常用的操作命令
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
    QT 实现 读取 增加 删除 实时操作xml
    QT5-图形视图框架
    C++之QT
    ,即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。
    要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是clock tick,即“时钟打点”。同时还有一个常数CLK_TCK,给
    给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字。要求所有可能组合出来的2位数字的和。例如给定2、5、8,则可以组合出:25、28、52、58、82、85,它们的和为330。
  • 原文地址:https://www.cnblogs.com/Eric-zhao/p/5323728.html
Copyright © 2020-2023  润新知