webdriver自動化俗稱Selenium 2.0測試Web應(yīng)用程序工具。 webdriver使用不同的底層框架,Selenium 遙控器使用JavaScript的Selenium 核嵌入式已經(jīng)在有一定的局限性的瀏覽器中。 webdriver直接交互而不與Selenium 遠(yuǎn)程控制,依賴于服務(wù)器上的任何中介的瀏覽器。它是用在以下方面:
在Selenium開發(fā)者社區(qū)努力下,不斷提高Selenium webdriver與Selenium的整合。
MULT瀏覽器測試,包括對不能很好地支持Selenium的遠(yuǎn)程控制瀏覽器改進(jìn)的功能(硒1.0)
處理多個幀,多個瀏覽器窗口,彈出窗口和警報。
復(fù)雜的頁面導(dǎo)航。
高級用戶導(dǎo)航,如拖動和拖放。
基于AJAX的UI元素
webdriver最好用一個簡單的架構(gòu)圖,說明,如下圖所示。
| Selenium RC | Selenium WebDriver |
|---|---|
| Selenium RC的結(jié)構(gòu)復(fù)雜,因?yàn)榉?wù)器需要啟動在開始試運(yùn)行前。 | webdriver架構(gòu)比Selenium RC簡單,因?yàn)樗刂浦鴱牟僮飨到y(tǒng)層面的瀏覽器。 |
| Selenium服務(wù)器充當(dāng)瀏覽器和Selenese的命令之間的中間人 | webdriver直接相互作用,以在瀏覽器和使用瀏覽器的引擎進(jìn)行控制。 |
| Selenium RC的腳本執(zhí)行速度較慢,因?yàn)樗褂昧薐avascript來與RC互動 | webdriver的速度更快,因?yàn)樗苯咏换ナ褂玫臑g覽器。 |
| Selenium RC不能支持無頭,因?yàn)樗枰粋€真正的瀏覽器一起工作。 | webdriver可以支持無頭執(zhí)行 |
| 它是一個簡單的API | 復(fù)雜,API相比,RC有點(diǎn)大 |
| 減面向?qū)ο蟮腁PI | 純粹的面向?qū)ο蟮腁PI |
| 不能測試移動應(yīng)用程序 | 可測試iPhone/Android應(yīng)用程序 |
讓我們了解webdriver如何工作。為了演示目的,我們將使用http://www.calculator.net/。我們將執(zhí)行“百分比計(jì)算器”,這是位于“數(shù)學(xué)計(jì)算器”。我們已經(jīng)下載了所需要webdriver的JAR。請參閱環(huán)境設(shè)置一章。
第1步:從提取Eclipse文件夾中啟動“Eclipse”。
第2步:點(diǎn)擊“Browse”按鈕選擇工作區(qū)。
第3步:現(xiàn)在,創(chuàng)建“New Project”,從“File”菜單。
第4步:輸入項(xiàng)目名稱,然后單擊“Next”。
第五步:進(jìn)入Libraries選項(xiàng)卡,并選中所有的JAR包文件,我們已經(jīng)下載(請參閱環(huán)境搭建章)。添加引用Selenium webdriver的庫文件夾中的所有JAR,selenium-java-2.42.2.jar和selenium-java-2.42.2-srcs.jar
第6步:如下圖所示創(chuàng)建包。
第7步:現(xiàn)在,讓我們創(chuàng)建一個通過執(zhí)行'Class'右鍵單擊程序包,然后選擇“New”>>“Class”
第8步:現(xiàn)在命名類,并讓它設(shè)置為main方法
第9步:類概要如下所示。
步驟10:現(xiàn)在是時候編寫代碼了。下面的腳本更容易理解,因?yàn)樗宄亟忉屃艘徊剑谇度氲淖⑨尣襟E。請看看“Locators”一章,了解如何捕捉對象的屬性。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class webdriverdemo
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
//Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.navigate().to("http://www.calculator.net/");
//Maximize the browser
driver.manage().window().maximize();
// Click on Math Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();
// Click on Percent Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();
// Enter value 10 in the first number of the percent Calculator
driver.findElement(By.id("cpar1")).sendKeys("10");
// Enter value 50 in the second number of the percent Calculator
driver.findElement(By.id("cpar2")).sendKeys("50");
// Click Calculate Button
driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();
// Get the Result Text based on its xpath
String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();
//Print a Log In message to the screen
System.out.println(" The Result is " + result);
//Close the Browser.
driver.close();
}
}
第11步:以上腳本的輸出將被打印在控制臺。
下表列出了webdriver的最常用的命令以及它的語法,這將有助于我們開發(fā)webdriver腳本。
| Commmand | 描述 |
|---|---|
| driver.get("URL") | 導(dǎo)航到應(yīng)用程序 |
| element.sendKeys("inputtext") | 輸入一些文本輸入框 |
| element.clear() | 從輸入框清空內(nèi)容 |
| select.deselectAll() | 這將取消選擇頁面上的第一個選擇所有選項(xiàng): |
| select.selectByVisibleText("some text") | select the OPTION with the input specified by the user. |
| driver.switchTo().window("windowName") | Moving the focus from one window to another |
| driver.switchTo().frame("frameName") | swing from frame to frame |
| driver.switchTo().alert() | Helps in handling alerts |
| driver.navigate().to("URL") | Navigate to the URL |
| driver.navigate().forward() | To Navigate forward |
| driver.navigate().back() | To Navigate back |
| driver.close() | Closes the current Browser associated with the driver |
| driver.quit() | Quits the driver and closes all the associated window of that driver. |
| driver.refresh() | Refreshes the current page. |