C#

TestProject SDK for C#

Getting Started

To get started, you need to complete the following prerequisites checklist:

Installation

TestProject SDK for C# is available via NuGet.

  • Right-click the project and select Manage NuGet Packages...

  • Search for TestProject SDK and add it to your project.

Test Development

A test class has to implement one of the following interfaces:

TestProject.SDK.Tests
├── IWebTest
├── IAndroidTest
└── IIOSTest

Here is an example of creating & running a test class implementing the IWebTest interface. This test performs a login and expects a logout button to appear:

using OpenQA.Selenium;
using TestProject.Common.Attributes;
using TestProject.Common.Enums;
using TestProject.SDK;
using TestProject.SDK.Tests;
using TestProject.SDK.Tests.Helpers;
namespace MyFirstExample
{
public class Program
{
static void Main(string[] args)
{
using (Runner runner = new RunnerBuilder("YOUR-DEV-TOKEN")
.AsWeb(AutomatedBrowserType.Chrome).Build())
{
runner.Run(new BasicTest());
}
}
[Test(Name="Basic Test")]
class BasicTest : IWebTest
{
public ExecutionResult Execute(WebTestHelper helper)
{
// Get driver initialized by TestProject Agent
// No need to specify browser type, it can be done later via UI
var driver = helper.Driver;
driver.Navigate().GoToUrl("https://example.testproject.io/web/");
driver.FindElementByCssSelector("#name").SendKeys("John Smith");
driver.FindElementByCssSelector("#password").SendKeys("12345");
driver.FindElementByCssSelector("#login").Click();
if (driver.FindElements(By.CssSelector("#logout")).Count > 0)
return ExecutionResult.Passed;
return ExecutionResult.Failed;
}
}
}
}

Below are links to other examples with complete source code:

Test Running

To debug or run the test, you will have to use the Runner class. Here' is an NUnit example that executes the BasicTest shown above:

using NUnit.Framework;
using TestProject.Common.Enums;
using TestProject.SDK.Examples.Web.Runners.Nunit.Base;
namespace TestProject.SDK.Examples.Runners.Nunit
{
public class BasicTests
{
Runner runner;
[OneTimeSetUp]
public void SetUp()
{
runner = new RunnerBuilder(DevToken).AsWeb(AutomatedBrowserType.Chrome).Build();
}
[Test]
public void TestLogin()
{
runner.Run(new BasicTest());
}
[OneTimeTearDown]
public void TearDown()
{
runner.Dispose();
}
}
}

Below are examples for initialization of other Runner types:

Desktop Web

var runner = new RunnerBuilder("DEV_TOKEN")
.AsWeb(AutomatedBrowserType...).Build();

Android

var runner = new RunnerBuilder("DEV_TOKEN")
.AsAndroid("DEVICE_UDID", "APP_PACKAGE", "ACTIVITY").Build();

Chrome on Android

var runner = new RunnerBuilder("DEV_TOKEN")
.AsAndroidWeb("DEVICE_UDID").Build();

iOS

var runner = new RunnerBuilder("DEV_TOKEN")
.AsIOS("DEVICE_UDID", "DEVICE_NAME", "APP_BUNDLE_ID").Build();

Safari on iOS

var runner = new RunnerBuilder("DEV_TOKEN")
.AsIOSWeb("IOS_DEVICE_ID", "IOS_DEVICE_NAME").Build();

Reports

TestProject SDK reports all driver commands and their results to the TestProject Cloud. Doing so, allows us to present beautifully designed reports and statistics in our dashboards.

Reports can be completely disabled via RunnerBuilder, for example:

var runner = new RunnerBuilder("DEV_TOKEN")
.WithReportsDisabled()
.AsWeb(AutomatedBrowserType...)
.Build();

There are more options to disable reporting of specific entities. See Disabling Reports section for more information.

Implicit Project and Job Names

The SDK will attempt to infer Project and Job names from NUnit / MSTest / XUnit attributes. If found, the following logic and priorities take place:

  • Namespace of the class where the Runner is created, is used for Project name.

  • Class name where the Runner is created, is used for the Job name.

  • Test class name, or the Name property of the Test attribute on the class is used for the Test name.

Examples of implicit Project & Job names inferred from annotations:

Explicit Names

Project and Job names can be also specified explicitly using the relevant options in RunnerBuilder, for example:

var runner = new RunnerBuilder("DEV_TOKEN")
.WithProjectName("My First Project")
.WithJobName("My First Job")
.AsWeb(AutomatedBrowserType...)
.Build();

You can specify a Project, a Job name or both. If you don't, the value will be inferred automatically. See Implicit Project and Job Names section for more information on how these names are inferred.

Examples of explicit Project & Job names configuration:

Tests Reports

Tests Reporting

Tests are reported automatically when a test ends, in other works, when Runner.Run() method execution ends. Test names are inferred from the [Test] attribute's Name property if it is present, or from the test class name.

This behavior can't be overridden or disabled at this time.

Each call to Runner.Run will create a separate test in a job report. Even calls in the same testing framework method behave this way.

For example, following NUnit based code, will generate the following four tests in the report:

class Test : IWebTest
{
[Parameter]
public string url;
public ExecutionResult Execute(WebTestHelper helper)
{
helper.Driver.Navigate().GoToUrl(url);
return ExecutionResult.Passed;
}
}
public DesktopTests(AutomatedBrowserType automatedBrowserType)
{
runner = new RunnerBuilder("DevToken")
.AsWeb(AutomatedBrowserType.Chrome).Build();
}
[Test]
public void RunGoogle()
{
var test = new Test() { url = "http://www.google.com" };
runner.Run(test);
}
[Test]
public void RunTestProject()
{
var test = new Test() { url = "http://testproject.io" };
runner.Run(test);
}
[Test]
public void RunAll()
{
var test = new Test() { url = "http://www.google.com" };
runner.Run(test);
test = new Test() { url = "http://testproject.io" };
runner.Run(test);
}
[OneTimeTearDown]
public void TearDown()
{
runner.Dispose();
}

Report:

Report
├── RunGoogle
│ └── Navigate To https://google.com/
├── RunTestProject
│ └── Navigate To http://testproject.io
└── RunAll
├── Navigate To https://google.com/
└── Navigate To http://testproject.io

Steps

Steps are reported automatically when driver commands are executed. Even if this feature is disabled, or in addition, steps can still be reported manually

Notice the following line in the Extended Test example. This line reports a step based on provided condition and takes a screenshot:

helper.Reporter.Step("Profile information saved", profilePage.Saved, TakeScreenshotConditionType.Always);

Using the following code one can set test result message:

report.Result = "Test completed successfully";

Disabling Reports

Reporting can be disabled during Runner creation. If reporting was explicitly disabled when the runner was created, it can not be enabled later.

Disable all reports

The following will create a runner with all types of reports disabled (except manual step reports):

var runner = new RunnerBuilder("DEV_TOKEN")
.WithReportsDisabled()
.Build();

Disable driver commands reports

Disabling commands reporting will result in test reports with no steps, unless they are reported manually using helper.Reporter.step(). The following will disable driver commands reporting:

var runner = new RunnerBuilder("DEV_TOKEN")
.WithDriverCommandReportingDisabled()
.Build();

License

TestProject SDK For C# is licensed under the LICENSE file in the root directory of this source tree.