一、使用Maven Java Selenium3.6.0搭建自动化测试环境所需要的软件版本介绍
selenium版本:3.6.0
webdriver为geckodriver,版本为:
v0.18.0
JDK :jdk1.8.0_131
junit 版本:4.12
Firefox 版本:Firefox Setup 55.0
二、准备工作
1、确保系统和java开发环境的jdk为 1.8或以上, 我用的JDK版本是 :jdk1.8.0_131
2、firefox 47以上版本,需要下载第三方driver,即geckodriver,我使用的是v0.18.0版本,建议找到v0.18.0版本进行下载
下载地址:https://github.com/mozilla/geckodriver/releases
3、因为Selenium WebDriver geckodriver版本是v0.18.0 geckodriver要求对应的 Firefox 版本为Firefox Setup 55.0
Firefox Setup 55.0下载地址:http://ftp.mozilla.org/pub/firefox/releases/55.0/win64/zh-CN/ 下载Firefox Setup 55.0.exe 并安装
Selenium3.6介绍
2)、Selenium3.6中的Firefox浏览器驱动独立了,以前装完selenium2就可以驱动Firefox浏览器了,现在和 Chrome一样,必须下载和设置浏览器驱动(geckodriver)。
3)、MAC OS 集成Safari的浏览器驱动。默认在/usr/bin/safaridriver 目录下。
4)、只支持IE 9.0版本以上。
三、搭建Selenium3.6.0自动化测试环境
我这边使用的是myeclipse,打开myeclipse,新建一个maven项目——>新建pom.xml文件,具体代码如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xym</groupId>
<artifactId>TestSelenium</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestSelenium Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>TestSelenium</finalName>
</build>
</project>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xym</groupId>
<artifactId>TestSelenium</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestSelenium Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>TestSelenium</finalName>
</build>
</project>
自动化测试代码如下:
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertNotNull;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Selenium2Example1Test {
private static WebDriver driver = null;
@BeforeClass
public static void beforeClass() {
// check driver https://github.com/mozilla/geckodriver#supported-firefoxen
System.setProperty("webdriver.gecko.driver", "D:/Tool/selenium2Tool/geckodriver.exe");//这里的路径是geckodriver.exe所在目录的路径
driver = new FirefoxDriver();
System.out.println("Start integration test");
}
@AfterClass
public static void afterClass() {
if (null != driver) {
// driver.quit();
driver = null;
}
System.out.println("End integration test");
}
@Test
public void test001() throws InterruptedException {
String url = "http://www.baidu.com";
System.out.println(System.getProperty("webdriver.gecko.driver"));
driver.get(url);
WebElement searchInput = driver.findElement(By.id("kw"));
searchInput.sendKeys("Selenium");
searchInput.submit();
SECONDS.sleep(2);
WebElement baiKei = driver.findElement(By.cssSelector("a em"));
assertNotNull(baiKei);
baiKei.click();
SECONDS.sleep(1);
}
}
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertNotNull;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Selenium2Example1Test {
private static WebDriver driver = null;
@BeforeClass
public static void beforeClass() {
// check driver https://github.com/mozilla/geckodriver#supported-firefoxen
System.setProperty("webdriver.gecko.driver", "D:/Tool/selenium2Tool/geckodriver.exe");//这里的路径是geckodriver.exe所在目录的路径
driver = new FirefoxDriver();
System.out.println("Start integration test");
}
@AfterClass
public static void afterClass() {
if (null != driver) {
// driver.quit();
driver = null;
}
System.out.println("End integration test");
}
@Test
public void test001() throws InterruptedException {
String url = "http://www.baidu.com";
System.out.println(System.getProperty("webdriver.gecko.driver"));
driver.get(url);
WebElement searchInput = driver.findElement(By.id("kw"));
searchInput.sendKeys("Selenium");
searchInput.submit();
SECONDS.sleep(2);
WebElement baiKei = driver.findElement(By.cssSelector("a em"));
assertNotNull(baiKei);
baiKei.click();
SECONDS.sleep(1);
}
}
运行 mvn test