Selenium Webdriver概述(转)
Selenium Webdriver
https://www.yiibai.com/selenium/selenium_overview.html#
?
webdriver自動化俗稱Selenium 2.0測試Web應用程序工具。 webdriver使用不同的底層框架,Selenium 遙控器使用JavaScript的Selenium 核嵌入式已經(jīng)在有一定的局限性的瀏覽器中。 webdriver直接交互而不與Selenium 遠程控制,依賴于服務器上的任何中介的瀏覽器。它是用在以下方面:
在Selenium開發(fā)者社區(qū)努力下,不斷提高Selenium webdriver與Selenium的整合。
-
MULT瀏覽器測試,包括對不能很好地支持Selenium的遠程控制瀏覽器改進的功能(硒1.0)
-
處理多個幀,多個瀏覽器窗口,彈出窗口和警報。
-
復雜的頁面導航。
-
高級用戶導航,如拖動和拖放。
-
基于AJAX的UI元素
體系結構
webdriver最好用一個簡單的架構圖,說明,如下圖所示。
Selenium RC VS webdriver
| Selenium RC的結構復雜,因為服務器需要啟動在開始試運行前。 | webdriver架構比Selenium RC簡單,因為它控制著從操作系統(tǒng)層面的瀏覽器。 |
| Selenium服務器充當瀏覽器和Selenese的命令之間的中間人 | webdriver直接相互作用,以在瀏覽器和使用瀏覽器的引擎進行控制。 |
| Selenium RC的腳本執(zhí)行速度較慢,因為它使用了Javascript來與RC互動 | webdriver的速度更快,因為它直接交互使用的瀏覽器。 |
| Selenium RC不能支持無頭,因為它需要一個真正的瀏覽器一起工作。 | webdriver可以支持無頭執(zhí)行 |
| 它是一個簡單的API | 復雜,API相比,RC有點大 |
| 減面向對象的API | 純粹的面向對象的API |
| 不能測試移動應用程序 | 可測試iPhone/Android應用程序 |
使用webdriver腳本
讓我們了解webdriver如何工作。為了演示目的,我們將使用http://www.calculator.net/。我們將執(zhí)行“百分比計算器”,這是位于“數(shù)學計算器”。我們已經(jīng)下載了所需要webdriver的JAR。請參閱環(huán)境設置一章。
第1步:從提取Eclipse文件夾中啟動“Eclipse”。
第2步:點擊“Browse”按鈕選擇工作區(qū)。
第3步:現(xiàn)在,創(chuàng)建“New Project”,從“File”菜單。
第4步:輸入項目名稱,然后單擊“Next”。
第五步:進入Libraries選項卡,并選中所有的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)在命名類,并讓它設置為main方法
第9步:類概要如下所示。
步驟10:現(xiàn)在是時候編寫代碼了。下面的腳本更容易理解,因為它清楚地解釋了一步,在嵌入的注釋步驟。請看看“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 exceptiondriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//Launch websitedriver.navigate().to("http://www.calculator.net/");//Maximize the browserdriver.manage().window().maximize();// Click on Math Calculatorsdriver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();// Click on Percent Calculatorsdriver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();// Enter value 10 in the first number of the percent Calculatordriver.findElement(By.id("cpar1")).sendKeys("10");// Enter value 50 in the second number of the percent Calculatordriver.findElement(By.id("cpar2")).sendKeys("50");// Click Calculate Buttondriver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();// Get the Result Text based on its xpathString result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();//Print a Log In message to the screenSystem.out.println(" The Result is " + result);//Close the Browser.driver.close(); } }第11步:以上腳本的輸出將被打印在控制臺。
最常用的命令
下表列出了webdriver的最常用的命令以及它的語法,這將有助于我們開發(fā)webdriver腳本。
| driver.get("URL") | 導航到應用程序 |
| element.sendKeys("inputtext") | 輸入一些文本輸入框 |
| element.clear() | 從輸入框清空內容 |
| select.deselectAll() | 這將取消選擇頁面上的第一個選擇所有選項: |
| 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. |
轉載于:https://www.cnblogs.com/it-tsz/p/9814309.html
總結
以上是生活随笔為你收集整理的Selenium Webdriver概述(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷.4897.[模板]最小割树(Din
- 下一篇: 【NOIP模拟】方格稿纸