標籤:準備就緒 開啟 public set code 搜尋方塊 .com 入門 pos
Selenium2(webdirver)入門之環境搭建(Java版)
一、開發環境:
1、JDK1.6
2、Eclipse:Version: Kepler Service Release 1,:http://www.eclipse.org/downloads/
3、Selenium:selenium-java-2.52.0.zip,:http://docs.seleniumhq.org/download/
解壓selenium-java包,這個包裡麵包含四部分,如:
二、建立一個Java Project:
1、然後把上面解壓出來的檔案拷到建立的project目錄下,目錄結構如:
2、添加build path,項目目錄右鍵-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JARs
把libs檔案夾下的jar包全部添加上,再添加selenium-java-2.39.0和selenium-java-2.39.0-srcs
3、添加完之後目錄結構如,多了Referenced Libraries,這裡就是上面那一步添加進去的jar包:
4、關聯webdriver的源碼:
至此,環境工作準備就緒,下面來寫一個簡單的小例子。
三、在src下面建立測試類別,如:
代碼如下,主要是開啟百度,然後在搜尋方塊輸入glen,點擊搜尋按鈕,關閉瀏覽器。
1 package com.selenium.Glen; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.firefox.*; 7 8 public class TestHelloWorld { 9 10 public static void main(String[] args) {11 12 //如果Firefox瀏覽器沒有預設安裝在C盤,需要制定其路徑13 //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 14 WebDriver driver = new FirefoxDriver();15 driver.get("http://www.baidu.com/");16 17 driver.manage().window().maximize();18 19 WebElement txtbox = driver.findElement(By.name("wd"));20 txtbox.sendKeys("Glen");21 22 WebElement btn = driver.findElement(By.id("su"));23 btn.click();24 25 driver.close();26 27 }28 29 }
然後直接右鍵-->Run As-->Java Application就可以看到效果了
Selenium java 環境搭建