日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

firefox是什么浏览器_Selenium Firefox驱动程序:使用Firefox浏览器自动进行测试

發布時間:2024/7/23 HTML 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 firefox是什么浏览器_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

  • 導航到URL?https://lambdatest.github.io/sample-todo-app/

  • 選擇前兩個復選框

  • 將“將項目添加到列表”發送到ID = sampletodotext的文本框

  • 單擊添加按鈕,并驗證是否已添加文本

  • 測試用例– 2

  • 導航到URL?https://www.lambdatest.com

  • 找到描述為“自動”的懸停元素

  • 單擊了解更多信息選項以打開目標網頁

  • 如果驅動程序標題與預期標題不匹配,則引發斷言

  • 使用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。

    總結

    以上是生活随笔為你收集整理的firefox是什么浏览器_Selenium Firefox驱动程序:使用Firefox浏览器自动进行测试的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。