Selenium教程

WebDriver Firefox上运行测试

在本节中,我们将学习如何在Firefox浏览器上运行Selenium测试脚本。
在继续本节之前,让我们首先了解Gecko Driver的基础。

什么是Gecko Driver?

术语Gecko指的是Gecko。由Mozilla Foundation作为Mozilla浏览器的一部分开发的浏览器引擎。
Gecko驱动程序充当Selenium中的测试与Firefox浏览器之间的链接。它充当W3C WebDriver兼容客户端(Eclipse,Netbeans等)之间的代理,以与基于Gecko的浏览器(Mozilla Firefox)进行交互。
木偶(下一代FirefoxDriver)由Selenium的默认设置。Selenium使用W3C Webdriver协议将请求发送到GeckoDriver,后者将其转换为名为Marionette的协议。即使您使用的是旧版本的Firefox浏览器,Selenium 3仍希望您设置 webdriver.gecko.driver 可执行驱动程序的路径。
注意: Selenium 3已升级,现在可以使用Marionette驱动程序而不是先前支持的默认初始化来启动Firefox驱动程序。
让我们考虑一个测试案例,在该案例中,我们将尝试在Firefox浏览器中自动执行以下方案。
启动Firefox浏览器。 打开URL: www.lidihuo.com 点击"自定义搜索"文本框 输入值" Java" 单击"搜索"按钮。
我们将在同一测试套件(Demo_Test)中创建第二个测试用例。
Step1、右键单击" src"文件夹,然后从"新建">"类"中创建一个新的类文件。
将您的类名命名为"第二",然后单击"完成"按钮。

Selenium WebDriver在Firefox浏览器Gecko驱动程序上的运行测试
Selenium WebDriver在Firefox浏览器Gecko驱动程序上的运行测试
第二步。打开URL: https://github.com/mozilla/geckodriver/releases 在浏览器中,然后根据您当前正在使用的操作系统,单击相应版本以下载GeckoDriver。在这里,我们正在下载Windows版GeckoDriver的64位版本。

Selenium WebDriver在Firefox浏览器Gecko驱动程序上的运行测试
下载的文件将采用压缩格式。将内容解压缩到一个方便的目录中。

Selenium WebDriver在Firefox浏览器上运行测试Gecko驱动程序
在编写测试脚本之前,让我们首先了解如何在Selenium中初始化GeckoDriver。有三种初始化GeckoDriver的方法:

1、使用所需功能

首先,我们必须为Gecko Driver设置系统属性。
System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
下面是使用DesiredCapabilities类设置壁虎驱动程序的代码。
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette",true);
这是完整的代码:
System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette",true);
        WebDriver driver= new FirefoxDriver(capabilities);

2、使用木偶属性:

Gecko驱动程序也可以使用木偶属性进行初始化。
System.setProperty("webdriver.firefox.marionette","D:\\GeckoDriver\\geckodriver.exe");
此方法不需要所需功能的代码。

3、使用Firefox选项:

Firefox 47或更高版本将木偶驱动程序作为旧版系统。因此,可以使用Firefox选项调用木偶驱动程序,如下所示。
FirefoxOptions options = new FirefoxOptions();
    options.setLegacy(true);
Step3、现在该进行编码了。我们在每个代码块中都嵌入了注释,以清楚地说明这些步骤。
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:\\GeckoDriver\\geckodriver.exe" );
        
          // Initialize Gecko Driver using Desired Capabilities Class
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette",true);
    WebDriver driver= new FirefoxDriver(capabilities);
        
         // Launch Website
    driver.navigate().to("http://www.lidihuo.com/");
        
        // Click on the Custom Search text box and send value
    driver.findElement(By.id("gsc-i-id1")).sendKeys("Java");
        
        // Click on the Search button
driver.findElement(By.className("gsc-search-button gsc-search-buttonv2")).click();   
        }
}
Eclipse代码窗口将如下所示:

Selenium WebDriver在Firefox浏览器Gecko驱动程序上的运行测试
Step4、右键单击Eclipse代码,然后选择运行方式> Java应用程序。

Selenium WebDriver在Firefox浏览器Gecko驱动程序上运行测试
Step5、以上测试脚本的输出将显示在Firefox浏览器中。

Selenium WebDriver在Firefox浏览器Gecko驱动程序上运行测试
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4