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

根据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#

...........................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();        }  }}

Python

...........................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

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进行测试的实施

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驱动程序实例。

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进行相同的操作。具有布尔条件约束的断言用于验证测试用例的正确性。

IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));textfield.SendKeys(itemName);// 执行等待以检查输出IWebElement addButton = driver.FindElement(By.Id("addbutton"));addButton.Click();

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

在这里插入图片描述
测试用例– 2

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

在这里插入图片描述
我们执行500像素的垂直滚动,因为要搜索的元素只能位于滚动之后。

IJavaScriptExecutor js = driver as IJavaScriptExecutor; js.ExecuteScript("window.scrollBy(0,500)");

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

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约束将预期的窗口标题与当前页面的标题进行比较。如果标题不匹配,则引发断言。

在这里插入图片描述

String url_title = driver.Title; Assert.That(expected_url_title, Is.EqualTo(url_title));

结论

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

软件测试是IT相关行业中最容易入门的学科~不需要开发人员烧脑的逻辑思维、不需要运维人员24小时的随时待命,需要的是细心认真的态度和IT相关知识点广度的了解,每个测试人员从入行到成为专业大牛的成长路线可划分为:软件测试、自动化测试、测试开发工程师 3个阶段。

在这里插入图片描述

如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加我们的软件测试交流群 313782132 ,里面有各种软件测试资料和技术交流。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值