XUnit Testing Tutorial: Running First Script in XUnit

Himanshu Sheth

Posted On: October 16, 2020

view count79323 Views

Read time6 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-

Browser
Download location
Opera
https://github.com/operasoftware/operachromiumdriver/releases
Firefox
https://github.com/mozilla/geckodriver/releases
Chrome
http://chromedriver.chromium.org/downloads
Internet Explorer
https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
Microsoft Edge
https://blogs.windows.com/msedgedev/2015/07/23/bringing-automated-testing-to-microsoft-edge-through-webdriver/

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-

  1. Go to this URL- https://lambdatest.github.io/sample-todo-app/.
  2. Choose the first two checkboxes.
  3. Now send ‘Adding item to the list’ in the textbox below with this id = sampletodotext.
  4. 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.

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

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

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.

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.

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.

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.

Execution

For executing the NavigateToDoApp test case, go to the Test and press Run All Tests.

Execution

The progress of test execution is available in the Test Explorer window. To open the Test Explorer window, go to View -> Test Explorer.

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.

xunit tutorial

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

Source

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!

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 128



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free