XUnit Testing Tutorial: Running First Script in XUnit
Himanshu Sheth
Posted On: October 16, 2020
79323 Views
6 Min Read
In our earlier blog under the xUnit testing tutorial series, we had a look at setting up the xUnit.net framework (further referred as xUnit) in Visual Studio 2019 (VS 2019). Today our core focus is on Selenium test automation with C#. So, we would also need to install the Selenium WebDriver.
Irrespective of the test framework being used, Selenium tests can be executed on a local Selenium Grid as well as remote or cloud-based Selenium Grid. The test case which is presented in this xUnit testing tutorial is executed on the local Selenium grid, i.e., target web browser, corresponding Selenium WebDriver for the browser, Selenium test framework, etc. are present on the machine from where tests are triggered.
Getting Started
Every web browser has its corresponding WebDriver that has to be installed before that browser is invoked from using the Selenium APIs. For xUnit testing framework setup, the example used for target web-page will be opened on the Chrome browser. Hence, Chrome WebDriver has to be installed on the machine where xUnit.net and Selenium test automation framework is already installed.
The locations from where you can download Selenium drivers for popular browsers like Chrome, Firefox, Internet Explorer, Microsoft Edge, Safari, etc are listed down here-
In this xUnit testing tutorial, we will use the Chrome browser for test execution. We download the Selenium WebDriver for Chrome from the location mentioned above and install it in the directory where the Chrome browser (i.e., chrome.exe) is installed.
If the above-mentioned installation practice is followed, we won’t need to specify the location of Chrome WebDriver when the browser is invoked from the core.
Take this certification to master the fundamentals of Selenium automation testing with C# and prove your credibility as a tester.
Here’s a short glimpse of the Selenium C# 101 certification from LambdaTest:
C#, xUnit, & Selenium WebDriver Demonstration
To demonstrate the usage of Selenium WebDriver with C# and xUnit testing framework, we look at the following test scenario-
- Go to this URL- https://lambdatest.github.io/sample-todo-app/.
- Choose the first two checkboxes.
- Now send ‘Adding item to the list’ in the textbox below with this id = sampletodotext.
- Lastly, click on the Add Button and verify whether the text has been added or not.
Implementation
Code WalkThrough
Step 1 – Like any other test framework, we first include the necessary packages before starting the actual test implementation. The packages that are included are xUnit, Selenium, and Chrome WebDriver.
1 2 3 4 5 6 |
using Xunit; using System; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; …………………………………………………………………………….. …………………………………………………………………………….. |
Step 2 – Since this is a non-parameterized test, i.e., the test API does not take any input parameters, we use the [Fact] attribute to declare the test method that is run by the test runner. In this example, the test method we have used is NavigateToDoApp().
1 2 3 4 5 6 7 8 |
[Fact] public void NavigateToDoApp() { IWebDriver driver; driver = new ChromeDriver(); …………………………………… …………………………………… |
Step 3 – The IWebDriver acts as the interface through which the user controls the web browser. A new instance of the Chrome browser is created using the command driver = new ChromeDriver()
.
The test URL is https://lambdatest.github.io/sample-todo-app/ which is set using driver.Navigate().GoToUrl(test_url)
. The browser window is maximized by invoking the Selenium API Manage().Window.Maximize().
1 2 3 4 5 6 7 8 9 10 |
public void NavigateToDoApp() { IWebDriver driver; // Local Selenium WebDriver driver = new ChromeDriver(); driver.Manage().Window.Maximize(); driver.Navigate().GoToUrl(test_url); driver.Manage().Window.Maximize(); |
Step 4 – The elements on which automation testing has to be performed are located using web locators such as Name, ID, XPath, etc. The Inspect tool in the Chrome browser is used to get the details of the web elements. As seen in the screenshot below, the element “li1” is located using the Name web locator. We also follow the same approach for locating the other web elements with Name – li2, li3. The other elements are located using the Id of those elements.
Once the details of these web elements are obtained, appropriate Selenium commands are used to perform actions on the web element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[Fact] public void NavigateToDoApp() { IWebDriver driver; ............................................................... ............................................................... ............................................................... // Click on First Check box IWebElement firstCheckBox = driver.FindElement(By.Name("li1")); firstCheckBox.Click(); // Click on Second Check box IWebElement secondCheckBox = driver.FindElement(By.Name("li2")); secondCheckBox.Click(); // Enter Item name IWebElement textfield = driver.FindElement(By.Id("sampletodotext")); textfield.SendKeys(itemName); ............................................................... ............................................................... ............................................................... } |
The FindElement command is used to identify the web elements present on the web-page. Once all the web elements are located, appropriate actions are performed on the web element, i.e., Click to select a particular check-box, SendKeys to input some text or in a text-box, and so on.
1 2 3 4 5 6 7 |
// Click on Second Check box IWebElement secondCheckBox = driver.FindElement(By.Name("li2")); secondCheckBox.Click(); // Enter Item name IWebElement textfield = driver.FindElement(By.Id("sampletodotext")); textfield.SendKeys(itemName); |
Step 5 – A new item (Yey, Let’s add it to list) is also added to the list. Assert is raised if the content of the newly added item does not match with the text which is supplied in the test code.
1 2 3 4 5 6 7 |
[Fact] public void NavigateToDoApp() { .......................................................................... .......................................................................... .......................................................................... // Verified Added Item name IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span")); String getText = itemtext.Text; Assert.IsTrue(itemName.Contains(getText)); |
As the test execution is complete, the Quit command (driver.quit) is invoked for terminating the WebDriver session and releasing the resources that were used by Chrome WebDriver.
1 2 3 4 5 6 7 8 9 |
[Fact] public void NavigateToDoApp() { .......................................................................... .......................................................................... .......................................................................... Console.WriteLine("LT_ToDo_Test Passed"); driver.Quit(); } |
Execution
For executing the NavigateToDoApp test case, go to the Test and press Run All Tests.
The progress of test execution is available in the Test Explorer window. To open the Test Explorer window, go to View -> Test Explorer.
The Green tick mark against the test case i.e. NavigateToDoApp indicates the execution status. As seen in the execution snapshot, the tests were executed successfully.
Shown below is the test case being executed on the Chrome browser:
This xUnit Tutorial for beginners and professionals will help you learn how to use xUnit framework with Selenium C# for performing Selenium automation testing.
Conclusion
In this xUnit testing tutorial, we had a look at Selenium using C# & xUnit testing framework. Thorough understanding of the implementation of the ToDoApp test demonstrated as a part of this xUnit testing tutorial is extremely important. It forms the base for more complex tests that can be implemented using C#, Selenium, and xUnit.net (or xUnit) testing framework. We will soon be back with more interesting blogs in this xUnit testing tutorial series. Stay tuned.
Till then Happy testing!
Got Questions? Drop them on LambdaTest Community. Visit now