zTree 東西不多,我也一直使用著原始的人工測(cè)試手段,隨著內(nèi)容的不斷增多,測(cè)試起來就越發(fā)的繁雜,而且經(jīng)常犯懶,這樣就會(huì)忽略很多本該發(fā)現(xiàn)的問題,而且也容易出現(xiàn)舊的bug 反復(fù)出現(xiàn)的情況,這都是測(cè)試不規(guī)范造成的。要做好東西就要更加規(guī)范和嚴(yán)格,于是乎決定要學(xué)習(xí)一下 Selenium WebDriver,也就是原先的 Selenium v2 了,這方面整體的文章并不多,所以一邊學(xué)著,自己一邊整理吧。
對(duì)于這個(gè)可以自動(dòng)化測(cè)試的工具( Selenium WebDriver)我就不做過多描述了,去 google、baidu 搜索一下即可。 我這里只記錄學(xué)習(xí) Selenium WebDriver 的過程,尤其是運(yùn)行時(shí)可能出現(xiàn)的問題,當(dāng)然了,我是做java的,我只學(xué)習(xí) java 與 Selenium WebDriver 配合的方法。
一、下載文件
先要去官網(wǎng)(
http://seleniumhq.org/download/)下載必需的文件:
Selenium IDE (專門用于 FireFox 測(cè)試的獨(dú)立界面,可以錄制測(cè)試步驟,但我更傾向于寫代碼做標(biāo)準(zhǔn)的功能測(cè)試)
Selenium Server (可以輸入指令控制、可以解決跨域的 js 問題,等到后面學(xué)到了再講吧)
The Internet Explorer Driver Server (專門用于IE測(cè)試的)
Selenium Client Drivers (可以找到你熟悉的語(yǔ)言,例如我選擇的 Java)
Third Party Browser Drivers NOT SUPPORTED/DEVELOPED by seleniumhq(第三方開發(fā)的 Selenium 插件,第一個(gè)就是 Chrome 的,否則你就沒辦法測(cè)試 Chrome 了)
其他的,就根據(jù)你自己的需要尋找吧,目前這些足夠我用了。
二、安裝 & 運(yùn)行
貌似擺弄新東西時(shí),只有 “Hello World” 蹦出來以后,我們這些初學(xué)者才會(huì)感到情緒穩(wěn)定,那就趕緊開始吧。
對(duì)于初期打算直接用編程方式制作測(cè)試用例的情況來說,Selenium IDE、Selenium Server 都可以不用安裝執(zhí)行。
英語(yǔ)好的朋友可以直接看官網(wǎng)的文檔(
http://seleniumhq.org/documentation/)就能夠開始使用了。
看中文的,就繼續(xù)聽我嘮叨:
【1. 建立 Maven 工程】
Selenium 支持 maven 工程,這會(huì)讓你的工作更加簡(jiǎn)便。
用 Eclipse 建個(gè) Maven 的工程,建成后,直接修改 pom.xml,(參考:
http://seleniumhq.org/docs/03_webdriver.html#setting-up-a-selenium-webdriver-project)
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 會(huì)自動(dòng)把需要的 jar 包下載完成。
【2. 測(cè)試 FireFox】
Selenium 最初就是在 FireFox 上做起來的插件,所以我們先來搭建 FireFox 的環(huán)境。
確保你正確安裝了 FireFox 后,就可以直接編寫 java 代碼測(cè)試嘍。
在 lesson1 目錄下建立 ExampleForFireFox.java
(因?yàn)閲?guó)內(nèi)不少朋友訪問 google 的時(shí)候會(huì)出問題,所以我就把代碼中的 google 變成 baidu 了)
Java代碼
package lesson1;
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 沒有安裝在默認(rèn)目錄,那么必須在程序中設(shè)置
// System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
// 創(chuàng)建一個(gè) FireFox 的瀏覽器實(shí)例
WebDriver driver = new FirefoxDriver();
// 讓瀏覽器訪問 Baidu
driver.get("http://www.baidu.com");
// 用下面代碼也可以實(shí)現(xiàn)
// driver.navigate().to("http://www.baidu.com");
// 獲取 網(wǎng)頁(yè)的 title
System.out.println("1 Page title is: " + driver.getTitle());
// 通過 id 找到 input 的 DOM
WebElement element = driver.findElement(By.id("kw"));
// 輸入關(guān)鍵字
element.sendKeys("zTree");
// 提交 input 所在的 form
element.submit();
// 通過判斷 title 內(nèi)容等待搜索頁(yè)面加載完畢,Timeout 設(shè)置10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().endsWith("ztree");
}
});
// 顯示搜索結(jié)果頁(yè)面的 title
System.out.println("2 Page title is: " + driver.getTitle());
//關(guān)閉瀏覽器
driver.quit();
}
}
普通情況下,直接運(yùn)行代碼就可以看到會(huì)自動(dòng)彈出 FireFox 窗口,訪問 baidu.com,然后輸入關(guān)鍵字并查詢,一切都是自動(dòng)完成的。
錯(cuò)誤提醒:
1)Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
出現(xiàn)這個(gè)錯(cuò)誤,是說明你的 FireFox 文件并沒有安裝在默認(rèn)目錄下,這時(shí)候需要在最開始執(zhí)行:System.setProperty 設(shè)置環(huán)境變量 "webdriver.firefox.bin" 將自己機(jī)器上 FireFox 的正確路徑設(shè)置完畢后即可。
2)Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request
出現(xiàn)這個(gè)錯(cuò)誤,很有意思。 查了一下 有人說應(yīng)該是 hosts 出現(xiàn)了問題,加上一個(gè) 127.0.0.1 localhost 就行了,但我的 hosts 上肯定有這個(gè)玩意,為啥也會(huì)出現(xiàn)這個(gè)問題呢?
經(jīng)過調(diào)試,發(fā)現(xiàn) 127.0.0.1 localhost 的設(shè)置必須要在 hosts 文件的最開始,而且如果后面有其他設(shè)置后,也不要再出現(xiàn)同樣的 127.0.0.1 localhost ,只要有就會(huì)出錯(cuò)。(因?yàn)槲覟榱朔奖阍L問 google 的網(wǎng)站,專門加入了 smarthosts 的內(nèi)容,導(dǎo)致了 localhost 的重復(fù))
【3. 測(cè)試 Chrome】
Chrome 雖然不是 Selenium 的原配,但是沒辦法,她太火辣了,絕對(duì)不能拋下她不管的。
把 ExampleForFireFox.java 稍微修改就可以制作出一個(gè) ExampleForChrome.java ,直接把 new FireFoxDriver() 修改為 new ChromeDriver() 你會(huì)發(fā)現(xiàn)還是行不通。
錯(cuò)誤如下:
1)Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
這應(yīng)該是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路徑,這里要注意,是 “webdriver.chrome.driver” 可不是“webdriver.chrome.bin”
設(shè)置路徑后還是會(huì)報(bào)錯(cuò):
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)] Can't retrieve a valid WinSAT assessment.
這個(gè)貌似是因?yàn)?Selenium 無(wú)法直接啟動(dòng) Chrome 導(dǎo)致的,必須要通過前面咱們下載 Chrome 的第三方插件 ChromeDriver,去看第一個(gè)錯(cuò)誤中提示給你的 網(wǎng)址:
http://code.google.com/p/selenium/wiki/ChromeDriver按照人家給的例子來修改我們的測(cè)試代碼吧:
Java代碼
package lesson1;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExampleForChrome {
public static void main(String[] args) throws IOException {
// 設(shè)置 chrome 的路徑
System.setProperty(
"webdriver.chrome.driver",
"C:\\Documents and Settings\\sq\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe");
// 創(chuàng)建一個(gè) ChromeDriver 的接口,用于連接 Chrome
@SuppressWarnings("deprecation")
ChromeDriverService service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(
new File(
"E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe"))
.usingAnyFreePort().build();
service.start();
// 創(chuàng)建一個(gè) Chrome 的瀏覽器實(shí)例
WebDriver driver = new RemoteWebDriver(service.getUrl(),
DesiredCapabilities.chrome());
// 讓瀏覽器訪問 Baidu
driver.get("http://www.baidu.com");
// 用下面代碼也可以實(shí)現(xiàn)
// driver.navigate().to("http://www.baidu.com");
// 獲取 網(wǎng)頁(yè)的 title
System.out.println("1 Page title is: " + driver.getTitle());
// 通過 id 找到 input 的 DOM
WebElement element = driver.findElement(By.id("kw"));
// 輸入關(guān)鍵字
element.sendKeys("zTree");
// 提交 input 所在的 form
element.submit();
// 通過判斷 title 內(nèi)容等待搜索頁(yè)面加載完畢,Timeout 設(shè)置10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().endsWith("ztree");
}
});
// 顯示搜索結(jié)果頁(yè)面的 title
System.out.println("2 Page title is: " + driver.getTitle());
// 關(guān)閉瀏覽器
driver.quit();
// 關(guān)閉 ChromeDriver 接口
service.stop();
}
}
運(yùn)行一下看看,是不是一切OK了?
【2012.12.06補(bǔ)充】
上一個(gè) Demo 中無(wú)法正常使用 new ChromeDriver(),今天在做進(jìn)一步學(xué)習(xí)時(shí)看到一篇文章(http://qa.blog.163.com/blog/static/19014700220122231779/?),恍然大悟,原來只需要把 ‘webdriver.chrome.driver?’ 的值設(shè)置為 chromedriver 的路徑就可以了。
Java代碼
package lesson1;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExampleForChrome2 {
public static void main(String[] args) throws IOException {
// 設(shè)置 chrome 的路徑
System.setProperty(
"webdriver.chrome.driver",
"E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe");
// 創(chuàng)建一個(gè) ChromeDriver 的接口,用于連接 Chrome
// 創(chuàng)建一個(gè) Chrome 的瀏覽器實(shí)例
WebDriver driver = new ChromeDriver();
// 讓瀏覽器訪問 Baidu
driver.get("http://www.baidu.com");
// 用下面代碼也可以實(shí)現(xiàn)
// driver.navigate().to("http://www.baidu.com");
// 獲取 網(wǎng)頁(yè)的 title
System.out.println("1 Page title is: " + driver.getTitle());
// 通過 id 找到 input 的 DOM
WebElement element = driver.findElement(By.id("kw"));
// 輸入關(guān)鍵字
element.sendKeys("zTree");
// 提交 input 所在的 form
element.submit();
// 通過判斷 title 內(nèi)容等待搜索頁(yè)面加載完畢,Timeout 設(shè)置10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().endsWith("ztree");
}
});
// 顯示搜索結(jié)果頁(yè)面的 title
System.out.println("2 Page title is: " + driver.getTitle());
// 關(guān)閉瀏覽器
driver.quit();
// element = driver.findElement(By.id("kw"));
// // element.clear();
// element.click();
// element.clear();
// element.sendKeys("zTree");
// element.submit();
}
}
【4. 測(cè)試 IE】
想逃避 IE 嗎?? 作為前端開發(fā),IE 你是必須要面對(duì)的,沖吧!
其實(shí)你會(huì)發(fā)現(xiàn), Selenium 主要也就是針對(duì) FireFox 和 IE 來制作的,所以把 FireFox 的代碼修改為 IE 的,那是相當(dāng)?shù)娜菀?,只需要?jiǎn)單地兩步:
1)把 ExampleForFireFox.java 另存為 ExampleForIE.java
2)把 WebDriver driver = new FirefoxDriver(); 修改為 WebDriver driver = new InternetExplorerDriver();
3)一般大家的 IE都是默認(rèn)路徑吧,所以也就不用設(shè)置 property 了
Java代碼
package lesson1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExampleForIE {
public static void main(String[] args) {
// 如果你的 FireFox 沒有安裝在默認(rèn)目錄,那么必須在程序中設(shè)置
// System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
// 創(chuàng)建一個(gè) FireFox 的瀏覽器實(shí)例
WebDriver driver = new InternetExplorerDriver();
// 讓瀏覽器訪問 Baidu
driver.get("http://www.baidu.com");
// 用下面代碼也可以實(shí)現(xiàn)
// driver.navigate().to("http://www.baidu.com");
// 獲取 網(wǎng)頁(yè)的 title
System.out.println("1 Page title is: " + driver.getTitle());
// 通過 id 找到 input 的 DOM
WebElement element = driver.findElement(By.id("kw"));
// 輸入關(guān)鍵字
element.sendKeys("zTree");
// 提交 input 所在的 form
element.submit();
// 通過判斷 title 內(nèi)容等待搜索頁(yè)面加載完畢,Timeout 設(shè)置10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().endsWith("ztree");
}
});
// 顯示搜索結(jié)果頁(yè)面的 title
System.out.println("2 Page title is: " + driver.getTitle());
// 關(guān)閉瀏覽器
driver.quit();
}
}
運(yùn)行一下,是不是 so easy?
入門工作完成,現(xiàn)在完全可以利用 java 代碼,讓 Selenium 自動(dòng)執(zhí)行我們?cè)O(shè)置好的測(cè)試用例了,不過.....這僅僅是個(gè)開始。