selenium火狐驱动_在Selenium Firefox驱动程序上运行测试

Selenium 3.0引入了GeckoDriver,用于与Firefox交互。Gecko是Mozilla开发的浏览器引擎,而GeckoDriver是Selenium与Firefox之间的桥梁。在没有GeckoDriver的情况下,Selenium测试会抛出异常。要使用GeckoDriver,需要下载对应操作系统的版本,设置系统路径或环境变量,然后通过System.setProperty初始化。
摘要由CSDN通过智能技术生成

selenium火狐驱动

带有Selenium 3.0的Gecko Marionette Firefox驱动程序 (Gecko Marionette Firefox Driver with Selenium 3.0)

Many of you know that before Selenium 3, Mozilla Firefox browser was the default browser for Selenium where we launch Firefox by default without any property settings. After Selenium 3.0 has been released in the market and in this version of Selenium, testers need to initialize the script to use Firefox using GeckoDriver explicitly.

你们中的许多人都知道,在Selenium 3之前,Mozilla Firefox浏览器是Selenium的默认浏览器,在默认情况下,我们在没有任何属性设置的情况下启动Firefox。 在市场上以及此版本的Selenium中发布Selenium 3.0之后,测试人员需要使用GeckoDriver显式初始化脚本以使用Firefox。

什么是壁虎? (What is Gecko?)

Gecko is a web browser engine which is developed by Mozilla Foundation and written in C++. It is an Open Source. Web browser engine is a software program that is used to control and render the content (like HTML, CSS, XML, images) on the browser.

Gecko是由Mozilla Foundation开发并用C ++编写的Web浏览器引擎 。 它是一个开源的。 Web浏览器引擎是一种软件程序,用于控制和呈现浏览器上的内容(如HTML,CSS,XML,图像)。

什么是壁虎驱动程序? (What is Gecko Driver?)

Gecko is a Web Browser engine used in various applications developed by Mozilla Foundation and the Mozilla Corporation. It is a proxy which is used to interact with the browsers that run on Gecko-based browsers like Firefox. Gecko driver is the link between your tests in Selenium WebDriver and Mozilla Firefox browser. As Selenium 3.0 will not have any native implementation of Firefox, we need to direct all the driver commands through Gecko Driver. Gecko Driver is an executable file that you need to have in one of the system paths before running your tests.
The Firefox browser implements the WebDriver protocol using an executable called GeckoDriver.exe. Selenium uses WebDriver protocol to send requests to GeckoDriver, which translates calls into the Marionette automation protocol and Firefox will understand the commands transmitted in the form of Marionette protocol and executes them.

Gecko是Web浏览器引擎,可用于Mozilla Foundation和Mozilla Corporation开发的各种应用程序。 它是一个代理,用于与在基于Gecko的浏览器(如Firefox)上运行的浏览器进行交互。 Gecko驱动程序是Selenium WebDriver和Mozilla Firefox浏览器之间的测试之间的链接。 由于Selenium 3.0将不具有Firefox的任何本机实现,因此我们需要通过Gecko Driver定向所有驱动程序命令。 Gecko驱动程序是一个可执行文件,在运行测试之前,您需要将其放置在系统路径之一中。
Firefox浏览器使用名为GeckoDriver.exe的可执行文件来实现WebDriver协议。 Selenium使用WebDriver协议将请求发送到GeckoDriver,后者将呼叫转换为Marionette自动化协议,Firefox将理解以Marionette协议形式传输的命令并执行它们。

GeckoDriver

GeckoDriver

壁虎驱动程序

没有GeckoDriver.exe的Selenium 3测试 (Selenium 3 Test without GeckoDriver.exe)

Let’s see what happens, if we run the Selenium test without GeckoDriver. It was a very straight forward process where you were not required to use GeckoDriver. You just write the code to instantiate the WebDriver and open Firefox browser. Below is the sample program to launch a Firefox browser with Selenium 2.0

让我们看看如果在没有GeckoDriver的情况下运行Selenium测试会发生什么。 这是一个非常简单的过程,不需要您使用GeckoDriver。 您只需编写代码以实例化WebDriver并打开Firefox浏览器。 以下是使用Selenium 2.0启动Firefox浏览器的示例程序


package com.journaldev.selenium.Firefox;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GeckoDriver {

    public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://journaldev.com");
    String PageTitle = driver.getTitle();
    System.out.println("Page Title is:" + PageTitle);
    }
 }

When you run above program we get an exception called java.lang.IllegalStateException. which tells The path to the driver executable must be set by webdriver.gecko.driver.

当您在上述程序上运行时,我们将得到一个名为java.lang.IllegalStateException的异常 告诉驱动程序可执行文件的路径必须由webdriver.gecko.driver设置。

Exception

Exception

例外

To overcome the above problem we need to download the GeckoDriver in order to work with selenium commands which we are writing on Mozilla. Every browser as a driver. The driver for Mozilla is the GeckoDriver. The selenium commands will be interpreted by GeckoDriver and it will be executed on Mozilla.

为了克服上述问题,我们需要下载GeckoDriver才能使用我们在Mozilla上编写的Selenium命令。 每个浏览器都作为驱动程序。 Mozilla的驱动程序是GeckoDriver。 Selenium命令将由GeckoDriver解释,并将在Mozilla上执行。

下载并安装Gecko驱动程序 (Download and Install Gecko Driver)

Gecko Driver is available as an executable file that starts as a server on your system. The following are the steps to download Gecko Driver.

