selenium 2.0 webdriver api

技術文章翻譯:selenium 2.0 webdriver api

created by linyuxinqing at 2018-1-10

轉載請註明出處

原文地址:http://www.seleniumhq.org/docs/03_webdriver.jsp

本人已將原文也轉載過來了,附上地址:,有興趣的可以移步。下面是翻譯正文:

注意:我們目前還在編輯這些章節。我們相信這裏的信息是準確無誤的,但請知曉我們仍然在完善這篇文檔。隨着我們工作的開展,附加信息會被添加進來,以使本章更加精確。

1、WebDriver介紹:
        Selenium 2.0主要的新功能就是WebDriver API的集成。WebDriver目的是提供一個更簡單,更簡明的編程接口,也爲了addressing一些Selenium-RC API的限制。Selenium-WebDriver是爲了更好的支持動態網頁,這些動態網頁的元素可能在頁面本身沒有被重新加載的情況發生改變。WebDriver的目標是提供一個設計良好、面向對象的API,這個API爲現代的高級網頁應用測試遇到的問題提供更完善地支持。
2、相比於Selenium-RC,WebDriver是怎樣驅動瀏覽器的:
        Selenium-WebDriver使用各個瀏覽器自身對自動化的支持技術直接調用瀏覽器。這些直接是怎樣做的呢,他們支持的特徵取決於你所使用的瀏覽器。每個瀏覽器驅動的信息會在本章稍後的內容中提供。
對於熟悉Selenium-RC的用戶,這種方式與他們曾經使用的有很大不同。Selenium-RC對於每個瀏覽器都使用相同的方式。當瀏覽器加載時,它將javascript函數注入到瀏覽器中,然後就使用這些javascript在瀏覽器中驅動AUT。WebDriver沒有使用這個技術。再次說明:它通過各個瀏覽器對自動化的內建支持,直接驅動瀏覽器。
3、WebDriver和Selenium-Server:
        你可能需要Selenium-Server,也可能用不上,這取決於你想怎樣使用Selenium-WebDriver。如果你的瀏覽器和測試腳本都在同一臺及其上面運行,並且你的測試腳本僅僅使用了WebDriver API,那你根本不需要。WebDriver會直接運行瀏覽器。
下面是一些你需要同時使用Selenium-Server和Selenium-WebDriver的理由:
(1)、你正在使用Selenium-Grid在多臺機器或多臺虛擬機之間分發你的測試腳本。
(2)、你想要連接到一臺裝有特殊版本的瀏覽器的遠程機器,而你自己的電腦上沒有這個版本
(3)、你沒有使用Java綁定,並且想使用HtmlUnit 驅動
4、建立一個Selenium-WebDriver的工程:
        安裝Selenium,意味着在一個開發環境建立一個工程,因此你能使用Selenium來編程。怎樣去安裝則取決於你選擇的編程語言和你的開發環境。
Java

使用Maven是建立一個Selenium 2.0 Java工程最簡單的方式。Maven會下載java 綁定。Selenium 2.0 java客戶端庫,以及所有的依賴,並且會替你創建好一個使用pom.xml文件的工程。一旦你完成了這個工作,你就能將maven工程引入到你喜歡的IDE中,IntelliJ IDEA或者Eclipse。

    首先,新建一個文件夾來存放你的Selenium工程文件。要使用maven,你還需要一個pom.xml文件。可以使用文本編輯器來創建。這裏不會講解pom.xml或使用maven的細節,這些東西在對應的網站上已經講解的足夠優秀。你的pom.xml文件應該長這樣。在工程中的這個。文件夾下新建這個文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>MySel20Proj</groupId>
        <artifactId>MySel20Proj</artifactId>
        <version>1.0</version>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-server</artifactId>
                <version>3.0.1</version>
            </dependency>
        </dependencies>
</project>
首先確定你使用的是最新的版本。上面列出的版本號是在編寫這篇文檔的時候最新的,但在Selenium 2.0發佈後,頻繁的發佈。在Maven下載頁面可以獲取最新的版本,相應地將上面的依賴修改就好了。
現在,打開命令行工具,CD到你的工程所在目錄,運行下面的命令:mvn clean install。這樣就會下載Selenium以及它所有的依賴,並且將他們都添加到工程中。
        最後,將這個工程引入到你最喜歡的編程環境中。如果不是很熟悉這個過程的,這裏有相關鏈接:
Importing a maven project into IntelliJ IDEA. Importing a maven project into Eclipse.
5、從Selenium 1.0遷移到2.0:
        對於之前使用Selenium 1.0編寫測試集的用戶,我們也提供了怎樣將現有代碼遷移到2.0的小技巧。Selenium 2.0的首席工程師,Simon Stewart,編寫了一篇關於如何從Selenium 1.0遷移的文章。這裏也附上鍊接:
Migrating From Selenium RC to Selenium WebDriver(譯者注:這篇也需要翻牆,也是英文,如果有需要,後面會把原文轉載過來,有時間再翻譯)
6、通過例子來介紹Selenium-WebDriver的API:
        WebDriver是網頁應用程序自動化測試工具,特別是用於驗證它們是否按照預期設定在工作。它旨在提供一個易於瀏覽、易於理解的友好的API。希望它比Selenium-RC(1.0)API易於使用,能使你的測試腳本易於閱讀和維護。它本身並沒有與任何其他測試框架綁定,因此既能很好地用於某個單元測試工程,也能很好地用在原始的main方法中。這個章節介紹WebDriver的API,以期幫助你逐漸熟悉它。如果你還沒一個WebDriver工程,現在開始創建。創建的方法在前一章節中有講到:
當你新建好工程後,你會發現WebDriver跟其他一般的庫沒有區別:它完全自包含,在使用它之前,通常不需要像Selenium-RC的代理服務器一樣,啓動其他進程,或安裝其他東西。
注意:要使用ChromeDriver, Opera Driver, Android Driver and iOS Driver,你需要做其他準備工作。
現在,你已經可以編寫一些代碼了。最簡單的方式就是跟着下面這個例子,先在Google上搜索“Cheese”,然後將搜索結果頁面的標題輸入到控制檯。
下面是java代碼實現:
package org.openqa.selenium.example;


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.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();


        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");


        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));


        // Enter something to search for
        element.sendKeys("Cheese!");


        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();


        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (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());
        
        //Close the browser
        driver.quit();
    }
}

未完待續。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章