Selenium2(java)環境搭建
1.下載JDK
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
2.配置java的環境變量,比較簡單,大家可以百度到比較完整配置環境變量的文檔:
http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef.html
3.下載eclipse
https://www.eclipse.org/downloads/packages/release/luna/sr2
4.下載selenium2相關類庫,版本selenium-2.48.2
鏈接: http://pan.baidu.com/s/1THNoa 密碼: 75cn
5.將selenium類庫引入eclipse
解壓4下載下來的壓縮包
打開eclipse,新建用戶自定義類庫:
Windos --> Preference --> Java --> Build path --> User Libraries
新建自定義類庫,命名為selenuim
導入selenium類庫,將selenium-2.48.2下面的jar包和libs下面的jar包全都導入
點擊OK,selenium開發環境基本搭建完畢。
6.一個簡單的例子
新建Java Project,命名為seleniumTest,將5中自定義的類庫導入seleniumTest中:
右鍵seleniumTes –-> Build Path –-> Add Libraries –-> User Library –-> Next --> 勾選selenuim –-> Finish
新建一個Java類,類名為:FirstCase
具體代碼如下:
package seleniumTest; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; public class FirstCase { public static void main(String[] args) { // TODO Auto-generated method stub //聲明一個firefox driver對象 WebDriver driver = new FirefoxDriver(); //打開sougo driver.get("http://www.sogou.com"); //定位搜索框 WebElement searchInput = driver.findElement(By.name("query")); //搜索框輸入關鍵字 searchInput.sendKeys("selenium"); //定位搜索按鈕 WebElement searchButton = driver.findElement(By.id("stb")); //點擊搜索按鈕 searchButton.click(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } //斷言搜索結果頁面 WebElement keywordInput = driver.findElement(By.id("upquery")); Assert.assertEquals(keywordInput.getAttribute("value"), "selenium"); //關閉瀏覽器 driver.quit(); } }