How Can We Help?

< All Topics

C# is one of the most popular programing language. Imagium can be integrated with C# and selenium with great ease.

In this example we shall use Restsharp to make API calls and NOKSA screenshot extension to take full page screenshot.

Install these dependencies via NuGet

Noksa.WebDriver.ScreenshotsExtensions
RestSharp

Here is a sample code

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using RestSharp;
using System;
using System.Net.Http;
using System.Windows.Forms;
using WDSE;
using WDSE.Decorators;
using WDSE.ScreenshotMaker;

namespace SeleniumImagium
{
    public partial class Form1 : Form
    {
        string endPoint = "http://192.168.10.13/api";
        string projectKey = "7991bf3d-060d-481d-9da4-4b885373f58b";
        HttpClient client = new HttpClient();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var client = new RestClient(endPoint);
            var request = new RestRequest("/GetUID", Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddJsonBody(
                new
                {
                    TestName = "CSharp Test Wiki",
                    ProjectKey = projectKey
                }
                );

            // Generate unique test run ID
            // Only one test run ID is needed for a single test case
            // Test name and step name should not contain special charecters like @ # & % ^ '
            string uidTest = client.Post(request).Content.Replace("\"", "");

            //Launch a browser
            IWebDriver driver = new ChromeDriver(@"C:\drivers");
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("https://en.wikipedia.org/wiki/Main_Page");

            // Use the VerticalCombineDecorator to capture Full page Screenshot:
            //Use the following method for scrollable page only - It gives better results in with full page screenshots
            var vcs = new VerticalCombineDecorator(new ScreenshotMaker());
            string base64 = vcs.MakeScreenshot(driver).ToBase64();

            //For single page use the following method
            //string base64 = Convert.ToBase64String(driver.TakeScreenshot(vcs));

            //Validate using Imagium
            //Test name and step name should not contain special charecters like @ # & % ^ '
            request = new RestRequest("/Validate", Method.POST);
            request.AddJsonBody(
              new
              {
                  StepName = "Step 1",
                  TestRunID = uidTest,
                  ImageBase64 = base64
              }
              );

            string responseValidate = client.Post(request).Content;
            Console.WriteLine(responseValidate);
        }
    }

   
}

Dependencies