maven + junit + webdriver 简单demo

http://blog.csdn.net/caiqcong/article/details/8587802

 

一、环境准备

1. 安装jdk 1.6

2. 安装maven 2.0

3. 安装 elipse

4. 安装 eclipse mvn2 插件

5. 下载selenium-java 2.0 开发包

6. 下载junit 4.0 jar包

7. 利用 mvn 创建java 项目

mvn archetype:create   -DgroupId=packageName    -DartifactId=projectName 

8. 将mvn项目转换为eclipse项目

mvn eclipse:eclipse

9. 将mvn项目导入eclipse

Import existing mavenproject


二、添加web driver 开发包和第三方jar包到项目(可以直接在eclipse build path中添加第三方类,这里采用maven管理)

1.      安装selenium jar包和第三方加班到mvn本地仓库

D:\webdriver\projectName>mvn install:install-file -Dfile="D:\junit\junit-4.10.jar" -DgroupId=junit -DartifactId=junit -Dversion=4.10-Dpackaging=jar


D:\webdriver\projectName>mvn install:install-file -Dfile="D:\chromedownload\selenium-java-2.28.0\selenium-2.28.0\selenium-java-2.28.0.jar"

-DgroupId=org.openqa.selenium-DartifactId=selenium-java -Dversion=2.28.0 -Dpackaging=jar


D:\webdriver\projectName>mvn install:install-file -Dfile="D:\chromedownload\selenium-java-2.28.0\selenium-2.28.0\libs\guava-13.0.1.jar"

-DgroupId=com.google -DartifactId=guava13-Dversion=13.0.1 -Dpackaging=jar

D:\webdriver\projectName>mvn install:install-file -Dfile="D:\chromedownload\selenium-java-2.28.0\selenium-2.28.0\libs\ json-20080701.jar"

-DgroupId=org.json -DartifactId=json -Dversion=20080701-Dpackaging=jar

D:\webdriver\projectName>mvn install:install-file -Dfile="D:\chromedownload\selenium-java-2.28.0\selenium-2.28.0\libs\commons-exec-1.1.jar"

-DgroupId=org.apache -DartifactId=commons-exec -Dversion=1.1-Dpackaging=jar


D:\webdriver\projectName>mvn install:install-file -Dfile="D:\chromedownload\selenium-java-2.28.0\selenium-2.28.0\libs\ httpclient-4.2.1.jar"

-DgroupId=org.apache.http -DartifactId=httpclient -Dversion=4.2.1-Dpackaging=jar

2.      在pom文件中添加依赖

  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.taobao</groupId> 
  6.   <artifactId>projectName</artifactId> 
  7.   <version>1.0-SNAPSHOT</version> 
  8.   <packaging>jar</packaging> 
  9.  
  10.   <name>projectName</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>4.10</version> 
  22.       <scope>test</scope> 
  23.     </dependency> 
  24.     <dependency> 
  25.       <groupId>org.openqa.selenium</groupId> 
  26.       <artifactId>selenium-java</artifactId> 
  27.       <version>2.28.0</version> 
  28.       </dependency> 
  29.       <dependency> 
  30.       <groupId>com.google</groupId> 
  31.       <artifactId>guava13</artifactId> 
  32.       <version>13.0.1</version> 
  33.     </dependency> 
  34.     <dependency> 
  35.       <groupId>org.json</groupId> 
  36.       <artifactId>json</artifactId> 
  37.       <version>20080701</version> 
  38.     </dependency> 
  39.     <dependency> 
  40.       <groupId>org.apache.commons</groupId> 
  41.       <artifactId>commons-exec</artifactId> 
  42.       <version>1.1</version> 
  43.     </dependency> 
  44.     <dependency> 
  45.       <groupId>org.apache.httpcomponents</groupId> 
  46.       <artifactId>httpclient</artifactId> 
  47.       <version>4.2.1</version> 
  48.     </dependency> 
  49.   </dependencies> 
  50. </project> 
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.taobao</groupId>
  <artifactId>projectName</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.openqa.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.28.0</version>
      </dependency>
      <dependency>
      <groupId>com.google</groupId>
      <artifactId>guava13</artifactId>
      <version>13.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20080701</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-exec</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.2.1</version>
    </dependency>
  </dependencies>
