Selenium with PNUnit

Your guide to running Selenium Webdriver tests with PNUnit on BrowserStack.

PNUnit repo on Github

Introduction

BrowserStack gives you instant access to our Selenium Grid of 2000+ real devices and desktop browsers. Running your Selenium tests with PNUnit on BrowserStack is simple. This guide will help you:

  1. Run your first test
  2. Mark tests as pass / fail
  3. Debug your app

Prerequisites

Before you can start running your Selenium tests with PNUnit, ensure you have the PNUnit libraries installed:

Note: Install PNUnit and PNUnit3TestAdapter using NuGet Gallery

Run your first test

To understand how to integrate with BrowserStack, we will look at two things:

  1. A sample test case written in PNUnit with C#
  2. Integration of this sample test case with BrowserStack

Sample test case

The sample PNUnit test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”

public class SingleTest : BrowserStackPNUnitTest
{
  [Test]
  public void SearchGoogle()
  {
    driver.Navigate().GoToUrl("https://www.google.com/ncr");
    IWebElement query = driver.FindElement(By.Name("q"));
    query.SendKeys("BrowserStack");
    query.Submit();
    System.Threading.Thread.Sleep(5000);
    Assert.AreEqual("BrowserStack - Google Search", driver.Title);
  }
}

Once we have defined the test case, we are ready to integrate this PNUnit test case into BrowserStack.

Integrating with BrowserStack

Note: Running your Selenium tests on BrowserStack requires a BrowserStack Username and Access Key.

To obtain your username and access keys, sign up for a Free Trial or purchase a plan.

Integration of PNUnit with BrowserStack is made possible by use of following module:

[TestFixture]
public class BrowserStackPNUnitTest
{
  protected IWebDriver driver;
  private Local browserStackLocal;
  private string[] testParams;

  [SetUp]
  public void Init()
  {
    testParams = PNUnitServices.Get().GetTestParams();
    string profile = testParams[0];
    string environment = testParams[1];
    NameValueCollection caps = ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection;
    NameValueCollection settings = ConfigurationManager.GetSection("environments/" + environment) as NameValueCollection;

    DesiredCapabilities capability = new DesiredCapabilities();

    foreach (string key in caps.AllKeys)
    {
      capability.SetCapability(key, caps[key]);
    }

    foreach (string key in settings.AllKeys)
    {
      capability.SetCapability(key, settings[key]);
    }

    String username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
    if(username == null)
    {
      username = ConfigurationManager.AppSettings.Get("user");
    }

    String accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
    if (accesskey == null)
    {
      accesskey = ConfigurationManager.AppSettings.Get("key");
    }

    capability.SetCapability("browserstack.user", username);
    capability.SetCapability("browserstack.key", accesskey);
    capability.SetCapability("name", "Bstack-[Pnunit] Sample Test");

    if (capability.GetCapability("browserstack.local") != null && capability.GetCapability("browserstack.local").ToString() == "true")
    {
      browserStackLocal = new Local();
      List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
        new KeyValuePair<string, string>("key", accesskey)
      };
      browserStackLocal.start(bsLocalArgs);
    }

    driver = new RemoteWebDriver(new Uri("https://"+ ConfigurationManager.AppSettings.Get("server") +"/wd/hub/"), capability);
  }

  [TearDown]
  public void Cleanup()
  {
    driver.Quit();
    if (browserStackLocal != null)
    {
      browserStackLocal.stop();
    }
  }
}

The module reads from config file where you need to put the BrowserStack Hub URL and credentials.

// App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="capabilities">
      <section name="single" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>

    <sectionGroup name="environments">
      <section name="chrome" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>
  </configSections>

  <appSettings>
    <add key="user" value="YOUR_USERNAME" />
    <add key="key" value="YOUR_ACCESS_KEY" />
    <add key="server" value="hub-cloud.browserstack.com" />
  </appSettings>

  <capabilities>
    <single>
      <add key="browserstack.debug" value="true" />
    </single>
  </capabilities>

  <environments>
    <chrome>
      <add key="browser" value="chrome" />
    </chrome>
  </environments>
</configuration>
// config.xml
<TestGroup>
  <ParallelTests>
      <ParallelTest>
        <Name>PNUnit-BrowserStack</Name>
        <Tests>
          <TestConf>
            <Name>single_test</Name>
            <Assembly>PNUnit-BrowserStack\bin\Release\PNUnit-BrowserStack.dll</Assembly>
            <TestToRun>BrowserStack.SingleTest.SearchGoogle</TestToRun>
            <Machine>localhost:8080</Machine>
            <TestParams>
              <string>single</string>
              <string>chrome</string>
            </TestParams>
          </TestConf>
        </Tests>
      </ParallelTest>
  </ParallelTests>
</TestGroup>

Run your test on BrowserStack using following command:

Run PNUnit agent start pnunit-agent.exe agent.conf Build the solution in Visual Studio Run test pnunit-launcher.exe config\single.config.xml

Mark tests as pass / fail

BrowserStack provides a comprehensive REST API to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as pass or fail based on the assertions in your PNUnit test cases.

namespace RestApi {
  class ChangeSessionStatus {
    static void Main(string[] args) {
      string reqString = "{\"status\":\"completed\", \"reason\":\"\"}";

      byte[] requestData = Encoding.UTF8.GetBytes(reqString);
      Uri myUri = new Uri(string.Format("https://www.browserstack.com/automate/sessions/<session-id>.json"));
      WebRequest myWebRequest = HttpWebRequest.Create(myUri);
      HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
      myWebRequest.ContentType = "application/json";
      myWebRequest.Method = "PUT";
      myWebRequest.ContentLength = requestData.Length;
      using (Stream st = myWebRequest.GetRequestStream())st.Write(requestData, 0, requestData.Length);

      NetworkCredential myNetworkCredential = new NetworkCredential("YOUR_USERNAME", "YOUR_ACCESS_KEY");
      CredentialCache myCredentialCache = new CredentialCache();
      myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
      myHttpWebRequest.PreAuthenticate = true;
      myHttpWebRequest.Credentials = myCredentialCache;

      myWebRequest.GetResponse().Close();
    }
  }
}

The two potential values for status can either be completed or error. Optionally, a reason can also be passed.

A full reference of our REST API can be found here.

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.

  • Text Logs

Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.

  • Visual Logs

Visual Logs automatically capture the screenshots generated at every Selenium command run through your PNUnit tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.

Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug capability to true:

<capabilities>
  <single>
    <add key="browserstack.debug" value="true" />
  </single>
</capabilities>
  • Video Recording

Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.

Note: Video recording increases test execution time slightly. You can disable this feature by setting the browserstack.video capability to false.

<capabilities>
  <single>
    <add key="browserstack.video" value="false" />
  </single>
</capabilities>

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. Complete details to enable all the debugging options can be found here.