Study:基于Selenium进行firefox自动化测试的例子。
Maven配置:
<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.hisky</groupId>
<artifactId>seleniumTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>seleniumTest</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.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.35.0</version>
</dependency>
</dependencies>
</project>
代码事例:
package com.hisky.seleniumTest;
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;
/**
*
* @author zhenglin.yang
*
*/
public class WebTest {
public static void main(String[] args) {
// testSearch();
testLogin();
}
private static void testSearch() {
// System.setProperty ( "webdriver.firefox.driver" ,
// "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" );
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
WebElement element = driver.findElement(By.name("wd"));
element.sendKeys("中国");
element.submit();
(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("中国");
}
});
System.out.println("Page title is: " + driver.getTitle());
// Close the browser
driver.quit();
}
private static void testLogin() {
// System.setProperty ( "webdriver.firefox.driver" ,
// "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" );
WebDriver driver = new FirefoxDriver();
System.out.println("starts browser...");
driver.get("http://yun.baidu.com");
System.out.println("access login page...");
WebElement element = driver.findElement(By.name("userName"));
element.sendKeys("your user name...");
System.out.println("input user name...");
element = driver.findElement(By.name("password"));
element.sendKeys("your password...");
System.out.println("input password");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
// Close the browser
driver.quit();
}
}
以上内容参考selenium官网教程。firefox的版本不能高于20,否则无法启动。