</project>



 

三、编写测试类

WebDriverDemoTest.java

  1. package com.taobao.webdriver; 
  2.  
  3. import java.util.concurrent.TimeUnit; 
  4.  
  5. import org.junit.After; 
  6. import org.junit.Before; 
  7. import org.junit.Test; 
  8. import org.openqa.selenium.By; 
  9. import org.openqa.selenium.WebDriver; 
  10. import org.openqa.selenium.WebElement; 
  11. import org.openqa.selenium.firefox.FirefoxDriver; 
  12.  
  13. import junit.framework.TestCase; 
  14.  
  15. public class WebDriverDemoTest extends TestCase{ 
  16.     private WebDriver driver; 
  17.     private String baseUrl; 
  18.  
  19.     @Before 
  20.     public void setUp() throws Exception { 
  21.         driver = new FirefoxDriver(); 
  22.         baseUrl = "http://www.baidu.com/"
  23.         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
  24.     } 
  25.   
  26.     @Test 
  27.     public void testSearchMyBlog() throws Exception { 
  28.         driver.get(baseUrl); 
  29.          
  30.         WebElement element = driver.findElement(By.id("kw")); 
  31.         element.clear(); 
  32.         element.sendKeys("webDriver"); 
  33.          
  34.         element = driver.findElement(By.id("su")); 
  35.         element.click(); 
  36.          
  37.         Thread.sleep(3*1000); 
  38.     } 
  39.   
  40.     @After 
  41.     public void tearDown() throws Exception { 
  42.         driver.quit(); 
  43.     } 
package com.taobao.webdriver;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import junit.framework.TestCase;

public class WebDriverDemoTest extends TestCase{
	private WebDriver driver;
	private String baseUrl;

	@Before
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		baseUrl = "http://www.baidu.com/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
 
	@Test
	public void testSearchMyBlog() throws Exception {
		driver.get(baseUrl);
		
		WebElement element = driver.findElement(By.id("kw"));
		element.clear();
		element.sendKeys("webDriver");
		
		element = driver.findElement(By.id("su"));
		element.click();
		
		Thread.sleep(3*1000);
	}
 
	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽可能详细地回答你的问题。 首先,我们需要了解一下这几个工具的作用: - Eclipse:Java开发的集成开发环境(IDE) - Maven:Java项目管理工具,可以自动化构建、依赖管理、打包等操作 - Junit5:Java的单元测试框架,可以方便地编写和执行单元测试 - Pitest:Java的变异测试框架,可以自动生成变异体并运行测试用例,用于评估测试用例的质量。 接下来,我们按照以下步骤来进行参数化测试: 1. 在 Eclipse 中创建 Maven 项目,可以选择使用 Maven 的 Quickstart 模板来创建。 2. 在 pom.xml 文件中添加 Junit5 和 Pitest 的依赖: ```xml <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.pitest</groupId> <artifactId>pitest-junit5-plugin</artifactId> <version>0.13</version> </dependency> ``` 3. 编写测试类,使用 Junit5 的 ParameterizedTest 注解来标记参数化测试方法,并使用 ValueSource 注解来指定参数值: ```java import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class CalculatorTest { private final Calculator calculator = new Calculator(); @ParameterizedTest @ValueSource(ints = {1, 2, 3}) void testAdd(int value) { int result = calculator.add(value, 2); Assertions.assertEquals(value + 2, result); } } ``` 4. 运行测试,可以使用 Maven 命令 `mvn test` 来执行所有测试用例,也可以使用 Eclipse 的 Junit5 运行器来执行单个测试用例。 5. 运行 Pitest,可以使用 Maven 命令 `mvn org.pitest:pitest-maven:mutationCoverage` 来运行 Pitest。Pitest 会自动生成变异体并运行测试用例,最后生成一个变异测试报告。 以上就是使用 Eclipse+Maven+Junit5+Pitest 进行参数化测试的步骤。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值