Git Product home page Git Product logo

csharp-sdk-examples's Introduction

TestProject SDK for C#

TestProject is a Free Test Automation platform for Web, Mobile and API testing.
To get familiar with the TestProject, visit our main documentation website.

TestProject SDK is a single, integrated interface to scripting with the most popular open source test automation frameworks.

From now on, you can effortlessly execute Selenium and Appium native tests using a single automation platform that already takes care of all the complex setup, maintenance and configs.

With one unified SDK available across multiple languages, developers and testers receive a go-to toolset, solving some of the greatest challenges in open source test automation.

With TestProject SDK, users save a bunch of time and enjoy the following benefits out of the box:

  • Available as a NuGet package.
  • 5-mins simple Selenium and Appium setup with a single Agent deployment.
  • Automatic test reports in HTML/PDF format (including screenshots).
  • Collaborative reporting dashboards with execution history and RESTful API support.
  • Always up-to-date with the latest and stable Selenium driver version.
  • A simplified, familiar syntax for both web and mobile applications.
  • Complete test runner capabilities for both local and remote executions, anywhere.
  • Cross platform support for Mac, Windows, Linux and Docker.
  • Ability to store and execute tests locally on any source control tool, such as Git.

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;
    }
  }
  
  class TestRunner {

    Runner runner;

    TestRunner()
    {
      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 tests 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();

Custom capabilities

You can set custom capabilities that will be used by selenium/appium when opening the session.
Please note that you can't override basic options like browserName using this technique

using OpenQA.Selenium;
using TestProject.SDK;
using TestProject.Common.Enums;

var options = new ChromeOptions();
// Setting custom capability - Headless Chrome
options.AddArguments("--headless");

using (var runner = new RunnerBuilder("MY_TOKEN")
	.AsWeb(AutomatedBrowserType.Chrome)
	.WithOptions(options)
	.Build())

License

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

csharp-sdk-examples's People

Contributors

mstrelex avatar s-glatshtein avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.