前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium Firefox驱动程序:使用Firefox浏览器自动进行测试

Selenium Firefox驱动程序:使用Firefox浏览器自动进行测试

作者头像
用户7466307
发布2020-07-20 15:36:51
8.1K0
发布2020-07-20 15:36:51
举报

根据statcounter统计,到2020年6月,Mozilla Firefox浏览器在全球浏览器市场中所占份额为4.25%,因此,对于每个Selenium测试用例,Mozilla Firefox浏览器都是不可避免的。 Mozilla开发人员推出了Geckodriver(也称为Selenium Firefox驱动程序),以帮助测试人员使用Firefox浏览器自动进行浏览器测试。

在本文中,我们将研究Selenium Firefox驱动程序的基础知识,以及如何在系统中下载和设置它。然后,我们将使用Selenium Firefox驱动程序运行自动化脚本,以在Mozilla Firefox浏览器上进行测试。

什么是Selenium Firefox驱动程序?

每个浏览器都有一个独特的引擎,负责呈现网站的UI。Gecko一直是浏览器引擎,负责通过Mozilla Firefox浏览器显示Web元素。

GeckoDriver也称为Selenium Firefox驱动程序,它充当代理,可在Firefox浏览器实例上执行Selenium测试。当您使用Firefox执行Selenium测试时,JSON有线协议会将说明提供给Selenium Firefox驱动程序,即Selenium GeckoDriver。然后GeckoDriver根据指令在浏览器实例中执行相关操作,并通过HTTP服务器以HTTP协议发送响应。这是说明Selenium WebDriver架构的图像。您会注意到Selenium Firefox Driver在哪里起作用。

您是否应该在Selenium测试脚本中包括GeckoDriver?

尽管不是最受欢迎的浏览器,但Mozilla Firefox 自2002年问世以来一直是浏览器大战中的知名参与者。Firefox在Chrome之后仍然占据着很大的浏览器市场份额。我敢肯定,您可能在某个时候已经对Google Chrome和Mozilla Firefox产生了争论。实际上,许多同事更喜欢Mozilla Firefox作为默认浏览器,而不是Google Chrome。让我们看看从2019年6月到2020年7月的浏览器市场份额。

现在,相比于Chrome浏览器的69.42%的市场份额,8.48%的外观看起来要少得多,但是您注意到了什么吗?

Mozilla Firefox是第三受欢迎的桌面浏览器,并且与Safari并驾齐驱。

那么,您是否应该在Selenium测试脚本中包含Firefox驱动程序?

是的,毫无疑问。Firefox浏览器具有8.48%的稳定市场份额,已经成为跨浏览器测试的必然选择。如果您不将Firefox驱动程序包含在Selenium测试脚本中,那么您可能会错过许多潜在的潜在客户和有希望的客户。

话虽如此,让我们在您的操作系统中下载并设置Selenium Firefox驱动程序。

下载并设置GeckoDriver / Selenium项目的Firefox驱动程序

第1步:可以从Mozilla的官方GitHub存储库下载Selenium Firefox Driver或Selenium GeckoDriver 。转到链接,然后滚动到页面底部。打开“ 资产”菜单,然后将Selenium Firefox驱动程序分别下载到您的操作系统。

步骤2:解压缩下载的文件。

步骤3:将GeckoDriver(geckodriver.exe)复制到Firefox浏览器所在的文件夹中。这样,如果在测试代码中创建了Selenium Firefox Driver实例,则可以避免给出Selenium GeckoDriver的绝对路径。

为Selenium项目调用Selenium Firefox驱动程序

为了将Selenium与GeckoDriver或Selenium FirefoxDriver一起使用,必须在初始化Firefox类的新实例之前包含相应的程序包(或类)。以下是可通过流行语言使用带有GeckoDriver的Selenium的一些方法。

Selenium C#

代码语言:javascript
复制
...........................
using OpenQA.Selenium.Firefox;
...........................
...........................
 
namespace Firefox_Demo
{
    class Firefox_Demo
    {
    ...........................
    ...........................
        IWebDriver driver;
 
