Andrey is a frontend developer at Blazemeter. He specializes in development and maintaining the front-end part of the GUI Functional Tests and Chrome Extension features. 

Learn JMeter in 5 Hours

Start Learning
Slack

Test Your Website Performance NOW!

arrow Please enter a URL with http(s)
Oct 23 2019

Getting started with Selenium, C#, and BlazeMeter GUI Functional Test

Selenium is one of the most popular tools for open source GUI Functional testing. You can read here about getting started with GUI Functional Testing with BlazeMeter.

 

If you prefer to run your Selenium test on C#, you can do so by writing your own script, running from your local IDE and connecting it to the BlazeMeter app.

 

The First thing that we need is Visual Studio. It allows us to run selenium tests written in C#. Go to the Visual Studio website and download Visual Studio IDE Community Edition which is available for free. In Visual Studio Installer select Universal Windows Platform development, .NET desktop development components and wait for downloading.

 

After installation, open Visual Studio and click Create a New Project, then choose Console App (.NET Framework) and give it a name:

 

Testing with Visual Studio and BlazeMeter

 

Testing with BlazeMeter and Visual Studio

 

Our project has been created, now we should add Selenium WebDriver. Click Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

 

Testing with BlazeMeter and Visual Studio

 

In the opened window click Browse and type “selenium” in search field and select Selenium WebDriver. Then select your project (if it hasn’t been selected automatically) and click Install button.

 

Testing with Visual Studio and BlazeMeter

 

As well as Selenium WebDriver, we have to install NUnit and NUnitTestAdapter. Let’s install NUnit:

 

BlazeMeter testing with Nunit

 

and NUnitTestAdapter:

 

BlazeMeter with NUnit testing

 

Once we’ve got all of the packages installed, we need to create a Class of our test: Click Project > Add Class and give it a name.

 

BlazeMeter GUI Testing with Selenium

 

Now you should see your created class:

 

BlazeMeter testing with Visual Studio

 

In our test we will use the following decorators:

[OneTimeSetUp] - used for one-time setup per test-run.

[OneTimeTearDown] - is used for one-time teardown per test-run.

[Test] - is used for describe test.

 

More info about decorators can be found in official NUnit documentation

 

using System;
using System.Collections.Generic;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace FirstBlazemeterTest
{
    class TestClass
    {

        [OneTimeSetUp]
        public void startBrowser()
        {
            
        }

        [Test]
        public void openWikiUrl()
        {
            
        }

        [OneTimeTearDown]
        public void closeBrowser()
        {
            
        }
    }
}

 

First, we need to configure the WebDriver initialization with the BlazeMeter App:

 

using System;
using System.Collections.Generic;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace FirstBlazemeterTest
{
    class TestClass
    {

        [OneTimeSetUp]
        public void startBrowser()
        {
            string HARBOR_ID = <your_harbor_id>;
            string API_KEY = <your_api_key>;
            string API_SECRET = <your api secret>;
            string CURL = "https://a.blazemeter.com/api/v4/grid/wd/hub";

            string REPORT_NAME = My First C# Test;
            string BROWSER_NAME = firefox;
            string BROWSER_VERSION = 65.0.2;

            DesiredCapabilities Capabilities = new DesiredCapabilities();
            Capabilities.SetCapability("browserName", BROWSER_NAME);
            Capabilities.SetCapability("browserVersion", BROWSER_VERSION);
            Capabilities.SetCapability("blazemeter.locationId", HARBOR_ID);
            Capabilities.SetCapability("blazemeter.reportName", REPORT_NAME);
            Capabilities.SetCapability("blazemeter.apiKey", API_KEY);
            Capabilities.SetCapability("blazemeter.apiSecret", API_SECRET);


            Uri uri = new Uri(CURL);
            driver = new RemoteWebDriver(uri, Capabilities);
        }

        [Test]
        public void openWikiUrl()
        {
            driver.Navigate().GoToUrl("https://en.wikipedia.org/");
            
            IWebElement searchField = driver.FindElement(By.Id("searchInput"));
            searchField.SendKeys("c sharp");
            searchField.SendKeys(Keys.Return);
        }

        [OneTimeTearDown]
        public void closeBrowser()
        {
            driver.Close();
        }
    }
}

 

Where API_KEY, API_SECRET - your BlazeMeter credentials, HARBOR_ID - one of the available BlazeMeter locations or your own private location.

 

Now your test is ready to run. Click Test > Windows > Test Explorer and then click Run All:

 

BlazeMeter and Visual Studio GUI Testing

 

You’ll see the progress of your test in Visual Studio. Now let’s open the BlazeMeter App and check the report:

 

Visual Studio Test run in BlazeMeter

 

Here we can see the selenium commands and video.

 

Now let’s add custom Test Suite name and Test Case name using flow-markers:

 

