一、下載必要的文件
1、eclipse
2、jdk
3、selenium IDE、Selenium Server、Selenium Client Drivers(Java)等等
Selenium下載地址 備注:需要代理服務器才能下載 我使用的是太太貓
4、maven安裝、配置等
二、安裝
1、Eclipse解壓縮就可以用了
2、jdk安裝、配置變量等
3、Selenium相關的安裝
4、maven
最新版本的Eclipse已經自帶maven
三、運行
1、Eclipse建個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/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>
- </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>
pom.xml修改保存后,Eclipse會自動把需要的jar包下載完成
給項目工程配置所需要的庫
工程右擊properties->java build path->add library
2、測試Firefox(ExampleForFirefox.java)
- 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 ExampleForFireFox {
- public static void main(String[] args) {
- // 如果你的 FireFox 沒有安裝在默認目錄,那么必須在程序中設置
- // System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
- // 創建一個 FireFox 的瀏覽器實例
- WebDriver driver = new FirefoxDriver();
- // 讓瀏覽器訪問 Baidu
- driver.get("http://www.baidu.com");
- // 用下面代碼也可以實現
- // driver.navigate().to("http://www.baidu.com");
- // 獲取 網頁的 title
- System.out.println("1 Page title is: " + driver.getTitle());
- // 通過 id 找到 input 的 DOM
- WebElement element = driver.findElement(By.id("kw"));
- // 輸入關鍵字
- element.sendKeys("zTree");
- // 提交 input 所在的 form
- element.submit();
- // 通過判斷 title 內容等待搜索頁面加載完畢,間隔10秒
- (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
- public Boolean apply(WebDriver d) {
- return d.getTitle().toLowerCase().endsWith("ztree");
- }
- });
- // 顯示搜索結果頁面的 title
- System.out.println("2 Page title is: " + driver.getTitle());
- //關閉瀏覽器
- driver.quit();
- }
運行正常的情況下就可以看到自動打開Firefox窗口,訪問baidu.com,然后輸入關鍵字並查詢
在搭建、測試中出現了幾個問題:
問題1:
Description Resource Path Location Type ArtifactTransferException: Failure to transfer com.google.code.findbugs:jsr305:jar:1.3.9 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact com.google.code.findbugs:jsr305:jar:1.3.9 from/to central (http://repo.maven.apache.org/maven2): repo.maven.apache.org pom.xml /lesson line 2 Maven Dependency Problem
解決方案:
可能是連不上http://repo1.maven.org/maven2這個倉庫,在pom.xml文件加一下下面的配置試試看。
xml代碼:
- <repositories>
- <repository>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- <id>central</id>
- <name>Maven Repository Switchboard</name>
- <url>http://repo2.maven.org/maven2</url>
- </repository>
- </repositories>
問題2:
ava文件(.java)右鍵run as沒有java application
解決方法:
public static void main(String[] args)
這幾個必不可少
public
static
void
main
String[] arg 或者 String arg[]
可能少掉其中一個元素
問題3:
Caused by: com.thoughtworks.selenium.SeleniumException: Failed to start new browser session: java.lang.RuntimeException: Browser not supported: http://www.baidu.com (Did you forget to add a *?)
分析:出現這個錯誤,是說明你的 FireFox 文件並沒有安裝在默認目錄下,這時候需要在最開始執行:System.setProperty 設置環境變量 "webdriver.firefox.bin" 將自己機器上 FireFox 的正確路徑設置完畢后即可。
解決方法:
如果你的 FireFox 沒有安裝在默認目錄,那么必須在程序中設置
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
3、測試IE
Selenium 主要也就是針對 FireFox 和 IE 來制作的,所以把 FireFox 的代碼修改為 IE 的,那是相當的容易,只需要簡單地兩步:
1)把 ExampleForFireFox.java 另存為 ExampleForIE.java
2)把 WebDriver driver = new FirefoxDriver(); 修改為 WebDriver driver = new InternetExplorerDriver();
3)一般大家的 IE都是默認路徑,所以也就不用設置 property 了
問題1:
運行出現
WebDriverException: Message: u'Unexpected error launching Internet Explorer. Protected Mode must be set to the same value (enabled or disabled) for all zones.'
解決方法:
進入ie8工具選項->進入“安全”tab->保證"internet" "本地intranet" "可信站點" "受限站點"四個安全設置下的"啟用保護模式(要求重新啟動Internet Explorer)"的選擇是一致的:要么都選,要么都不選。由於ie8在默認情況下,只"受限站點"安全設置內此選項是啟用的,所以才會出現上述問題中的exception