最近在研究Java+selenium2的自動(dòng)化測(cè)試,網(wǎng)上的資料比較多,自己學(xué)習(xí)的同時(shí)也順便記錄一下,加深印象的同時(shí)也方便以后回顧。
一、開發(fā)環(huán)境:
1、JDK1.6
2、Eclipse:Version: Kepler Service Release 1,下載地址:http://www.eclipse.org/downloads/
3、Selenium:selenium-java-2.39.0.zip,下載地址:http://code.google.com/p/selenium/downloads/list
解壓selenium-java包,這個(gè)包里面包含四部分,如下圖:
二、新建一個(gè)Java Project:
1、然后把上面解壓出來(lái)的文件拷到新建的project目錄下,目錄結(jié)構(gòu)如下圖:
2、添加build path,項(xiàng)目目錄右鍵-->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、添加完之后目錄結(jié)構(gòu)如下圖,多了Referenced Libraries,這里就是上面那一步添加進(jìn)去的jar包:
4、關(guān)聯(lián)webdriver的源碼:
至此,環(huán)境工作準(zhǔn)備就緒,下面來(lái)寫一個(gè)簡(jiǎn)單的小例子。
三、在src下面新建測(cè)試類,如下圖:
代碼如下,主要是打開百度,然后在搜索框輸入glen,點(diǎn)擊搜索按鈕,關(guān)閉瀏覽器。
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 //如果火狐瀏覽器沒有默認(rèn)安裝在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 }
然后直接右鍵TestHelloWorld.java-->Run As-->Java Application就可以看到效果了。
聯(lián)系客服