using System;
using System.Collections.Generic;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace FirstBlazemeterTest
{
    class TestClass
    {

        [OneTimeSetUp]
        public void startBrowser()
        {
            string HARBOR_ID = <your_harbor_id>;
            string API_KEY = <your_api_key>;
            string API_SECRET = <your api secret>;
            string CURL = "https://a.blazemeter.com/api/v4/grid/wd/hub";

            string REPORT_NAME = My First C# Test;
            string BROWSER_NAME = firefox;
            string BROWSER_VERSION = 65.0.2

            DesiredCapabilities Capabilities = new DesiredCapabilities();
            Capabilities.SetCapability("browserName", BROWSER_NAME);
            Capabilities.SetCapability("browserVersion", BROWSER_VERSION);
            Capabilities.SetCapability("blazemeter.locationId", HARBOR_ID);
            Capabilities.SetCapability("blazemeter.reportName", REPORT_NAME);
            Capabilities.SetCapability("blazemeter.apiKey", API_KEY);
            Capabilities.SetCapability("blazemeter.apiSecret", API_SECRET);


            Uri uri = new Uri(CURL);
            driver = new RemoteWebDriver(uri, Capabilities);
        }

        [Test]
        public void openWikiUrl()
        {
            var map = new Dictionary<String, String>();

            map.Add("testCaseName", "Wikipedia CSharp Search");
            map.Add("testSuiteName", "Search imitation");
            ((IJavaScriptExecutor)driver).ExecuteAsyncScript("/* FLOW_MARKER test-case-start */", map);

            driver.Navigate().GoToUrl("https://en.wikipedia.org/");
            IWebElement searchField = driver.FindElement(By.Id("searchInput"));
            searchField.SendKeys("c sharp");
            searchField.SendKeys(Keys.Return);

            map.Clear();
            map.Add("status", "success");
            map.Add("message", "");
            ((IJavaScriptExecutor)driver).ExecuteAsyncScript("/* FLOW_MARKER test-case-stop */", map);
        }

        [OneTimeTearDown]
        public void closeBrowser()
        {
            driver.Close();
        }
    }
}

 

Your test now has a custom Test Suite name and a Test Case name:

 

Running tests with Visual Studio and BlazeMeter

 

Now let’s add some logic to define, does C++ link element exist on the page or not:

 

using System;
using System.Collections.Generic;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace FirstBlazemeterTest
{
    class TestClass
    {

        [OneTimeSetUp]
        public void startBrowser()
        {
            string HARBOR_ID = <your_harbor_id>;
            string API_KEY = <your_api_key>;
            string API_SECRET = <your api secret>;
            string CURL = "https://a.blazemeter.com/api/v4/grid/wd/hub";

            string REPORT_NAME = My First C# Test;
            string BROWSER_NAME = firefox;
            string BROWSER_VERSION = 65.0.2

            DesiredCapabilities Capabilities = new DesiredCapabilities();
            Capabilities.SetCapability("browserName", BROWSER_NAME);
            Capabilities.SetCapability("browserVersion", BROWSER_VERSION);
            Capabilities.SetCapability("blazemeter.locationId", HARBOR_ID);
            Capabilities.SetCapability("blazemeter.reportName", REPORT_NAME);
            Capabilities.SetCapability("blazemeter.apiKey", API_KEY);
            Capabilities.SetCapability("blazemeter.apiSecret", API_SECRET);


            Uri uri = new Uri(CURL);
            driver = new RemoteWebDriver(uri, Capabilities);
        }

        [Test]
        public void openWikiUrl()
        {
            var map = new Dictionary<String, String>();

            map.Add("testCaseName", "Wikipedia CSharp Search");
            map.Add("testSuiteName", "Search imitation");
            ((IJavaScriptExecutor)driver).ExecuteAsyncScript("/* FLOW_MARKER test-case-start */", map);

            driver.Navigate().GoToUrl("https://en.wikipedia.org/");
            IWebElement searchField = driver.FindElement(By.Id("searchInput"));
            searchField.SendKeys("c sharp");
            searchField.SendKeys(Keys.Return);

            try
            {
                driver.FindElement(By.CssSelector("a[title='C++']"));
                map.Add("status", "success");
                map.Add("message", "");
            } catch(Exception e)
            {
                map.Add("status", "fail");
                map.Add("message", e.Message);
            }
            ((IJavaScriptExecutor)driver).ExecuteAsyncScript("/* FLOW_MARKER test-case-stop */", map);
        }

        [OneTimeTearDown]
        public void closeBrowser()
        {
            driver.Close();
        }
    }
}

 

And then run our script and check the report. C++ does not exist on the page, so we got Failed status, as described in catch() section:

 

BlazeMeter with Visual Studio

 

Visual Studio tests in BlazeMeter

 

We can define multiple test cases with different statuses using flow-markers and try - catch constructions, which may be useful for report view:

 

BlazeMeter and Visual Studio

 

To get started with running your GUI Functional Testing in BlazeMeter, set up your account here, or request a demo for you and your team.

     
arrow Please enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!