Maven+Selenium+TestNG搭建

本文详细介绍如何在MyEclipse环境中搭建Maven,包括环境配置、项目创建、依赖管理及测试执行等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结合虫师的电子版和Rahul的视频,总结了一套适合自己MyEclipse环境的Maven搭建流程。


1.    准备工作:

JDK : Java 开发程序所使用的环境。

MyEclipse: Java 开发程序所使用的 IDE。

Maven :下载地址: http://maven.apache.org/download.cgi#

For windows:

 

2.     环境配置:

将 Maven 下载到本地解压,我们以下载当前最新版本为例,解压到得到 apache-maven-3.3.3 目录。

下面设置环境变量: “我的电脑”右键菜单--->属性--->高级--->环境变量--->系统变量--->新建..变量名:MAVEN_HOME

变量值:D:\java\apache-maven-3.3.3

找到 path 变量名—>“编辑”添加:

变量名:PATH

变量值:%MAVEN_HOME%\bin;

在 Windows 命令提示符下输入“mvn -version”查看 Maven 是否成功:

 

3.     配置setting.xml

任意选择一个目录来创建Maven项目,此处选择E:\selenium\mavenRepo

修改 Maven 仓库的路径。打开E:\selenium\apache-maven-3.5.2\conf\settings.xml 文件,大概 在54行的位置做如下修改:


该仓库路径可以自己新建: E:\selenium\mavenRepo\Repository

然后将E:\selenium\apache-maven-3.5.2\conf\settings.xml这个setting.xml拷贝一份到E:\selenium\mavenRepo\Repository。注意,原来的不要删掉。

 

4.     创建Maven项目

创建一个目录存放Maven项目,本文是在E盘selenium目录下创建MavenProject目录

>cd /d E:\selenium\mavenRepo\MavenCucumber

创建Maven项目的命令:

Ø  mvn archetype:generate -DgroupId=qaclickacademy-DartifactId=Mavenjava-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


   archetype:generate-> create a dummy Maven project

   artifactId-> will be the Project Name in MyEclipse

   groupId ->will be the Package Name in MyEclipse

   -D ->represent a parameter

   archetypeArtifactId-> template information

   maven-archetype-quickstart-> a common template for test projects across all the organizations

interactiveMode=false-> Its attribute for standard template

由于是首次创建项目,所以,Maven 需要下载一些基础的依赖包,这个过程会相对比较漫长。当项目创建结束,会在当前目录下生成Mavenjava项目。但现在生成的项目还不能直接导入到MyEclipse 中,还需要执行以下命令。



clean 清除项目中的生成结果;compile 编译测试程序。

eclipse:eclipse :生成 Eclipse 项目文件。

 

5.    向MyEclipse导入Maven项目

下面就可以通过MyEclipse导入Mavenjava项目,启动MyEclipse,选择菜单栏File-->Import-->Maven4MyEclipse->Existing Maven Projects -->点击“Next”,添加Mavenjava路径到RootDirectory,然后勾选pom.xml。


修改Maven配置文件路径。在MyEclipse菜单栏选择Window-->Perferences-->Maven-->User Settings。修改UserSetting和Local Repository。

Maven will not hard code jars. And Mavenwill not always pick up jars from the Internet. It will first search from LocalRepository.



打开MyEclipse,Maven Project的结构应该如下,但目前还没有testng.xml。需要后续添加。


6.修改pom.xml

在 Maven 中由 pom.xml 文件对当前项目的包进行管理,在我们创建的Mavenjava 项目中找到该文件打开。

添加需要的plugin。该项目需要testng测试框架,所以需要import maven-surefire-plugin

Thisplugin will help execute all yourtestcases presenting in the test folder.

Getfrom here:

http://maven.apache.org/surefire/maven-surefire-plugin/  -> Usage

<build>

   <pluginManagement>

     <plugins>

        <plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-surefire-plugin</artifactId>

          <version>2.12.4</version>

                  <configuration>

          <suiteXmlFiles>

            <suiteXmlFile>testng.xml</suiteXmlFile>

          </suiteXmlFiles>

        </configuration>

        </plugin>     

     </plugins>

   </pluginManagement>

 </build>

 

导入需要的dependency。

Get from here:

http://mvnrepository.com/

For selenium:

http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/3.8.1

   <dependency>

      <groupId>org.seleniumhq.selenium</groupId>

      <artifactId>selenium-java</artifactId>

      <version>3.8.1</version>

   </dependency>

再次在项目下执行“mvn eclipse:eclipse”命令,打开项目的 Libraries,发现所有的 Selenium 相关的 jar 包都被导入进来了。

 

 <dependencies>

   <dependency>

      <groupId>org.seleniumhq.selenium</groupId>

      <artifactId>selenium-java</artifactId>

      <version>3.8.1</version>

   </dependency>

  

   <dependency>

       <groupId>org.testng</groupId>

       <artifactId>testng</artifactId>

       <version>6.9.9</version>

       <scope>test</scope>

   </dependency>

 

   <dependency>

       <groupId>io.rest-assured</groupId>

       <artifactId>rest-assured</artifactId>

       <version>3.0.6</version>

       <scope>test</scope>

   </dependency>

  

   <dependency>

       <groupId>io.appium</groupId>

       <artifactId>java-client</artifactId>

       <version>5.0.4</version>

   </dependency>

 </dependencies>

 

pom.xml

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>

 <groupId>qaclickacademy</groupId>

 <artifactId>Mavenjava</artifactId>

 <packaging>jar</packaging>

 <version>1.0-SNAPSHOT</version>

 <name>Mavenjava</name>

 <url>http://maven.apache.org</url>

<build>

   <pluginManagement>

     <plugins>

         ……

     </plugins>

   </pluginManagement>

 </build>

 

