Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Jun 27, 2019 14:08

Step-by-step guide

  1. Add the Perfecto Reporting package to your project, you can download it from NuGet: https://www.nuget.org/packages/Perfecto-Reporting/

  2. Add the following "CreateReportingClient" static method to your project:

            private static ReportiumClient CreateReportingClient(RemoteWebDriver driver)
            {
                PerfectoExecutionContext perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
                   .withProject(new Project("Perfecto Sample Project", "v1.0")) //optional
                   .withContextTags(new[] { "Perfecto", "Sample", "C#" }) //optional
                   .withJob(new Job("Sample C# Job", 1)) //optional
                   .withWebDriver(driver)
                   .build();
                return PerfectoClientFactory.createPerfectoReportiumClient(perfectoExecutionContext);
            }
  3. Create a ReportiumClient instance in the "SetUp" method using the driver being used (e.g. RemoteWebDriver): 

            [SetUp]
            public void initDriver()
            {
                capabilities = new DesiredCapabilities();
                capabilities.SetCapability("deviceName", "10101010");
                capabilities.SetCapability("securityToken", "token");
                driver = new RemoteWebDriver(new Uri("https://yourLab.perfectomobile.com/nexperience/perfectomobile/wd/hub"), capabilities, TimeSpan.FromSeconds(300));
                reportiumClient = CreateReportingClient(driver);
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
            }
    
  4. Use reportiumClient.testStart() method to start a test, use a try/catch block to capture any exception/failure:

            [Test]
            public void openGooglePage()
            {
                try
                {
                    reportiumClient.testStart("C# - Sample Test", new TestContextTags("C# Test"));
                    Assert.IsNotNull(driver);
                    driver.Navigate().GoToUrl("https://www.google.com/");
                    driver.FindElementByXPath("//*[@name='q']");
                }
                catch (Exception ex)
                {
                    errorMsg = ex.ToString();
                }
            }
    
  5. Report the test success or failure by using reportiumClient.testStop() in the "TearDown" method before the driver is closed:

            [TearDown]
            public void closeDriver()
            {
                if (errorMsg != null)
                    reportiumClient.testStop(TestResultFactory.createFailure(errorMsg, null));
                else
                {
                    reportiumClient.testStop(TestResultFactory.createSuccess());
                }
                driver.Close();
                driver.Quit();
            }
    

Important Note: It is essential that the testStop() method is executed before the driver close/quit command otherwise the test stop command may fail and the test may have "Unknown" status.


Full sample project:

using System;
using NUnit.Framework;
using OpenQA.Selenium.Remote;
using Reportium.client;
using Reportium.model;
using Reportium.test;
using Reportium.test.Result;

namespace PerfectoReportingSample
{
    public class Test
    {

        RemoteWebDriver driver;
        DesiredCapabilities capabilities;
        ReportiumClient reportiumClient;
        String errorMsg = null;

        [SetUp]
        public void initDriver()
        {
            capabilities = new DesiredCapabilities();
            capabilities.SetCapability("deviceName", "10101010");
            capabilities.SetCapability("securityToken", "token");
            driver = new RemoteWebDriver(new Uri("https://yourLab.perfectomobile.com/nexperience/perfectomobile/wd/hub"), capabilities, TimeSpan.FromSeconds(300));
            reportiumClient = CreateReportingClient(driver);
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
        }

        [Test]
        public void openGooglePage()
        {
            try
            {
                reportiumClient.testStart("C# - Sample Test", new TestContextTags("C# Test"));
                Assert.IsNotNull(driver);
                driver.Navigate().GoToUrl("https://www.google.com/");
                driver.FindElementByXPath("//*[@name='q']");
            }
            catch (Exception ex)
            {
                errorMsg = ex.ToString();
            }
        }

        [TearDown]
        public void closeDriver()
        {
            if (errorMsg != null)
                reportiumClient.testStop(TestResultFactory.createFailure(errorMsg, null));
            else
            {
                reportiumClient.testStop(TestResultFactory.createSuccess());
            }
            driver.Close();
            driver.Quit();
        }

        private static ReportiumClient CreateReportingClient(RemoteWebDriver driver)
        {
            PerfectoExecutionContext perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
               .withProject(new Project("Perfecto Sample Project", "v1.0")) //optional
               .withContextTags(new[] { "Perfecto", "Sample", "C#" }) //optional
               .withJob(new Job("Sample C# Job", 1)) //optional
               .withWebDriver(driver)
               .build();
            return PerfectoClientFactory.createPerfectoReportiumClient(perfectoExecutionContext);
        }

        public static void Main() { }
    }
}