        [SetUp]
        public void start_Browser()
        {
            driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
        }
    ...........................
    ...........................
    [TearDown]
        public void close_Browser()
        {
            driver.Quit();
        }
  }
}

27%

Python

代码语言:javascript
复制
...........................
from selenium import webdriver
...........................
...........................
 
class Automation_Test(unittest.TestCase):
    def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.maximize_window()
    ...........................
    ...........................
  def tearDown(self):
        Quit selenium driver
        self.driver.quit()
 
if __name__ == "__main__":
    unittest.main()

Java

代码语言:javascript
复制
package org.package_name;
import org.openqa.selenium.firefox.FirefoxDriver;
...........................
...........................
 
public class ClassTest{
  private WebDriver driver;
  ...........................
  ...........................
 
  @BeforeClass
    public void setUp(){
      driver = new FirefoxDriver();
      driver.manage().window().maximize();
    ...........................
    ...........................
    }
  
  ...........................
  ...........................
  
    @AfterClass
    public void tearDown() throws Exception {
       if (driver != null) {
           driver.quit();
        }
    }
}

使用NUnit Framework在Firefox驱动程序中进行Selenium C#测试

NUnit是一个流行的开源Web测试框架。它与Selenium C#一起用于自动浏览器测试。与其他框架(例如MSTest / Visual Studio,xUnit.NET等)相比,NUnit框架的广泛功能集使其成为更流行的框架。这些框架也可以与C#和Selenium测试套件一起使用。

36%

NUnit中的断言有助于使代码更具模块化,从而减少了对源代码的维护。

这是NUnit测试的基本执行流程。初始化和取消初始化的必要步骤是[Setup]和[TearDown]批注的一部分。

牢记基本流程。让我们动手使用带有NUnit的Selenium和Geckodriver进行测试自动化。为了演示使用NUnit进行Selenium测试自动化,我们有两个示例测试用例。详情在下面提及-

测试用例– 1

  1. 导航到URL https://lambdatest.github.io/sample-todo-app/
  2. 选择前两个复选框
  3. 将“将项目添加到列表”发送到ID = sampletodotext的文本框
  4. 单击添加按钮,并验证是否已添加文本

测试用例– 2

  1. 导航到URL https://www.lambdatest.com
  2. 找到描述为“自动”的悬停元素
  3. 单击了解更多信息选项以打开目标网页
  4. 如果驱动程序标题与预期标题不匹配,则引发断言

使用Selenium Firefox Driver和NUnit进行测试的实施

代码语言:javascript
复制
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
 
namespace Firefox_Demo
{
    class Firefox_Demo
    {
        String test_url_1 = "https://lambdatest.github.io/sample-todo-app/";
        String test_url_2 = "https://www.lambdatest.com";
        IWebDriver driver;
 
        [SetUp]
        public void start_Browser()
        {
            driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
 
        }
 
        [Test, Order(1)]
        public void test_ff_1()
        {
            driver.Url = test_url_1;
            String itemName = "Adding item to the list";
 
            System.Threading.Thread.Sleep(2000);
 
            // 单击第一个复选框
            IWebElement firstCheckBox = driver.FindElement(By.Name("li1"));
            firstCheckBox.Click();
 
            // 单击第二个复选框
            IWebElement secondCheckBox = driver.FindElement(By.Name("li2"));
            secondCheckBox.Click();
 
            // 输入商品名称
            IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
            textfield.SendKeys(itemName);
 
            // 点击添加按钮
            IWebElement addButton = driver.FindElement(By.Id("addbutton"));
            addButton.Click();
 
            // 已验证添加的商品名称
            IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span"));
            String getText = itemtext.Text;
 
            //检查是否存在新添加的项目
            // 条件约束(布尔)
            Assert.That((itemName.Contains(getText)), Is.True);
 
            /* 执行等待以检查输出 */
            System.Threading.Thread.Sleep(2000);
 
            Console.WriteLine("Firefox - Test 1 Passed");
        }
 
