• Selenium Video Tutorials

使用C#的Selenium教程



Selenium 可以與多種語言一起使用,例如 Java、Python、JavaScript、Ruby、C#等等。Selenium 廣泛用於 Web 自動化測試。Selenium 是一款開源且可移植的自動化軟體測試工具,用於測試 Web 應用程式。

它能夠跨不同的瀏覽器和作業系統執行。Selenium 不僅僅是一個單一的工具,而是一套工具,它幫助測試人員更有效地自動化基於 Web 的應用程式。C# 是 Selenium 的另一種語言繫結。

Selenium 中 Java 和 C# 語言繫結之間沒有主要區別。主要區別僅在於語言本身。此外,這兩種語言的變更日誌也存在差異。

在 Java 中,我們使用 Webdriver、WebElement,但在 C# 中,我們稱之為 IWebdriver、IWebElement。因此,C# 中遵循的約定是在宣告介面時,在介面名稱之前新增字母“I”。

在使用 Java 的頁面工廠中,使用 @FindBy 註解來識別元素,但是,在使用 C# 的頁面工廠中,它被稱為屬性,並用 [FindBy] 表示,用於定位元素。

使用 C# 設定 Selenium 並啟動瀏覽器

步驟 1 - 從以下連結安裝名為 Visual Studio 的 C# 程式碼編輯器:https://visualstudio.microsoft.com/downloads/

使用此編輯器,我們可以開始在 C# 專案上工作以啟動我們的測試自動化。

要更詳細地瞭解如何設定 Visual Studio,請參閱以下連結:https://tutorialspoint.tw/ebook/

步驟 2 - 建立一個新專案,方法是右鍵單擊現有專案,然後單擊“新增”選項,然後單擊“新建專案”,或者如果沒有開啟任何專案,請單擊“檔案”選單,然後選擇“新建專案”選項。

Selenium Csharp Tutorial 1

步驟 3 - 選擇“控制檯應用程式”選項,然後單擊“下一步”按鈕。

Selenium Csharp Tutorial 2

步驟 4 - 輸入專案名稱,例如 SeleniumTest,然後單擊“建立”按鈕。

Selenium Csharp Tutorial 3

步驟 5 - 新建立的專案 SeleniumTest 應該可見。

Selenium Csharp Tutorial 4

步驟 6 - 右鍵單擊新建立的專案,然後選擇“管理 NuGet 包”選項。

Selenium Csharp Tutorial 5

步驟 7 - 在右上角的搜尋框中輸入 selenium,所有基於 selenium 搜尋的包都應該顯示。單擊 Selenium.WebDriver,然後單擊“新增包”按鈕。安裝所有與 Selenium 相關的包。

Selenium Csharp Tutorial 6

步驟 8 - 完成後,Selenium.Webdriver 成功新增訊息將在 Visual Studio 的頂部顯示。

Selenium Csharp Tutorial 7

步驟 9 - 重新啟動 Visual Studio。再次啟動後,所有安裝的包都應顯示在解決方案資源管理器中的 NuGet 資料夾下。

Selenium Csharp Tutorial 8

步驟 10 - 編寫程式碼以獲取以下頁面的頁面標題 - **Selenium Practice - Modal Dialogs**。

Selenium Csharp Tutorial 9

示例

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest {
   class Program {
      static void Main(string[] args) {
	  
         // Initiate Webdriver
         IWebDriver driver = new ChromeDriver();

         // adding an implicit wait of 20 secs
         driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);

         // launch the application
         driver.Navigate().GoToUrl("https://tutorialspoint.tw/selenium/practice/modal-dialogs.php");

         // get the page title
         String pageTitle = driver.Title;
         Console.WriteLine("Page title is: " + pageTitle);
      }
   }
}

輸出

Page title is: Selenium Practice - Modal Dialogs

在上面的示例中,我們在控制檯中啟動了一個應用程式並獲得了其頁面標題,訊息為 - **頁面標題是:Selenium Practice - Modal Dialogs**。

使用 Selenium C# 啟動瀏覽器並退出驅動程式

我們可以使用 driver.Navigate().GoToUrl 方法啟動瀏覽器並開啟應用程式,最後使用 Quit() 方法退出瀏覽器。

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest {
   class Program {
      static void Main(string[] args){
	  
         // Initiate Webdriver
         IWebDriver driver = new ChromeDriver();

         // adding an implicit wait of 20 secs
         driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);

         // launch the application
         driver.Navigate().GoToUrl("https://tutorialspoint.tw/selenium/practice/check-box.php");

         // quitting browser
         driver.Quit();
      }
   }
}

在上面的示例中,我們首先在 Chrome 瀏覽器中啟動了一個應用程式,然後退出了瀏覽器。

使用 Selenium C# 識別元素並檢查其功能

導航到網頁後,我們必須與頁面上可用的 Web 元素進行互動,例如單擊連結/按鈕、在編輯框中輸入文字等等,以完成我們的自動化測試用例。

為此,我們的首要任務是識別元素。Selenium 中為此提供了一些定位器,它們是 id、class、類名、name、連結文字、部分連結文字、標籤名、css 和 xpath。這些定位器需要與 FindElement() 方法一起使用。

例如,driver.FindElement(By.Id("id")) 將定位具有給定 id 屬性值的第一個 Web 元素。如果找不到具有匹配 id 屬性值的元素,則應丟擲 NoSuchElementException。

讓我們看看下面圖片中突出顯示的“Impressive”標籤旁邊的單選按鈕的 html 程式碼 -

Selenium Csharp Tutorial 10
<input class="form-check-input" type="radio" name="tab" 
   value="igotthree" onclick="show3();">

單擊該單選按鈕後,我們將在網頁上看到文字 **You have checked Impressive**。

Selenium Csharp Tutorial 11

示例

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest {
   class Program {
      static void Main(string[] args){
	  
         // Initiate the Webdriver
         IWebDriver driver = new ChromeDriver();
         
         // adding an implicit wait of 20 secs
         driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
         
         // launch an application
         driver.Navigate().GoToUrl("https://tutorialspoint.tw/selenium/practice/radio-button.php");
         
         // identify a radio button then click on radio button	
         IWebElement r = driver.FindElement
            (By.XPath("/html/body/main/div/div/div[2]/form/div[3]/input"));
         r.Click();

         // identify text after clicking radio button
         IWebElement txt = driver.FindElement(By.Id("check1"));
         
         // obtain text
         String text = txt.Text;
         Console.WriteLine("Text is: " + text);
         
         // quitting browser
         driver.Quit();
      }
   }
}

輸出

Text is: You have checked Impressive

在上面的示例中,我們在控制檯中獲得了單擊“Impressive”標籤旁邊的單選按鈕後的文字,訊息為 - **You have checked Impressive**。

本教程到此結束,我們全面介紹了 Selenium - C# 教程。我們首先介紹瞭如何使用 C# 設定 Selenium 並啟動瀏覽器,以及如何使用 Selenium C# 識別元素並檢查其功能。

這使您掌握了 Selenium - C# 教程的深入知識。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。

廣告