Gecko驱动程序作为可执行文件提供,该文件作为系统上的服务器启动。 以下是下载Gecko驱动程序的步骤。

  • Step 1: Go to the Eclipse official website and select appropriate version for GeckoDriver based on your operating system

    步骤1 :转到Eclipse官方网站 ,然后根据您的操作系统为GeckoDriver选择适当的版本
  • Note: Here we are working on Windows Operating system, we need to download the corresponding Gecko driver of Windows version. If your Operating System is Linux or Mac then you need to download the corresponding Gecko driver.

    注意:在这里我们正在Windows操作系统上工作,我们需要下载相应的Windows版本的Gecko驱动程序。 如果您的操作系统是Linux或Mac,则需要下载相应的Gecko驱动程序。

    GeckoDriver Version

    GeckoDriver Version

    GeckoDriver版本

  • Step 2: Once the ZIP file download is complete, extract the ZIP file

    步骤2 :ZIP文件下载完成后,解压缩ZIP文件
  • GeckoDriver Extraction

    GeckoDriver Extraction

    GeckoDriver提取

  • Step 3: Note the location where you extracted the geckodriver. Location will be later used to instantiate the driver

    步骤3 :记下提取geckodriver的位置。 位置将在以后用于实例化驱动程序
  • GeckoDriver Location

    GeckoDriver Location

    GeckoDriver位置

    初始化GeckoDriver的不同方法: (Different ways to initialize GeckoDriver:)

    There are 2 methods to initialize GeckoDriver.

    有两种初始化GeckoDriver的方法。

    设置Gecko驱动程序的系统属性:- (Set System Properties for Gecko Driver:-)

    Code to set the System properties is

    设置系统属性的代码是

    System.setProperty(“webdriver.gecko.driver”,“Path to geckodriver.exe”);

    The complete program to launch the GeckoDriver will be like this:

    启动GeckoDriver的完整程序将如下所示:

    
    package com.journaldev.selenium.Firefox;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class GeckoDriver {
    
        public static void main(String[] args) {
        System.setproperty("Webdriver.gecko.driver","D:\\Drivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://journaldev.com");
        String PageTitle = driver.getTitle();
        System.out.println("Page Title is:" + PageTitle);
        driver.close();
        }
     }
    

    When you run the above program you will notice that Journaldev.com is opened in the new Firefox window and it will print the website title in the console.

    当您运行上述程序时,您会注意到在新的Firefox窗口中打开Journaldev.com,它将在控制台中打印网站标题。

    在Windows环境变量中设置Gecko驱动程序路径: (Setting Gecko Driver Path in Windows Environment Variables:-)

  • Step 1: Go to My Computer and Right click to get the context menu.

    步骤1 :转到我的电脑,然后右键单击以获取上下文菜单。
  • MyComputer Properties

    MyComputer Properties

    MyComputer属性

  • Step 2: Click on the Change Setting on the opened window.

    步骤2 :在打开的窗口中单击“ 更改设置 ”。
  • Change Settings

    Change Settings

    更改设置

  • Step 3: Click on Advance tab and click on Environment Variables.

    步骤3 :单击高级选项卡,然后单击环境变量。
  • Environment Variables

    Environment Variables

    环境变量

  • Step 4: Select Path under System Variables and click on Edit.

    步骤4 :在System Variables系统变量)下选择Path(路径) ,然后单击Edit(编辑)。
  • Path

    Path

    路径

  • Step 5: At the end of the string use semicolon and paste the path of the GeckoDriver. On my machine my FirefoxDriver exe resides in D:\Drivers\

    步骤5 :在字符串的末尾使用分号并粘贴GeckoDriver的路径 在我的机器上,我的FirefoxDriver exe驻留在D:\ Drivers \
  • Variable Value

    Variable Value

    可变值

    Note: Once the path is set you would not need to set the System property every time in the script. Your script will work without the System Property code.

    注意:设置路径后,您无需每次在脚本中都设置System属性。 您的脚本无需系统属性代码即可工作。

    The complete program to launch the GeckoDriver will be like this:

    启动GeckoDriver的完整程序将如下所示:

    
    package com.journaldev.selenium.Firefox;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class GeckoDriver {
    
        public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://journaldev.com");
        String PageTitle = driver.getTitle();
        System.out.println("Page Title is:" + PageTitle);
        driver.close();
        }
     }
    

    结论 (Conclusion)

    GeckoDriver will act as an intermediate factor between your Selenium scripts and Gecko-based browsers like Firefox. Firefox has done some changes, which has led to prevention of supporting third-party drivers to interact directly with the browsers.
    This is the main reason for which we need to use the GeckoDriver. The easiest way to use GeckoDriver in your script is to use the System.set property. [System.setProperty(“webdriver.gecko.driver”, ”Path of the Gecko Driver file”)].

    GeckoDriver将充当您的Selenium脚本和基于Firefox的基于Gecko的浏览器之间的中间因素。 Firefox进行了一些更改,从而导致无法支持第三方驱动程序直接与浏览器进行交互。
    这是我们需要使用GeckoDriver的主要原因。 在脚本中使用GeckoDriver的最简单方法是使用System.set属性。 [System.setProperty(“ webdriver.gecko.driver”,“ Gecko驱动程序文件的路径”))。

    翻译自: https://www.journaldev.com/26462/running-test-on-selenium-firefox-driver

    selenium火狐驱动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值