maven + junit + webdriver 简单demo
2014-04-22 13:16
330 查看
/article/10558016.html
一、环境准备
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文件中添加依赖
[html]
view plaincopyprint?
<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
[java]
view plaincopyprint?
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();
}
}
一、环境准备
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文件中添加依赖
[html]
view plaincopyprint?
<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>
<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
[java]
view plaincopyprint?
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();
}
}
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(); } }
相关文章推荐
- maven + junit + webdriver 简单demo
- maven+Spring简单web项目demo
- Maven+SpringMVC+Dubbo+zookeeper 简单的入门demo配置
- 简单的Maven(web)项目创建及运行
- webdriver实现简单的窗口切换
- eclipse+maven+cxf webservice demo
- Selenium WebDriver的简单操作说明
- eclipse 使用maven 构建springbooot+mysql +freemarker 简单web 项目
- 轻松自动化---selenium-webdriver(python) (三) 简单元素的定位
- Selenium2(webdriver)入门之TestNG的安装与简单使用
- 创建一个简单的maven web项目
- java web: eclipse & maven & jetty & struts2 & mysql = 简单登录页面
- Selenium WebDriver Demo
- python爬虫webdriver.Chrome 数据可视化简单案例matplotlib
- eclipse 使用maven 构建web项目 简单例子
- selenuim 2+webdriver+Junit 实现自动化登录
- 简单Maven的Web项目之验证码(jcaptcha组件篇)
- Selenium WebDriver的简单操作说明
- webdriver简单对象的定位_python