【1. 加入 jUnit】
Selenium 环境搞定,接下来就要想办法让我们的测试步入正轨了,对于 java 来说,用上 jUnit 是很方便的,maven 项目加入 jUnit 也是轻而易举,稍微修改一下 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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>Selenium2Test</groupId>
- <artifactId>Selenium2Test</artifactId>
- <version>1.0</version>
- <dependencies>
- <dependency>
- <groupId>org.seleniumhq.selenium</groupId>
- <artifactId>selenium-java</artifactId>
- <version>2.25.0</version>
- </dependency>
- <dependency>
- <groupId>com.opera</groupId>
- <artifactId>operadriver</artifactId>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>com.opera</groupId>
- <artifactId>operadriver</artifactId>
- <version>0.16</version>
- <exclusions>
- <exclusion>
- <groupId>org.seleniumhq.selenium</groupId>
- <artifactId>selenium-remote-driver</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
4.0.0 Selenium2Test Selenium2Test 1.0 org.seleniumhq.selenium selenium-java 2.25.0 com.opera operadriver junit junit 4.8.2 test com.opera operadriver 0.16 org.seleniumhq.selenium selenium-remote-driver
把上一篇中测试 FireFox 的代码拿来按照 jUnit 的结构适当修改一下,就可以直接利用 jUnit 进行测试了
- package lesson2;
- import java.util.List;
- import org.junit.AfterClass;
- import org.junit.BeforeClass;
- 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 org.openqa.selenium.support.ui.ExpectedCondition;
- import org.openqa.selenium.support.ui.WebDriverWait;
- public class ExampleJunit {
- static WebDriver driver;
- @BeforeClass
- public static void init() {
- System.out.println("init...");
- // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
- System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
- // 创建一个 FireFox 的浏览器实例
- driver = new FirefoxDriver();
- }
- @Test
- public void test() {
- // 让浏览器访问 zTree Demo
- driver.get("http://www.ztree.me/v3/demo/cn/core/standardData.html");
- // 等待 zTree 初始化完毕,Timeout 设置10秒
- (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
- public Boolean apply(WebDriver d) {
- WebElement element = driver.findElement(By.id("treeDemo"));
- return element.findElement(By.tagName("a")) != null;
- }
- });
- WebElement element = driver.findElement(By.id("treeDemo"));
- List<WebElement> elements = element.findElements(By.tagName("li"));
- // 显示生成的节点DOM 数量
- System.out.println("treeNode DOM length = " + elements.size());
- }
- @AfterClass
- public static void destory() {
- System.out.println("destory...");
- //关闭浏览器
- driver.quit();
- }
- }