Set up and Run Selenium C# Tests in Visual Studio Code on macOS
Readers of my and my father’s blog know that we love to use Ruby to script automated tests. Some might wonder, “Is your view on the best language for test automation changed? Also, C# on macOS?”. This is just a curious exercise for a request. My love for the wonderful Ruby language is the same, probably even more so after this exercise.
Install Visual Studio Code first. The version I used is v1.76 on macOS 12.6.
Table of Contents:
∘ 1. Install .NET SDK (7.0)
∘ 2. Install C# and Nuget package manager extensions into VS Code
∘ 3. Create a MSTest Project Folder
∘ 4. Open the newly created test project folder in VS Code
∘ 5. Add the Selenium WebDriver package to the Test Project
∘ 6. First Selenium C# test
1. Install .NET SDK (7.0)
Download from https://dotnet.microsoft.com/en-us/download/dotnet/7.0. I chose the “macOS X64”. Run the installer.
Start a new terminal window and enter the command below to verify installation:
$ dotnet --version
2. Install C# and Nuget package manager extensions into VS Code
Press “Cmd+Shift+X” in VS Code and search for “C#” in the Extension Marketplace.
This is to add support C# in VS Code. Install it.
Repeat the same process for the “NuGet Package Manager”, which helps to add libraries, such as Selenium WebDriver, to test projects.
3. Create a MSTest Project Folder
MSTest framework is a test framework which is included, by default, with Microsoft Visual Studio.
From the terminal, run the command below.
$ dotnet new mstest -o HelloSeleniumTest
The template "MSTest Test Project" was created successfully.
Processing post-creation actions...
Determining projects to restore...
Restored /Users/courtney/tmp/HelloSeleniumTest/HelloSeleniumTest.csproj (in 1.16 sec).
4. Open the newly created test project folder in VS Code
Run the (empty) test case by clicking the ‘Run Text’ link, as indicated below:
There is an extra compilation step for readers who have only used scripting languages, such as Ruby and Python, before, as C# is a compiled language. As you can see, “
HelloSeleniumTest.dll” is the output of compilation (a.k.a build).
This empty test case is meaningless, but if it passed it means your MS test setup (with SDK) is correct.
5. Add the Selenium WebDriver package to the Test Project
We want to run a Selenium C# test that drives a chrome browser (on macOS). There are three prerequisites:
- Chrome browser
- ChromeDriver for the matching browser version is in the PATH
- Selenium WebDriver library is installed and configured in the test project.
The first two are generic and needed for any WebDriver’s powered automation. (Check out my other article, Resolve ChromeDriver Issues in Web Test Automation, if you run into issues, which is highly unlikely)
The latter one we can achieve with NuGet package manager.
Press “Cmd+Shift+P” to start Command Palette, type “nuget” and select “NuGet Package Manager: Add Package”.
Then type in “Selenium”, followed by the Enter key. You shall see this.
Press the Enter key to select “Selenium.WebDriver”. Then select the latest version, in my case, “4.8.2”.
After that, you will see popups at the right-bottom of VS Code.
Click the “Restore” button.
6. First Selenium C# test
Add a new file,
LoginTest.cs , to the project (in VS Code). Paste the content below.
public class SeleniumLoginOkTest
IWebDriver driver = null; [TestMethod]
public void TestLoginOK()
driver = new ChromeDriver();
Run it (as shown earlier). You will see a Chrome browser launched and logs into the AgileTravel site.
Animated GIF of running the above login test in Visual Studio Code below: