.NET Core brings the power of C# and VB.NET to macOS and Linux. Thanks to the hard work by the teams at Microsoft and the efforts of the Selenium Community, it is now even possible to run WebDriver tests in C# or VB.NET directly on your Mac! In this blog post we will show you how.
First, download and install the latest version of the .NET Core SDK from https://www.microsoft.com/net/download/macos on your Mac. As an IDE for C# and VB.NET on macOS, you can use Visual Studio Code, which you can download from https://code.visualstudio.com/download. To make Selenium testing easier, you can use Silk WebDriver to record your tests. You can download Silk WebDriver - also for macOS - directly from here: https://build.microfocuscloud.com/download?product=silkwebdriver.
Test project setup
After you have installed .NET Core, Visual Studio Code, and Silk WebDriver, it is time to set up your first .NET Core testing project. Create a new folder, for example on the desktop, and open that folder with Visual Studio Code. Open the Integrated Terminal by selecting View > Integrated Terminal and use the following command to actually create the project:
$ dotnet new mstest
This will create a .csproj file as well as an empty unit test which you can run with the following command:
$ dotnet test
If everything is set up correctly, .NET Core should compile and run the empty test:
Even more convenient is that you can run the test directly from Visual Studio Code. If you are prompted to install additional components for Visual Studio Code to enable this feature, do so.
Now it is time to record a Selenium test which you can later run in the context of your test project. Start Silk WebDriver, click Record, select the browser of your choice, and specify the URL of the application that you want to test:
Use the intuitive point-and-click recorder to record actions against your web app. You can also replay the recorded actions during recording to make sure that you have correctly captured the desired workflow.
When you are done with recording, select C# Unit Testing from the list of available export languages in Silk WebDriver:
Click Export… in the Silk WebDriver menu and specify a file name. Make sure that you export the file into the folder with the .NET Core project that you have previously created.
You can now close Silk WebDriver and switch back to Visual Studio Code.
Back in Visual Studio Code you should see that the project with the two unit test files now has some unresolved compile errors:
This is because you have not specified yet that you require the Selenium libraries for your project. Open the integrated terminal again and use the following commands to add those references:
$ dotnet add package Selenium.WebDriver $ dotnet add package Selenium.Support
After that, Visual Studio Code might prompt you to restore its package context - confirm this and the errors should be gone:
Running the test
Now it is time to actually run the Selenium test. You can again use either the dotnet test in the terminal or the run test link in Visual Studio Code to run the test:
Both on Windows and on the Mac, the combination of Visual Studio Code, C#/VB.NET, .NET Code, Selenium, and of course Silk WebDriver, has you covered when it comes to browser testing!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.