Error when trying to run Test through C# TestProject.OpenSDK example

Hi.

I am looking at C# - TestProject Documentation and thought i’d try the example of creating my first test project for it. When I try and run the test, i’m getting the following error:

Message:
TestProject.OpenSDK.Exceptions.InvalidTokenException : No token has been provided.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
AgentClient.ctor(Uri remoteAddress, String token, DriverOptions capabilities, ReportSettings reportSettings, Boolean disableReports, Int32 restClientTimeout, Version compatibleVersion)
AgentClient.GetInstance(Uri remoteAddress, String token, DriverOptions capabilities, ReportSettings reportSettings, Boolean disableReports, Int32 restClientTimeout, Version compatibleVersion)
BaseDriver.ctor(Uri remoteAddress, String token, DriverOptions driverOptions, String projectName, String jobName, Boolean disableReports, ReportType reportType, String reportName, String reportPath, Nullable1 remoteConnectionTimeout, Int32 restClientTimeout) ChromeDriver.ctor(Uri remoteAddress, String token, ChromeOptions chromeOptions, String projectName, String jobName, Boolean disableReports, ReportType reportType, String reportName, String reportPath, Nullable1 remoteConnectionTimeout, Int32 restClientTimeout)
Tests.Setup() line 16
–TearDown
Tests.TearDown() line 32

I have an agent set up and I have put the Dev Token into a RunSettings.xml file:

<RunSettings>
  <RunConfiguration>
    <EnvironmentVariables>
      <!-- List of environment variables we want to set-->      <TP_DEV_TOKEN>Lm1yYnWBsFa_rt7Js6w8_u7HvWFKcN9_mEIfHpqZ7701</TP_DEV_TOKEN>
    </EnvironmentVariables>
  </RunConfiguration>
</RunSettings>

Any idea of what I am doing wrong?

Hi,
You haven’t included any code but…

In your .cproj file add the following:

  <PropertyGroup>
    <RunSettingsFilePath>$(MSBuildProjectDirectory)\ProjectName.runsettings</RunSettingsFilePath>
  </PropertyGroup>

Then in your code use something like this:

public TestContext TestContext { get; set; }
public string _token = TestContext.Parameters["TP_DEV_TOKEN"];

Hi.

I tried the suggestion provided and still running into issues :frowning:

Here is what I have put in:

CSProject file

 <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RunSettingsFilePath>$(MSBuildProjectDirectory)\MyFirstOpenSDKTest.runsettings</RunSettingsFilePath>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

RunSettings XML file

 <EnvironmentVariables>
      <!-- List of environment variables we want to set-->
      <TP_DEV_TOKEN>Value of Token provided</TP_DEV_TOKEN>
    </EnvironmentVariables>

TestCSFile

using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using ChromeDriver = TestProject.OpenSDK.Drivers.Web.ChromeDriver;

namespace MyFirstOpenSDKTest
{
    public class Tests
    {
        private ChromeDriver driver;
        public TestContext TestContext { get; set; }
        public string _token = TestContext.Parameters["TP_DEV_TOKEN"];

        [SetUp]
        public void Setup()
        {
            driver = new ChromeDriver(null, _token, new ChromeOptions(), "MyFirstOpenSDKTest");
            driver.Url = "http://example.testproject.io";                
        }        

        [Test]
        public void MyFirstTest()
        {            
            driver.FindElement(By.CssSelector("#name")).SendKeys("John Smith");
            driver.FindElement(By.CssSelector("#password")).SendKeys("12345");
            driver.FindElement(By.CssSelector("#login")).Click();
            Assert.IsTrue(driver.FindElement(By.CssSelector("#logout")).Displayed);
        }

        [TearDown]
        public void TearDown()
        {
            driver.Quit();
        }
    }
}

Is there anything wrong with the above?

Hmmm! What error(s) do you see?

Source: TestCSFile line 23

Message:
TestProject.OpenSDK.Exceptions.InvalidTokenException : No token has been provided.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

Stack Trace:
AgentClient.ctor(Uri remoteAddress, String token, DriverOptions capabilities, ReportSettings reportSettings, Boolean disableReports, Int32 restClientTimeout, Version compatibleVersion)
AgentClient.GetInstance(Uri remoteAddress, String token, DriverOptions capabilities, ReportSettings reportSettings, Boolean disableReports, Int32 restClientTimeout, Version compatibleVersion)
BaseDriver.ctor(Uri remoteAddress, String token, DriverOptions driverOptions, String projectName, String jobName, Boolean disableReports, ReportType reportType, String reportName, String reportPath, Nullable1 remoteConnectionTimeout, Int32 restClientTimeout) ChromeDriver.ctor(Uri remoteAddress, String token, ChromeOptions chromeOptions, String projectName, String jobName, Boolean disableReports, ReportType reportType, String reportName, String reportPath, Nullable1 remoteConnectionTimeout, Int32 restClientTimeout)
Tests.Setup() line 16
–TearDown
Tests.TearDown() line 32

So your runsettings file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <RunConfiguration>
    <EnvironmentVariables>
      <TP_DEV_TOKEN>your token</TP_DEV_TOKEN>
    </EnvironmentVariables>
  </RunConfiguration>
</RunSettings>

and appears in the root of your project?

image

and “Auto Detect runsettings file” is ticked in Tools > Options > Test > General

Run Settings
Yes, looks exactly like that

Looks to appear in the root of the project yes

Tools > Options > Test > General
Yes, “Auto Detect runsettings file” option is ticked

Ah! It should just be “MyFirstOpenSDKTest.runsettings” get rid of the .xml

1 Like

That looks to have done it.
Thanks - much appreciated :+1:

1 Like