<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Selenium C#Webdriver 教程:NUnit 示例 > 原文: [https://www.guru99.com/selenium-csharp-tutorial.html](https://www.guru99.com/selenium-csharp-tutorial.html) ## 硒概述: Selenium 是一種開源 Web [自動化測試](/automation-testing.html)工具,它支持多個瀏覽器和多個操作系統。 它允許測試人員使用多種編程語言(例如 Java,C#,Python,.Net,Ruby,PHP 和 Perl)來編碼自動化測試。 ## C#概述: C#是從 C ++和 Java 派生的一種面向對象的編程語言。 C#允許開發人員在.Net 平臺上使用 Visual Studio 構建應用程序。 以下是 C#的主要功能。 1. 它是一種面向對象的編程語言 2. 它支持控制臺,Windows 和基于 Web 的應用程序的開發 3. 它提供了諸如封裝,繼承和多態性的功能。 <center>**What You Will Learn:** []</center> * [C#的基本語法:](#1) * [使用 Selenium WebDriver 設置 Visual Studio:](#2) * [在 Visual Studio 中創建一個新項目:](#3) * [使用 Selenium WebDriver 設置 Visual Studio:](#4) * [NUnit Framework:概述](#5) * [安裝 NUnit Framework 的步驟:](#6) * [下載 NUnit 測試適配器](#7)的步驟 * [Selenium 和 NUnit 框架:](#8) * [在 Selenium 中創建 NUnit 測試類的步驟:](#9) * C#中的 [WebDriver 命令:](#10) * [瀏覽器命令:](#11) * [Webelement 命令:](#12) * C#中的[下拉命令:](#13) * [代碼示例](#14) * [示例 1:使用 XPATH 定位器單擊鏈接:](#15) * [示例 2:將數據輸入到 TextBox 中,然后使用 XPATH 定位器單擊一個按鈕:](#16) * [示例 3:將數據輸入到 TextBox 中,然后使用 CSS 定位器單擊一個按鈕:](#17) * [示例 4:在下拉列表中選擇一個值:](#18) ## C#的基本語法: C#中的程序需要包含以下部分 1. 命名空間聲明 2. 班級 3. 類的屬性和方法 4. 主要方法 5. 計劃陳述 **示例-** 下面是一個示例 C#程序,用于在系統控制臺上打印文本“ Guru99”。 ``` using System; namespace FirstProgram { class DemoPrint { static void main(){ Console.WriteLine("Guru99"); } } } ``` 說明: * C#中的名稱空間是多個類的集合。 必須使用關鍵字“ using”聲明每個名稱空間。 * 代碼的第一條語句在我們的程序中包含名稱空間“ System”。 系統名稱空間定義了 C#中使用的基本類和事件。 要使用的名稱空間取決于程序要求。 * 第二條語句是為類“ DemoPrint”聲明一個名稱空間“ FirstProgram”。 一個 C#文件可以在同一個命名空間中包含多個類。 * 第三條語句包括類聲明。 一個類可以包含多個屬性和多個方法。 * 第四個語句包括 Main 方法的聲明。 主要方法是每個類的執行入口。 * 最后一條語句是用于將語句打印到控制臺的 C#語法。 WriteLine 是“控制臺”類的方法。 ## 使用 Selenium WebDriver 設置 Visual Studio: Visual Studio 是一個集成開發環境(IDE),開發人員可使用它來跨多個平臺(例如 Windows,Android,iOS 和基于云的應用程序)構建應用程序。 **步驟 1)**導航至 URL [https://www.visualstudio.com/downloads/](https://www.visualstudio.com/downloads/) ,然后單擊 Visual Studio Community 2017 選項卡上顯示的“免費下載”按鈕 ![](https://img.kancloud.cn/7b/61/7b617126ef3b548525c4041dd0ba6728_800x378.png) **步驟 2)**打開下載的 exe。 如果詢問管理員權限,請單擊“是”。 ![](https://img.kancloud.cn/f8/cf/f8cfeda3f2687bb7fc2ef19b767970cb_559x372.png) **步驟 3)**將會出現以下彈出窗口。 點擊“繼續”按鈕。 ![](https://img.kancloud.cn/6d/18/6d181c4d5952ebc010fd6a376b7ead14_400x200.png) 文件將被下載,如下面的彈出窗口所示。 ![](https://img.kancloud.cn/a7/8c/a78c74e3756f841046c036d35e964237_786x397.png) **步驟 4)**在下一個屏幕中, 選中以下復選框 * 通用 Windows 平臺開發 * 網絡桌面開發 點擊“安裝”。 ![](https://img.kancloud.cn/f3/e8/f3e8a40742e7b6fe2dac63717aec819a_800x447.png) 等待每個組件的安裝完成。 文件大小為 16GB,將需要一些時間。 ![](https://img.kancloud.cn/df/f6/dff6b234c4532664e767c398550bf726_644x396.png) **步驟 5)**將顯示以下彈出窗口。 點擊“重啟”按鈕。 ![](https://img.kancloud.cn/66/33/6633df01821e2d7ea99729cec544549c_600x206.png) **步驟 6)**重新啟動計算機后,在開始菜單上搜索“ Visual Studio 2017”,然后單擊搜索結果。 將出現以下彈出窗口。 如果您沒有現有帳戶,請單擊“不立即,也許以后”鏈接。 ![](https://img.kancloud.cn/e3/32/e332f7c2d632af7c7f3348dfe5d1b4ac_400x481.png) **步驟 7)**在下一個屏幕中, * 選擇您喜歡的顏色主題 * 單擊按鈕“啟動 Visual Studio” ![](https://img.kancloud.cn/8c/57/8c571221a9f4e6051b95a8079eb01a56_464x570.png) **步驟 8)**將出現 Visual Studio“入門”屏幕。 ![](https://img.kancloud.cn/b3/de/b3dea0f7600acd05676da883b7407e07_800x426.png) ### 在 Visual Studio 中創建一個新項目: **步驟 1)**在文件菜單中,單擊新建>項目 ![](https://img.kancloud.cn/4b/71/4b71244ef70d0e36a185c09c4fc18bb3_612x181.png) **步驟 2)**在下一個屏幕中, 1. 選擇選項“ Visual C#” 2. 單擊控制臺應用程序(.Net Framework) 3. 輸入名稱為“ Guru99” 4. 點擊確定 ![](https://img.kancloud.cn/71/88/7188cd222ae21c559b92ddb73e0103b7_800x455.png) **步驟 3)**成功創建項目后,將顯示以下屏幕。 ![](https://img.kancloud.cn/6e/63/6e63e3f91a0345df26690a40a682d0e8_837x394.png) ### 使用 Selenium WebDriver 設置 Visual Studio: **步驟 1)**導航到工具-> NuGet 軟件包管理器->管理 NuGet 軟件包以獲取解決方案 ![](https://img.kancloud.cn/b4/8a/b48a12ac5acff5b49abcbde3880efe4f_649x401.png) **步驟 2)**在下一個屏幕中 1. 在結果屏幕上搜索 Selenium 2. 選擇第一個搜索結果 3. 選中項目復選框 4. 點擊“安裝” ![](https://img.kancloud.cn/7b/ac/7bac32c892dfd30d17b80898937a08b4_1174x628.png) **步驟 3)**在彈出屏幕中單擊“確定”按鈕 ![](https://img.kancloud.cn/8d/1b/8d1b9521abf4bdbb09364349f410600d_400x407.png) **步驟 4)**成功安裝軟件包后,將顯示以下消息。 ![](https://img.kancloud.cn/a3/62/a3625ef692d72d8ec565cfd56eba90b2_918x529.png) ## NUnit 框架:概述 NUnit 是 Visual Studio 和 Selenium WebDriver 支持的[單元測試](/unit-testing-guide.html)框架。 NUnit 是.Net 應用程序中使用最廣泛的單元測試框架。 NUnit 以可讀的格式顯示測試結果,并允許測試人員調試自動測試。 為了使用它,我們需要在 Visual Studio 上安裝 NUnit Framework 和 NUnit 測試適配器。 ### 安裝 NUnit Framework 的步驟: 1.導航到工具-> NuGet 軟件包管理器->管理解決方案的 NuGet 軟件包 ![](https://img.kancloud.cn/b4/8a/b48a12ac5acff5b49abcbde3880efe4f_649x401.png) **步驟 2)**在下一個窗口中 1. 搜索 NUnit 2. 選擇搜索結果 3. 選擇項目 4. 點擊安裝 ![](https://img.kancloud.cn/25/2b/252b574fb88525b92b88e324c2d0e2a5_983x418.png) **步驟 3)**將會出現以下彈出窗口。 點擊“確定”按鈕。 ![](https://img.kancloud.cn/af/0e/af0e25986f0ea6f3e714bf33925f0537_400x406.png) **步驟 4)**安裝完成后,將出現以下消息。 ![](https://img.kancloud.cn/00/3a/003ae7d3415ebfced27e1fa943c5310b_383x221.png) ### 下載 NUnit 測試適配器的步驟 請注意,以下步驟僅適用于 32 位計算機。 對于 64 位計算機,您需要按照以下相同的步驟下載“ NUnit3 測試適配器”。 **步驟 1)**導航到工具-> NuGet 軟件包管理器->管理解決方案的 NuGet 軟件包。 在那個屏幕上 1. 搜索 NUnitTestAdapter 2. 點擊搜索結果 3. 選擇項目 4. 點擊安裝 ![](https://img.kancloud.cn/75/59/7559a97274fb2b375a713cd18592ca90_982x420.png) **步驟 2)**在確認彈出窗口上單擊“確定”。 安裝完成后,您將看到以下消息: ![](https://img.kancloud.cn/c8/31/c831ce974959e655b671ebf57c551f59_307x203.png) ## Selenium 和 NUnit 框架: 硒與 NUnit 框架的集成使測試人員可以區分各種測試類別。 NUnit 還允許測試人員在運行測試之前和之后使用諸如 SetUp,Test 和 TearDown 之類的注釋執行操作。 通過創建 NUnit 測試類并使用 NUnit 框架運行測試類,可以將 NUnit 框架與 Selenium 集成。 以下是使用 NUnit 框架創建和運行測試類所需的步驟。 ### 在 Selenium 中創建 NUnit 測試類的步驟: **步驟 1)**在解決方案資源管理器中,右鍵單擊項目>添加>類 ![](https://img.kancloud.cn/2b/af/2bafcafa5a67f243488269895167db20_663x474.png) **步驟 2)將出現**類創建窗口。 1. 給班級起個名字 2. 點擊添加按鈕 ![](https://img.kancloud.cn/83/9f/839f0118b2f537be085b7242a2e0c3a3_700x492.png) **步驟 3)**將會出現以下屏幕。 ![](https://img.kancloud.cn/26/64/2664f72f0bb282f3b3ac178e766d66d6_707x273.png) **步驟 4)**將以下代碼添加到創建的類中。 請注意,您需要在 chrome 驅動程序初始化期間指定“ chromdriver.exe”文件的位置。 ``` using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Firefox; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Guru99Demo { class Guru99Demo { IWebDriver driver; [SetUp] public void startBrowser() { driver = new ChromeDriver("D:\\3rdparty\\chrome"); } [Test] public void test() { driver.Url = "http://www.google.co.in"; } [TearDown] public void closeBrowser() { driver.Close(); } } } ``` **步驟 4)**單擊“構建”->“構建解決方案” ![](https://img.kancloud.cn/1d/f9/1df9d6def33e894adc5d17ecf756679e_499x89.png) **注意:**構建時,您可能會收到類似“不包含適用于入口點的靜態'main'方法”之類的錯誤。 [![](https://img.kancloud.cn/8b/9c/8b9c25b015ea3331976f6d85d83a68c7_323x103.png) ](/images/1/103017_0412_SeleniumCWe28.png) 要解決此問題,請轉到項目>屬性,然后將“輸出類型”更改為“類庫”。 默認值為“控制臺應用程序”。 ![](https://img.kancloud.cn/13/45/1345619871f63897a5f19bbe52335b10_622x190.png) **步驟 5)**構建成功后,我們需要打開“測試資源管理器”窗口。 單擊測試-> Windows->測試瀏覽器 ![](https://img.kancloud.cn/83/94/8394eb6f110106762959e6dc372d5886_441x168.png) **步驟 6)**將打開“測試資源管理器”窗口,其中包含可用測試的列表。 右鍵單擊“測試資源管理器”,然后選擇“運行選定的測試” ![](https://img.kancloud.cn/c9/5c/c95c88d39d3a9f505bc35b80cdd1d72c_536x592.png) **步驟 7)** Selenium 必須使用指定的 URL 打開瀏覽器并關閉瀏覽器。 測試用例的狀態將在“測試資源管理器”窗口中更改為“通過”。 ![](https://img.kancloud.cn/75/af/75afec1fabca73905f1cda5e2fa629c0_242x249.png) ## C#中的 Selenium WebDriver 命令: C#使用接口“ IWebDriver”進行瀏覽器交互。 以下是 C#中可用的命令類別。 1. 瀏覽器命令 2. Web 元素命令 3. 下拉命令 讓我們一一研究 ### 瀏覽器命令: 以下是 C#中可用的瀏覽器命令的列表。 | **命令名稱** | **說明** | **語法** | | **網址命令** | 此命令用于在瀏覽器中打開指定的 URL。 | ``` driver.Url = "https://www.guru99.com" ``` | | **標題命令** | 此命令用于檢索當前打開的網頁的頁面標題 | ``` String title = driver.Title ``` | | **PageSource 命令** | 該命令用于獲取當前打開的網頁的源代碼。 | ``` String pageSource = driver.PageSource ``` | | **關閉命令** | 此命令用于關閉最近打開的瀏覽器實例。 | ``` driver.Close(); ``` | | **退出命令** | 此命令用于關閉所有打開的瀏覽器實例 | ``` driver.Quit(); ``` | | **后退命令** | 此命令用于導航到瀏覽器歷史記錄的上一頁。 | ``` driver.Navigate().Back(); ``` | | **轉發命令** | 此命令用于導航到瀏覽器歷史記錄的下一頁。 | ``` driver.Navigate().Forward() ``` | | **刷新命令** | 此命令用于執行瀏覽器刷新。 | ``` driver.Navigate().Refresh() ``` | ### Webelement 命令: Webelement 代表網頁上的所有元素。 它們由 HTML 標記表示。 每個按鈕,文本框,鏈接,圖像,表格和框架都屬于 Webelements。 可以使用 IWebelement 界面觸發對 Web 元素的操作。 要與 Webelement 進行交互,我們需要在網頁上找到元素,然后對其進行操作。 諸如 Firebug 和 Firepath 之類的工具可用于識別 Webelement 的 Xpath。 以下是 C#中可用的 Webelement 命令的列表。 | **Command Name** | **Description** | **Syntax** | | **點擊命令** | 此命令用于單擊 Webelement。 為了使元素可單擊,該元素必須在網頁上可見。 此命令也用于復選框和單選按鈕操作。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); element.Click(); ``` | | **清除命令** | 該命令專門用于清除文本框的現有內容。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); element.Clear(); ``` | | **SendKeys 命令** | 此命令用于在文本框中輸入值。 輸入的值必須作為參數傳遞給 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); element.SendKeys("guru99"); ``` | | **顯示的命令** | 此命令用于標識網頁上是否顯示特定元素。 該命令返回一個布爾值; 是 true 還是 false 取決于 Web 元素的可見性。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); Boolean status = element.Displayed; ``` | | **啟用的命令** | 此命令用于標識是否在網頁上啟用了特定的 Web 元素。 該命令返回一個布爾值; 結果是對還是錯。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); Boolean status = element.Enabled; ``` | | **選定的命令** | 此命令用于標識是否選擇了特定的 Web 元素。 此命令用于復選框,單選按鈕和選擇操作。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); Boolean status = element.Selected; ``` | | **提交命令:** | 該命令與 click 命令類似,區別在于 HTML 表單是否具有類型為 Submit 的按鈕。 當 click 命令單擊任何按鈕時,submit 命令僅單擊類型為 commit 的按鈕。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); element.submit(); ``` | | **文本命令** | 此命令返回 Webelement 的內部文本。 此命令返回一個字符串值作為結果。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); String text=element.Text; ``` | | **TagName 命令** | 此命令返回 Web 元素的 HTML 標簽。 它返回一個字符串值作為結果。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); String tagName = element.TagName; ``` | | **GetCSSValue 命令:** | 此方法用于以 rgba 字符串(紅色,綠色,藍色和 Alpha)的形式返回 Web 元素的顏色。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); String color = element.getCSSValue; ``` **輸出**-如果元素的顏色為紅色,則輸出為 rgba(255,0,0,1) | ### 下拉命令: 可以使用 SelectElement 類實現 C#中的下拉操作。 以下是 C#中可用的各種下拉操作。 | **Command Name** | **Description** | **Syntax** | | **SelectByText 命令** | 該命令根據選項的文本選擇下拉菜單的選項。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); select.SelectByText("Guru99"); ``` | | **SelectByIndex 命令** | 此命令用于根據其索引選擇一個選項。 下拉索引從 0 開始。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); select.SelectByIndex("4"); ``` | | **SelectByValue 命令** | 此命令用于根據選項值選擇選項。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); select.SelectByValue("Guru99"); ``` | | **選項命令** | 此命令用于檢索下拉菜單中顯示的選項列表。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); List<IWebelement> options = select. Options; int size = options.Count; for(int i=0;i<options.size();i++) { String value = size.elementAt(i).Text; Console.writeLine(value); } ``` 上面的代碼在一個下拉列表中將所有選項打印到控制臺上。 | | **IsMultiple 命令** | 此命令用于識別下拉列表是否為多選下拉列表; 多選下拉菜單使用戶可以一次選擇一個下拉菜單中的多個選項。 此命令返回一個布爾值。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); Boolean status = select.IsMultiple(); ``` | | **DeSelectAll 命令** | 此命令用于多選下拉列表中。 清除已選擇的選項。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); select.DeSelectAll(); ``` | | **DeSelectByIndex 命令** | 此命令使用其索引取消選擇已選擇的值。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); select.DeSelectByIndex("4"); ``` | | **DeSelectByValue 命令** | 此命令使用其值取消選擇已選擇的值。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); select.DeSelectByValue("Guru99"); ``` | | **DeSelectByText 命令** | 此命令使用其文本取消選擇已選擇的值。 | ``` IWebelement element = driver.FindElement(By.xpath("xpath of Webelement")); SelectElement select = new SelectElement(element); select.DeSelectByText("Guru99"); ``` | ## 代碼示例 ### 示例 1:使用 XPATH 定位器單擊鏈接: **測試方案:** 1. 導航至 Demo Guru99 網頁- [http://demo.guru99.com/test/guru99home/](http://demo.guru99.com/test/guru99home/) 2. 最大化窗口 3. 點擊“測試”菜單 4. 關閉瀏覽器 ``` using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Guru99Demo { class CSS { IWebDriver m_driver; [Test] public void cssDemo() { m_driver = new ChromeDriver("D:\\3rdparty\\chrome"); m_driver.Url = "http://demo.guru99.com/test/guru99home/"; m_driver.Manage().Window.Maximize(); IWebelement link = m_driver.FindElement(By.XPath(".//*[@id='rt-header']//div[2]/div/ul/li[2]/a")); link.Click(); m_driver.Close(); } } } ``` ### 示例 2:將數據輸入到 TextBox 中,然后使用 XPATH 定位器單擊一個按鈕: **Test Scenario:** 1. 導航到 Guru 99 演示頁面- [http://demo.guru99.com/test/guru99home/](http://demo.guru99.com/test/guru99home/) 2. 在電子郵件文本框中輸入數據 3. 點擊注冊按鈕 ``` using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Guru99Demo { class CSS { IWebDriver m_driver; [Test] public void cssDemo() { m_driver = new ChromeDriver("G:\\"); m_driver.Url = "http://demo.guru99.com/test/guru99home/"; m_driver.Manage().Window.Maximize(); // Store locator values of email text box and sign up button IWebElement emailTextBox = m_driver.FindElement(By.XPath(".//*[@id='philadelphia-field-email']")); IWebElement signUpButton = m_driver.FindElement(By.XPath(".//*[@id='philadelphia-field-submit']")); emailTextBox.SendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it. "); signUpButton.Click(); } } } ``` ### 示例 3:在 TextBox 中輸入數據并使用 CSS 定位器單擊一個按鈕: **Test Scenario:** 1. 導航到 Guru 99 演示頁面- [http://demo.guru99.com/test/guru99home/](http://demo.guru99.com/test/guru99home/) 2. 在電子郵件文本框中輸入數據 3. 點擊注冊按鈕 ``` using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Guru99Demo { class CSS { IWebDriver m_driver; [Test] public void cssDemo() { m_driver = new ChromeDriver("G:\\"); m_driver.Url = "http://demo.guru99.com/test/guru99home/"; m_driver.Manage().Window.Maximize(); // Store locator values of email text box and sign up button IWebElement emailTextBox = m_driver.FindElement(By.CssSelector("input[id=philadelphia-field-email]")); IWebElement signUpButton = m_driver.FindElement(By.CssSelector("input[id=philadelphia-field-submit]")); emailTextBox.SendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it."); signUpButton.Click(); } } } ``` ### 示例 4:在下拉列表中選擇一個值: **Test Scenario:** 1. 導航到 Guru 99 演示頁面- [http://demo.guru99.com/test/guru99home/](http://demo.guru99.com/test/guru99home/) 2. 點擊 SAP 鏈接 3. 在名稱和電子郵件文本框中輸入數據 4. 從課程下拉菜單中選擇一個值 5. 關閉瀏覽器 ``` using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI; namespace Guru99Demo { class TestSelect { IWebDriver m_driver; [Test] public void selectDemo() { m_driver = new ChromeDriver("G:\\"); m_driver.Url = "http://demo.guru99.com/test/guru99home/"; m_driver.Manage().Window.Maximize(); IWebElement course = m_driver.FindElement(By.XPath(".//*[@id='awf_field-91977689']")); var selectTest = new SelectElement(course); // Select a value from the dropdown selectTest.SelectByValue("sap-abap"); } } } ``` ## 摘要: * 為了將 Selenium 與 C#一起使用,您需要安裝 Visual Studio。 * NUnit 是 Visual Studio 和 Selenium WebDriver 支持的單元測試框架 * 為了使用它,我們需要在 Visual Studio 上安裝 NUnit Framework 和 NUnit 測試適配器。 * 通過創建 NUnit 測試類并使用 NUnit 框架運行測試類,可以將 NUnit 框架與 Selenium 集成。 * NUnit 還允許測試人員在運行測試之前和之后使用諸如 SetUp,Test 和 TearDown 之類的注釋執行操作。 * Selenium WebDriver 命令可以分為瀏覽器命令,Webelement 命令和下拉命令。 本文由 Sneha Gudapati 提供
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看