您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源功能測(cè)試工具 > Selenium
自動(dòng)化測(cè)試工具 Selenium WebDriver 入門
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2014/1/9 9:30:07 ] 推薦標(biāo)簽:Selenium WebDriver 測(cè)試工具

【2. 測(cè)試 FireFox】
Selenium 初是在 FireFox 上做起來的插件,所以我們先來搭建 FireFox 的環(huán)境。
確保你正確安裝了 FireFox 后,可以直接編寫 java 代碼測(cè)試嘍。

在 lesson1 目錄下建立 ExampleForFireFox.java
(因?yàn)閲?guó)內(nèi)不少朋友訪問 google 的時(shí)候會(huì)出問題,所以我把代碼中的 google 變成 baidu 了)

01 packagelesson1;
02 
03 importorg.openqa.selenium.By;
04 importorg.openqa.selenium.WebDriver;
05 importorg.openqa.selenium.WebElement;
06 importorg.openqa.selenium.firefox.FirefoxDriver;
07 importorg.openqa.selenium.support.ui.ExpectedCondition;
08 importorg.openqa.selenium.support.ui.WebDriverWait;
09 
10 publicclassExampleForFireFox  {
11     publicstaticvoidmain(String[] args) {
12         // 如果你的 FireFox 沒有安裝在默認(rèn)目錄,那么必須在程序中設(shè)置
13 //      System.setProperty("webdriver.firefox.bin", "D:\Program Files\Mozilla Firefox\firefox.exe");
14         // 創(chuàng)建一個(gè) FireFox 的瀏覽器實(shí)例
15         WebDriver driver =newFirefoxDriver();
16 
17         // 讓瀏覽器訪問 Baidu
18         driver.get("http://www.baidu.com");
19         // 用下面代碼也可以實(shí)現(xiàn)
20         // driver.navigate().to("http://www.baidu.com");
21 
22         // 獲取 網(wǎng)頁的 title
23         System.out.println("1 Page title is: "+ driver.getTitle());
24 
25         // 通過 id 找到 input 的 DOM
26         WebElement element = driver.findElement(By.id("kw"));
27 
28         // 輸入關(guān)鍵字
29         element.sendKeys("zTree");
30 
31         // 提交 input 所在的  form
32         element.submit();
33         
34         // 通過判斷 title 內(nèi)容等待搜索頁面加載完畢,間隔10秒
35         (newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>() {
36             publicBoolean apply(WebDriver d) {
37                 returnd.getTitle().toLowerCase().endsWith("ztree");
38             }
39         });
40 
41         // 顯示搜索結(jié)果頁面的 title
42         System.out.println("2 Page title is: "+ driver.getTitle());
43         
44         //關(guān)閉瀏覽器
45         driver.quit();
46     }
47 }

普通情況下,直接運(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ù))

上一頁1234下一頁
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd