# .NET MSTest

## Installation

We need to include the MSTest kit by using:

```
dotnet add package CloudBeat.Kit.MSTest
```

### Install a specific version of a package <a href="#install-a-specific-version-of-a-package" id="install-a-specific-version-of-a-package"></a>

```
dotnet add package CloudBeat.Kit.MSTest --version 4.7.0
```

## Initialization

import:

```
using CloudBeat.Kit.MSTest;
```

Inherit the CbTest class and wrap the webdriver:

```
namespace CbExamples.MSTest.Infra
{
    [TestClass]
    public abstract class WebDriverTest : CbTest
    {
        private IWebDriver _driver = null;

        public EventFiringWebDriver Driver { get; private set; }

        [TestInitialize]
        public void SetUpWebDriver()
        {
            ChromeOptions options = new ChromeOptions();
            _driver = new ChromeDriver(options);
            Driver = new EventFiringWebDriver(_driver);
            CbMSTest.WrapWebDriver(Driver);
        }
    }
}
```

## Applying CBStep Attribute in your test cases

Import:

```
using CloudBeat.Kit.MSTest.Attributes;
```

Use attribute **\[CBStep]** to describe your test case:

```
[CbStep("Open \"Login\" page")]
public void Open()
{
    driver.Navigate().GoToUrl(baseUrl ?? DEFAULT_BASE_URL);
}
```

You can also use variables in the step description, to do that we'll put **username** in curly braces:

```
[CbStep("Type \"{username}\" in \"Username\" field")]
public void EnterUsername(string username)
{
	var usernameFld = UsernameField;
	if (usernameFld == null)
	   Assert.Fail("Username field not found");
	usernameFld.Click();
	usernameFld.SendKeys(username);
}
```

## Create Binaries

Before uploading our project to CloudBeat, we have to create a binaries zip folder, in your project folder open cmd and run the following command:

```
dotnet build
```

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2Fk1iyqNTiSZJcrG72QX8h%2Fimage.png?alt=media&#x26;token=bc5cbab5-ec47-4b93-a624-f77761b0eae3" alt=""><figcaption></figcaption></figure>

a bin folder should be created, now go to bin -> debug and create a zip from net.6.0&#x20;

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2Fwm9mJzFAt8Gwj37OyojY%2Fimage.png?alt=media&#x26;token=ff10a7fb-fddd-4299-b250-eafd3be85486" alt=""><figcaption></figcaption></figure>

## Creating MSTest project in Cloudbeat

Go to projects tab and create a new project and choose **MSTest - Binaries**

{% content-ref url="../references/projects" %}
[projects](https://docs.cloudbeat.io/references/projects)
{% endcontent-ref %}

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2FItxBKYMEDuJ0Am2Y5yz8%2F2024-03-31%2015_35_47-Projects%20-%20CloudBeat.jpg?alt=media&#x26;token=fccb5d06-2d84-40bb-b592-f7545daa76d1" alt=""><figcaption></figcaption></figure>

Decide who will have accessibility to this project

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2F9ROqtixes7oNuvPH5VXI%2F2024-03-31%2015_37_40-Projects%20-%20CloudBeat.jpg?alt=media&#x26;token=675fd547-3482-4e51-9f07-1a0ab3209647" alt=""><figcaption></figcaption></figure>

Upload the binaries as a zip

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2F4sLsy2sLa4TAUAPK3Ero%2Fimage.png?alt=media&#x26;token=303a0a2d-200f-4831-84e8-b265210c0518" alt=""><figcaption></figcaption></figure>

Add assembly names and finish

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2FE4ezKeCV84h5lkk6jj1E%2Fimage.png?alt=media&#x26;token=ce1cc695-9e5f-4916-b78b-ebea55a25648" alt=""><figcaption></figcaption></figure>

And that's how you have a new MSTest project&#x20;

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2FbLk7pXihzIAQtYbosT7k%2Fimage.png?alt=media&#x26;token=8d418f9f-a138-45f6-858f-ef772311bee1" alt=""><figcaption></figcaption></figure>

## Running Test  Cases

Go to Cases under Testing tab, there you will see all your test cases

{% content-ref url="../fundamentals/executing-tests" %}
[executing-tests](https://docs.cloudbeat.io/fundamentals/executing-tests)
{% endcontent-ref %}

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2FJhA07I5GujiYGApcZThe%2Fimage.png?alt=media&#x26;token=fb72fbf8-8e44-4352-8c05-2c7acfab2418" alt=""><figcaption></figcaption></figure>

Select a browser to run your test on, save changes and run

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2FFLNzNCkfzV86JnwQmHRu%2Fimage.png?alt=media&#x26;token=1e5148eb-998a-4030-9017-5bf6a4ea4662" alt=""><figcaption></figcaption></figure>

You will see the results based on the **\[CbSteps]** that were added in your project

<figure><img src="https://1835512707-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuDeDlFo7eLPBFbUgtb%2Fuploads%2FOFyjJvzMATcZVUE0wmJH%2Fimage.png?alt=media&#x26;token=c6464c96-1153-45bd-b30c-2839b3d03aad" alt=""><figcaption></figcaption></figure>
