Java + Selenium + TestNG + Maven

时间:2023-01-22 10:28:22

环境准备:

1. Java:

Install Java jdk:
Version: java 1.8 or above
Configure Java Environment Variables:
Add JAVA_HOME
Add CLASSPATH
Add to Path

Java + Selenium + TestNG + Maven

Verify Java was configured: Input javac in CMD can get output successfully.

2. Eclipse https://www.eclipse.org/

Unzip the eclipse file > Open eclipse.exe to launch Eclipse SDK:

Version: Oxygen.1a (4.7.1a)

Set workspace
Set Layout and Text Editor

Java + Selenium + TestNG + Maven

3. TestNG

Install TestNG online in Eclipse:
Click Help -> Install New Software… -> Click Add -> Set Name: TestNG for Eclipse and Location: http://beust.com/eclipse -> Select TestNG to install

Java + Selenium + TestNG + Maven

4. Maven
Copy Maven package to the machine:
Configure Maven Environment Variables:
Add MAVEN_HOME
Add to Path

Java + Selenium + TestNG + Maven

Verify Maven was configured: Input mvn –v in CMD can get output successfully.

5. Configure Maven and Eclipse
Click Help -> Install New Software… -> Click Add -> Set Name: maven for Eclipse and Location: http://download.eclipse.org/technology/m2e/releases -> Select Maven Integration for Eclipse to install
Set Maven Repository :
Set localRepository in MAVEN_HOME/conf/settings.xml, such as:
<localRepository>D:/AutomationProjects/Eclipse/repository</localRepository>
Configure Maven in Eclipse:
Open Eclipse -> Select Window -> Preferences -> Maven
Click Installations -> Click Add to add the installation used to launch Maven -> Click Apply
Click User Settings -> Click Browse to select the MAVEN_HOME/conf/settings.xml -> Click

Java + Selenium + TestNG + Maven

创建Maven Project

1. New a Maven Project
Open Eclipse -> Select File -> New -> Project -> Select Maven -> Maven Project -> Click Next -> Click Next -> Click Next -> Input Group Id and Artifact Id, then click Finish.

2. New a TestNG class
Right click in the Maven project > select New -> Other -> select TestNG -> TestNG class -> Click Next -> Input Class Name and click Finish.
Write the test scenarios in the class.

3. Add TestNG dependency in pom.xml
pom.xml: Project Object Model. Maven configuration file.

Java + Selenium + TestNG + Maven

4. Run the test scripts via TestNG
New a TestNG xml:
Right click the Maven project > select New -> Other -> select XML -> XML File -> Click Next -> Input File name and click Finish.
Input the TestNG xml content like:

Java + Selenium + TestNG + Maven

5. Build test project via Maven
Add build project information in pom.xml

Java + Selenium + TestNG + Maven

Open CMD -> cd to the project folder
Compile the project via command:
>mvn clean compile
Build the project via command:
>mvn clean package

If Build the project success, the .jar file was located in target folder like :
..\target\CPSTestDemo-0.0.1.jar

6. Execute .jar without Eclipse

Java + Selenium + TestNG + Maven