【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 了
01 packagelesson1;
02
03 importorg.openqa.selenium.By;
04 importorg.openqa.selenium.WebDriver;
05 importorg.openqa.selenium.WebElement;
06 importorg.openqa.selenium.ie.InternetExplorerDriver;
07 importorg.openqa.selenium.support.ui.ExpectedCondition;
08 importorg.openqa.selenium.support.ui.WebDriverWait;
09
10 publicclassExampleForIE {
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 =newInternetExplorerDriver();
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)行一下,是不是 so easy?
入門工作完成,現(xiàn)在完全可以利用 java 代碼,讓 Selenium 自動(dòng)執(zhí)行我們?cè)O(shè)置好的測(cè)試用例了,不過.....這僅僅是個(gè)開始。