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.zyiz.net
  • 单击“搜索”文本框
  • 输入值“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.zyiz;

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("http://www.zyiz.net/");

        // 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浏览器中。


上一篇:Selenium WebDriver在Chrome浏览器上运行测试

下一篇:Selenium WebDriver-在IE浏览器上运行测试

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程