        [Test, Order(2)]
        public void test_ff_2()
        {
            driver.Url = test_url_2;
            String hover_xpath = "/html/body/div[2]/section[2]/div/div/div[2]/div/div[1]";
            String learn_more_xpath = "/html/body/div[2]/section[2]/div/div/div[2]/div/div[1]/div[2]/span/a";
            String expected_url_title = "Online Appium and Selenium Automation Testing Tool | Selenium Grid for Web Automation Testing";
 
            System.Threading.Thread.Sleep(2000);
 
            IJavaScriptExecutor js = driver as IJavaScriptExecutor;
 
            js.ExecuteScript("window.scrollBy(0,500)");
 
            var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
            var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(hover_xpath)));
 
            Actions action = new Actions(driver);
            action.MoveToElement(element).Perform();
 
            // 由于该元素可见,因此我们应单击“了解更多”部分
 
            var more_element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(learn_more_xpath)));
            more_element.Click();
 
            /* 执行等待以检查输出 */
            System.Threading.Thread.Sleep(2000);
 
            String url_title = driver.Title;
 
            Assert.That(expected_url_title, Is.EqualTo(url_title));
 
            Console.WriteLine("Firefox - Test 2 Passed");
        }
 
        [TearDown]
        public void close_Browser()
        {
            driver.Quit();
        }
    }
}

代码演练(通用步骤)

由于Firefox浏览器用于跨浏览器测试,因此我们在执行任何测试之前创建了Firefox Web Driver实例。IWebDriver(它是OpenQA.Selenium命名空间的一部分)用于创建Selenium Firefox驱动程序实例。

代码语言:javascript
复制
namespace Firefox_Demo
{
    class Firefox_Demo
    {
        ......................................
        ......................................
        IWebDriver driver;
 
        [SetUp]
        public void start_Browser()
        {
            driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
        }
    }
    ......................................
    ......................................
}

此初始化是[SetUp]批注中实现的一部分。取消初始化过程(即释放Selenium Firefox驱动程序实例)作为[TearDown]批注中实现的一部分完成。

测试用例– 1

使用Firefox浏览器的Inspect工具,我们找到名称为li1,li2的元素(复选框)

找到复选框后,我们将找到必须添加目标文本的文本框。我们利用XPath进行相同的操作。具有布尔条件约束的断言用于验证测试用例的正确性。

代码语言:javascript
复制
IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
textfield.SendKeys(itemName);
// 执行等待以检查输出
IWebElement addButton = driver.FindElement(By.Id("addbutton"));
addButton.Click();

下面显示的是执行快照,在该快照中,我们可以看到新项目已添加到列表中-

测试用例– 2

为了找到显示名称为Automation的元素,我们使用ExecuteScript方法在当前窗口的上下文中执行JavaScript。

我们执行500像素的垂直滚动,因为要搜索的元素只能位于滚动之后。

代码语言:javascript
复制
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
 
js.ExecuteScript("window.scrollBy(0,500)");

等待30秒,以确保该元素可见。可见元素(名称= Automation)后,将使用Actions类的MoveToElement()方法执行悬停操作。

代码语言:javascript
复制
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath(“/html/body/div[2]/section[2]/div/div/div[2]/div/div[1]”)));
 
Actions action = new Actions(driver);
action.MoveToElement(element).Perform();

单击“自动化”选项的“了解更多”链接(如先前的快照所示)。新页面打开后,将使用EqualTo约束将预期的窗口标题与当前页面的标题进行比较。如果标题不匹配,则引发断言。

代码语言:javascript
复制
String url_title = driver.Title;
 
Assert.That(expected_url_title, Is.EqualTo(url_title));

结论

Mozilla Firefox在浏览器大战中占据主导地位。因此,开发人员需要牢记跨浏览器开发实践。测试人员需要在其Selenium测试套件中合并Selenium Geckodriver或Selenium Firefox Driver。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试test 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是Selenium Firefox驱动程序?
  • 您是否应该在Selenium测试脚本中包括GeckoDriver?
  • 下载并设置GeckoDriver / Selenium项目的Firefox驱动程序
  • 为Selenium项目调用Selenium Firefox驱动程序
    • Selenium C#
      • Python
        • Java
        • 使用NUnit Framework在Firefox驱动程序中进行Selenium C#测试
        • 结论
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档