How to run a .NET C# Test in Sauce Labs

Test Automation Published on:

I recently received the following email:

“I have gone through your video on How to run a Selenium Web driver test case with Sauce Labs but it was for Java. Can you please show [us] how to do the same using the Selenium C# .NET binding?” So today I'm going to show you how to run a C# Selenium script in Sauce labs.

For those of you that aren't aware, Sauce Labs is the exclusive sponsor of my TestTalks podcast. Accordingly, I have a special code you can use to get 20 hours of free automation! Check out my SauceLabs page for more info.

Getting Started with Sauce Labs C Sharp

  • First, log on to SauceLabs with your free account. (Check out my video on how to use my special SauceLabs promo offer.)
  • https://saucelabs.com/login

  • Under the Tools section, select the Getting Started button.

 


  • You'll be presented with three options:
    • Automated
    • Mobile
    • Manual


  • Select Automated

Now, all you need to do is follow this quick, four-step process:

Choose your language

  • For this example, I'll be using C#.NET, but Sauce Labs supports all the Selenium language bindings.
  • Select the C#.NET option.

Setup Your Project

Open up a new project in Visual Studio.

  • In the New Project screen, select a C# Class Library template.


  • Name the project ExampleSauce and click OK.
  • From the VST menu, choose Tools>Library Package Manager> Manage Nuget Package for Solution.
  • In the Manage NuGet Packages screen, enter Selenium in the Search packages field.


  • Click on the Install button for the Selenium WebDriver. Once it's installed, do the same for Selenium WebDriver Support Classes.
  • Next, search for Gallio and install Gallio & MbUnit.


  • And Gallio Bundle


  • Click Close in the Manage NuGet Packages window.

Start writing the C# tests

At the top under the using statements, add:


using Gallio.Framework;
using Gallio.Model;
using MbUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;


  • In the RunInSauce project right before Class1, add the following:


[TestFixture]
[Header("browser", "version", "platform")]
[Row("chrome", "42", "Windows 7")]
[Row("chrome", "35", "linux")]


  • Under the Class1 section, let's add a method named SimpleTest and our test code. (This is for demonstration purposes only — there are better ways of doing this.)
[Test, Parallelizable]
public void SimpleTest(string browser, string version, string platform)
{
Uri commandExecutorUri = new Uri("http://ondemand.saucelabs.com/wd/hub");
DesiredCapabilities desiredCapabilites = new DesiredCapabilities(browser, version, Platform.CurrentPlatform);
desiredCapabilites.SetCapability("platform", platform);
desiredCapabilites.SetCapability("username", "yourseleniumusername");
desiredCapabilites.SetCapability("accessKey", "yourseleniumaccesskey");
desiredCapabilites.SetCapability("name", TestContext.CurrentContext.Test.Name);
var _Driver = new RemoteWebDriver(commandExecutorUri, desiredCapabilites);
_Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
// navigate to the page under test
_Driver.Navigate().GoToUrl("https://testguild.com/SeleniumTestPage.html");
// verify the page title is correct
Assert.Contains(_Driver.Title, "Selenium WebDriver Validation");
_Driver.Quit();
}
  • Right click on the ExampleSauce project and select Properties.
  • Click on Debug.
  • Click on Start external program and navigate to Gallio.Icarus.exe in your VST projects packages>GallioBundle.3.4.14.0>bin directory. Mine was in:

 

 

 

  • Under the Command line arguments, enter the name of your namespace.dll


  • Click on F5 to build and debug the solution.
  • The Gallio Icarus runner will appear where you can run your tests.
  • Click on the Start button.


  • Your test should run and pass. You can tell by looking in the Gallio Test Report Execution Log tab:


  • You should also see the results when you login to Sauce Labs.


  • Clicking on each Session will bring up each individual run's execution results, where you can see step-by-step results and timings as well as video playback.


 

6 responses to “How to run a .NET C# Test in Sauce Labs”

  1. This was really useful, we are about to release a new product to the market and is really important for us to test how it behaves on the most used web browsers.

    Thanks a lot!

    Manuel

  2. It looks like the gallio framework can not be installed when .net framework 4.6.1 is being used. I suggest you rewrite this maybe for c#.net users to use nunit 3.. I got it working with the example on sauce lab with some minor changes (notably the status check that it passed was outdated syntax with < nunit 3) . But would be nice to have an example with running with multiple browsers. Their example only had textfixture with one.

  3. Hi there!
    How to resolve this error?
    I’m trying to add following package in VS project.

    Error: Could not install package ‘GallioBundle 3.4.14’. You are trying to install this package into a project that targets ‘.NETFramework,Version=v4.5.2’, but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. 0

    *********************
    Need quick solution, please tell the solution ASAP.
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.