创建一个maven工程

使用Idea创建maven工程

建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是用来进行自动化测试的,所以实际这里src/main/java是用不到的,只是IDEA统一规则建立了而已。这里个人还建立了一个bin目录,用来存放chromedriver等浏览器执行文件,用来匹配不同的操作系统及不同的浏览器版本。

pom.xml中的内容

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.wilmar.test</groupId>
 8     <artifactId>woodpecker</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <!--<packaging>jar</packaging>-->
11 
12     <dependencies>
13         <dependency>
14             <groupId>org.testng</groupId>
15             <artifactId>testng</artifactId>
16             <version>6.14.3</version>
17             <scope>test</scope>
18         </dependency>
19         <dependency>
20             <groupId>org.seleniumhq.selenium</groupId>
21             <artifactId>selenium-java</artifactId>
22             <version>3.141.59</version>
23         </dependency>
24         <dependency>
25             <groupId>com.alibaba</groupId>
26             <artifactId>dubbo</artifactId>
27             <version>2.5.3</version>
28             <exclusions>
29                 <exclusion>
30                     <groupId>org.springframework</groupId>
31                     <artifactId>spring</artifactId>
32                 </exclusion>
33             </exclusions>
34         </dependency>
35 
36     </dependencies>
37     <build>
38         <plugins>
39             <plugin>
40                 <groupId>org.apache.maven.plugins</groupId>
41                 <artifactId>maven-dependency-plugin</artifactId>
42                 <executions>
43                     <execution>
44                         <id>copy-dependencies</id>
45                         <phase>prepare-package</phase>
46                         <goals>
47                             <goal>copy-dependencies</goal>
48                         </goals>
49                         <configuration>
50                             <outputDirectory>${project.build.directory}/lib</outputDirectory>
51                             <overWriteReleases>false</overWriteReleases>
52                             <overWriteSnapshots>false</overWriteSnapshots>
53                             <overWriteIfNewer>true</overWriteIfNewer>
54                             <excludeScope>provided</excludeScope>
55                         </configuration>
56                     </execution>
57                 </executions>
58             </plugin>
59             <plugin>
60                 <groupId>org.apache.maven.plugins</groupId>
61                 <artifactId>maven-compiler-plugin</artifactId>
62                 <version>3.0</version>
63                 <configuration>
64                     <source>1.7</source>
65                     <target>1.7</target>
66                     <encoding>UTF-8</encoding>
67                 </configuration>
68             </plugin>
69         </plugins>
70     </build>
71     <distributionManagement>
72         <repository>
73             <id>releases</id>
74             <name>Team nexus Release Repository</name>
75             <url>http://10.118.888.10:8888/nexus/content/repositories/releases</url>
76         </repository>
77         <snapshotRepository>
78             <id>snapshots</id>
79             <name>Team nexus Snapshot Repository</name>
80             <url>http://10.118.888.10:8888/nexus/content/repositories/snapshots</url>
81         </snapshotRepository>
82     </distributionManagement>
83 </project>

 

这里计划使用TestNG和Selenium 作为自动化测试的框架,所以添加了这两个依赖包(根据需要添加)。另外repository镜像源地址最好添加为公司私有的资源镜像地址或者国内一些开源的镜像地址,比如阿里云、网易等。

 编写测试用例

在src/test/java下建立一个测试类,包名com.nitb.demo(根据自己意愿随意取),类名Demo1。

代码如下:

 
 
 1 package com.nitb.demo;
 2 
 3 import org.openqa.selenium.WebDriver;
 4 import org.openqa.selenium.chrome.ChromeDriver;
 5 import org.testng.annotations.AfterClass;
 6 import org.testng.annotations.BeforeClass;
 7 import org.testng.annotations.Test;
 8 
 9 import java.io.File;
10 
11 public class Demo1 {
12     private static WebDriver driver;
13     private static String executePath;
14 
15     @BeforeClass
16     public void init() {
17         executePath = System.getProperty("user.dir") + File.separator + "bin" + File.separator + "chromedriver_mac";
18         System.setProperty("webdriver.chrome.driver", executePath);
19         driver = new ChromeDriver();
20     }
21 
22     @Test
23     public void testDemoLogin() throws InterruptedException{
24         System.out.print("testDemoLogin");
25     }
26 
27     @AfterClass
28     public void quit() {
29         driver.quit();
30     }
31 }

 

 

测试用例比较简单,三个逻辑:

  1. 测试类实例化开始的时候,执行init,设置下当前系统应该使用哪个chromedriver,因为我是mac,且Chrome浏览器的版本是72.0.3626.81,所以去selenium官网下载了对应版本的driver放在bin下面使用。
  2. 使用testng.xml去运行类中有@Test注释的测试方法。
  3. 测试类实例销毁的时候,退出ChromeDriver。

 

 testng.xml的内容

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3 <suite name="Suite1" verbose="1" >
4     <test name="Regression1">
5         <classes>
6             <class name="com.wilmar.loms.testcase.ManualOrderTest"/>
7         </classes>
8     </test>
9 </suite>

 

这里的demo属于较简单的:

  •  suite:代表一个测试集,里面可以包含多个测试。
  • test:代表一个测试事务,里面可以运行多个测试类及多个测试方法。
  • classes:包含测试事务要运行的所有测试类。
  • classs:代表具体运行哪个类。

 通过IDEA运行testng.xml

右击testng.xml文件,然后点击run xxx/testngxml即可。

命令行运行testng.xml

java -ea -cp $CLASSPATH org.testng.TestNG testng.xml

这里需要注意的是$CLASSPATH指的是所有的依赖包,jar包较多,这里不举例,另外最后一个参数可以是testng.xml单个文件,也可以是包含多个testng xml配置文件的路径。

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!