Selenium WebDriver-在Firefox瀏覽器上執行測試


在本節中,我們將學習如何在Firefox瀏覽器上執行Selenium測試指令碼。在繼續本節之前,先來了解一下Gecko Driver的基礎知識。

Gecko Driver是什麼?

Gecko一詞指的是由Mozilla基金會開發的Gecko瀏覽器引擎,它用作為Mozilla瀏覽器的一部分。
Gecko Driver是Selenium和Firefox瀏覽器中測試之間的連結。 它充當W3C WebDriver相容用戶端(Eclipse,Netbeans等)之間的代理,以與基於Gecko的瀏覽器(Mozilla Firefox)進行互動。

Marionette(下一代FirefoxDriver)預設從Selenium 3開啟。Selenium使用W3C Webdriver協定向GeckoDriver傳送請求,GeckoDriver將它們轉換為名為Marionette的協定。 即使使用的是舊版本的Firefox瀏覽器,Selenium 3也希望通過webdriver.gecko.driver設定驅動程式可執行檔案的路徑。

注意:Selenium 3已升級為現在使用Marionette驅動程式啟動Firefox驅動程式,而不是之前支援的預設初始化。

假如要實現以下一個測試用例,嘗試在Firefox瀏覽器中自動執行以下測試方案。

  • 啟動Firefox瀏覽器。
  • 開啟URL : www.tw511.com
  • 單擊「搜尋」文字框
  • 輸入值「Java教學」
  • 單擊「搜尋」按鈕。

在一個Java工程中建立第二個測試用例。

第1步 - 右鍵單擊「src」檔案夾,然後從 New -> Class 建立一個新的類檔案。
將類的名稱命名為 - 「Second」 ,然後單擊「完成」按鈕。

第2步 - 在瀏覽器中開啟URL:

然後根據當前正在使用的作業系統單擊相應的GeckoDriver下載版本。 在這裡下載適用於Windows 64位元的GeckoDriver版本。

下載的檔案將採用壓縮格式,將內容解壓縮到方便的目錄中(如:D:/software/webdriver)。

在編寫測試指令碼之前,先來了解如何在Selenium中初始化GeckoDriver。 有三種方法可以初始化GeckoDriver:

1. 使用DesiredCapabilities
首先,需要為Gecko Driver 設定系統屬性。

System.setProperty("webdriver.gecko.driver","D:\\software\\webdriver\\geckodriver.exe" );

下面是使用DesiredCapabilities類設定gecko驅動程式的程式碼。

DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
capabilities.setCapability("marionette",true);

以下是完整的範例程式碼 -

System.setProperty("webdriver.gecko.driver","D:\\software\\webdriver\\geckodriver.exe" );  
DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
capabilities.setCapability("marionette",true);  
WebDriver driver= new FirefoxDriver(capabilities);

2. 使用 marionette 屬性:

Gecko Driver也可以使用marionette屬性進行初始化。

System.setProperty("webdriver.firefox.marionette","D:\\software\\webdriver\\geckodriver.exe");

此方法不需要 DesiredCapabilities 的程式碼。

3. 使用Firefox選項:

Firefox 47或更高版本將 marionette 驅動程式作為遺留系統。 因此,可以使用Firefox選項呼叫 marionette 驅動程式,如下所示。

FirefoxOptions options = new FirefoxOptions();  
options.setLegacy(true);

第3步 - 編寫測試程式碼,為每個程式碼塊嵌入了注釋,以便清楚地解釋這些步驟。

package com.yiibai;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Second {
    public static void main(String[] args) {

        // System Property for Gecko Driver
        System.setProperty("webdriver.gecko.driver", "D:\\software\\WebDriver\\geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");

        WebDriver driver = (WebDriver) new FirefoxDriver();

        // Launch Website
        driver.navigate().to("https://www.tw511.com/");

        // Click on the Custom Search text box and send value
        driver.findElement(By.name("kw")).sendKeys("java教學");
        driver.findElement(By.id("submit")).click();

        // Click on the Search button
        driver.findElement(By.className("article-list-item-txt")).click();
    }
}

第4步 - 右鍵單擊Eclipse程式碼,然後選擇:Run As -> Java Application

第5步 - 上述測試指令碼的輸出將顯示在Firefox瀏覽器中。