<dependencies>

 ……

……

</dependencies>

</project>

Ø project:pom.xml 文件中的顶层元素;

Ø modelVersion:指明 POM 使用的对象模型的版本。这个值很少改动。

Ø groupId:指明创建项目的组织或者小组的唯一标识。GroupId 是项目的关键标识,典型的,此标识以组织的完全限定名来定义。比如,org.apache.maven.plugins 是所有 Maven 插件项目指定的groupId。

Ø artifactId:指明此项目产生的主要产品的基本名称。项目的主要产品通常为一个 JAR 文件。第二,象源代码包通常使用 artifactId 作为最后名称的一部分。典型的产品名称使用这个格式: - . (比如:myapp-1.0.jar)。

Ø version:项目产品的版本号。Maven帮助你管理版本,可以经常看到 SNAPSHOT 这个版本,表明项目处于开发阶段。

Ø name:项目的显示名称,通常用于 maven 产生的文档中。

Ø url:指定项目站点,通常用于 maven 产生的文档中。

Ø description:描述此项目,通常用于maven 产生的文档中。

 

7.    配置jdk

Jdk一定要配对,不然会报错。

两个地方需要注意:

1>    Windows->preference->Java->InstalledJREs

这里jdk的路径一定要选正确C:\Program Files\Java\jdk7\jdk1.7,之前因为选成C:\ProgramFiles\Java\jdk7\jre7一直报错。

 

2>    Build Path里JRE Library要选对


 

8.运行TestNG Selenium测试

我们使用 maven,除了可以用它来方便管理 Java 的包外,另一个重要原因是可以用它来运行测试用例。下面我们就在项目中创建基于TestNG的web自动化测试脚本。


测试用例一定要创建在 test 的目录中,如果创建在 main 目录中并不会被 maven 执行。

 

在pom.xml中添加maven-surefire-plugin。该plugin用来执行单元测试。

The SurefirePlugin is used during the test phase of the build lifecycle toexecute the unit tests of an application. It generates reports in two differentfile formats:

·        Plain text files (*.txt)

·        XML files (*.xml)

By default,these files are generatedin ${basedir}/target/surefire-reports/TEST-*.xml.

Get from here:

http://maven.apache.org/surefire/maven-surefire-plugin/-> Usage

 

pom.xml

<project>
  [...]
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  [...]
</project>

 

下面通过 Maven 来执行测试。


clean 清除项目中的生成结果,test 执行测试。

 

9.配置testng.xml来运行TestNGSelenium测试

创建的test cases如果通过mvn test执行,会把test folder下的所有test cases都执行。而通过配置testng.xml可以更灵活指定测试用例的执行。

首先要新建testng.xml

选中项目->右键New->File

testing.xml

<suitename="mavanjava">

   <testname="mava test">

      <classes>

         <classname="qaclickacademy.SeleniumTest"/>

         <classname="qaclickacademy.RestAPITest"/>

      </classes>

   </test>

</suite>

 

其次要修改pom.xml中Surefire Plugin配置

Get from here:

http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html-> Using TestNG -> Using Suite XML Files

 

Another alternative is to use TestNGsuite XML files. This allows flexible configuration of the tests to be run.These files are created in the normal way, and then added to the SurefirePlugin configuration:

 

pom.xml

<plugins>

    [...]

     <plugin>

       <groupId>org.apache.maven.plugins</groupId>

       <artifactId>maven-surefire-plugin</artifactId>

       <version>2.20.1</version>

        <configuration>

          <suiteXmlFiles>

           <suiteXmlFile>testng.xml</suiteXmlFile>

          </suiteXmlFiles>

        </configuration>

     </plugin>

    [...]

</plugins>

 

10.  Run a single test class repeatedly

mvn -Dtest=TestCircle test

http://maven.apache.org/surefire/maven-surefire-plugin/-> Examples -> Running a Single Test


11.    Using profile tag in Maven

By using profile tag in pom.xml, you can createmultiple testing.xml based on the requirement, e.g. testing.xml for smoke test,sanity test, regression test, and don’t need to change pom.xml every time torun test cases.

Just run different testing.xml with different commandby given id name.



-P -> represent profile tag

 

testng.xml

<suitename="mavanjava">

   <testname="mava test">

      <classes>

         <classname="qaclickacademy.1Test"/>

         <classname="qaclickacademy.2Test"/>

      </classes>

   </test>

</suite>

 

testng2.xml

<suitename="mavanjava">

   <testname="mava test">

      <classes>

         <classname="qaclickacademy.1Test"/>

         <classname="qaclickacademy.2Test"/>

         <classname="qaclickacademy.3Test"/>

            ……

            ……

<classname="qaclickacademy.100Test"/>

      </classes>

   </test>

</suite>

 

pom.xml

 <profiles>

 <profile>

  <id>Regression</id>

 <build>

   <pluginManagement>

     <plugins>

        <plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-surefire-plugin</artifactId>

          <version>2.12.4</version>

          <configuration>

          <suiteXmlFiles>

            <suiteXmlFile>testng2.xml</suiteXmlFile>

          </suiteXmlFiles>

        </configuration>

        </plugin>     

     </plugins>

   </pluginManagement>

 </build>

 </profile>

 

 <profile>

  <id>Smoke</id>

 <build>

   <pluginManagement>

     <plugins>

        <plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-surefire-plugin</artifactId>

          <version>2.12.4</version>

          <configuration>

          <suiteXmlFiles>

            <suiteXmlFile>testng.xml</suiteXmlFile>

          </suiteXmlFiles>

        </configuration>

        </plugin>     

     </plugins>

   </pluginManagement>

 </build>

 </profile>

  </profiles>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值