菜鳥(niǎo)學(xué)自動(dòng)化測(cè)試(九)----WebDirver
關(guān)于什么是WebDirver,上一節(jié)做了簡(jiǎn)單的描述,環(huán)境也在上一章中搭建完成。
下面我們拷貝了官網(wǎng)提供的一個(gè)實(shí)例。讓其在我們的eclipse中運(yùn)行。
Selenium WebDirver 代碼如下:
package MySel20Proj;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Selenium2Example {
public static void main(String[] args) {
// 用Firefox driver創(chuàng)建一個(gè)新的的實(shí)例
//注意:其他的代碼依賴(lài)于界面
//不執(zhí)行
System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
WebDriver driver = new FirefoxDriver();// 這里我們可以使用firefox來(lái)運(yùn)行測(cè)試用例
//WebDriver driver = new ChromeDriver(); //這是chrome瀏覽器的驅(qū)動(dòng)
//WebDriver driver = new InternetExplorerDriver(); //這是IE瀏覽器的驅(qū)動(dòng)
// WebDriver driver = new HtmlUnitDriver(); //這是一個(gè)無(wú)界面測(cè)試模式,不用打開(kāi)瀏覽器,通過(guò)后臺(tái)輸入來(lái)判斷測(cè)試用例是否通過(guò)
// 現(xiàn)在用這個(gè)來(lái)訪問(wèn)谷歌
driver.get("http://www.google.com");
// 也可以用下面的方式訪問(wèn)谷歌
// driver.navigate().to("http://www.google.com");
// 找到文本輸入元件的名字
WebElement element = driver.findElement(By.name("q"));
// 在搜索框內(nèi)輸入“cheese!”
element.sendKeys("Cheese!");
// 現(xiàn)在遞交表格. WebDriver會(huì)發(fā)現(xiàn)我們的形式元素
element.submit();
// 后臺(tái)打印輸出,檢查網(wǎng)頁(yè)的標(biāo)題
System.out.println("Page title is: " + driver.getTitle());
// 谷歌的搜索是渲染過(guò)的動(dòng)態(tài)JavaScript. 等待頁(yè)面加載,暫停10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//關(guān)閉瀏覽器
driver.quit();
}
}
運(yùn)行時(shí)報(bào)出了錯(cuò)誤;
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP
Build info: version: '2.16.1', revision: '15405', time: '2012-01-05 12:30:12'
我們只要在WebDriver driver = new FirefoxDriver(); 前面指定我們?yōu)g覽器的具體信息即可:
System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
WebDriver driver = new FirefoxDriver();
WebDirver 的實(shí)現(xiàn):
驅(qū)動(dòng)名稱(chēng) |
對(duì)操作系統(tǒng)的支持 |
調(diào)用的接口 |
FireFox Driver |
ALL |
org.openqa.selenium.firefox.FirefoxDriver |
Chrome Driver |
ALL |
org.openqa.selenium.chrome.ChromeDriver |
InternetExplorer Driver |
Windows |
org.openqa.selenium.ie.InternetExplorerDriver |
HtmlUnit Driver |
ALL |
org.openqa.selenium.htmlunit.HtmlUnitDriver |
什么情況下選用WebDirver ?
(1)Selenium-1.0不支持的瀏覽器功能。
(2)multiple frames, multiple browser windows, popups, and alerts.
(3)頁(yè)面導(dǎo)航。
(4)下拉。
(5)基于AJAX的UI元素。
同樣,我們的selenium IDE也支持WebDriver類(lèi)型腳本的導(dǎo)出。
將我們錄制好的腳本 導(dǎo)出為junit(WebDriver) 類(lèi)型
下面代碼是我錄制的一個(gè)google搜索“selenium”關(guān)鍵安的操作:
package com.test.hzh;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Test1 {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
driver = new FirefoxDriver();
baseUrl = "http://www.google.com.hk/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys("selenium");
driver.findElement(By.name("btnK")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}