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

Last updated: Nov 14, 2019 12:16

Download

Download Reporting SDK client for your programming languages and framework

Include Smart Reporting in Automation

Add the following elements to your automation code to take advantage of Perfecto Smart Reporting.

Add the basic classes

Declare the Reporting classes

Add the following Using statements to the base test class:

//Reportium required using statements 
using Reportium.Test;
using Reportium.Test.Result;
using Reportium.Client;
using Reportium.Model;

On this page:

Create the reporting client

Use the ReportiumClientFactory class' CreatePerfectoReportiumClient() method to create the Smart Reporting ReportiumClient instance. The reporting client is responsible for gathering basic information about the test and transmitting it to the Smart Reporting system.

Before creating the ReportiumClient, you should create the instance of the automation driver (either SeleniumDriver or one of the Appium drivers) and use PerfectoExecutionContext to supply the link to the factory class creating the client instance. Use the withWebDriver() method to supply the link of the driver instance. The context supports all of the optional settings described below, in addition to supplying the driver link. Use the Build() method to create the context object's instance and supply this to the CreatePerfectoReportiumClient() method when creating the ReportiumClient instance.

PerfectoExecutionContext perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
    .WithProject(new Project("Sample Reportium project", "1.0"))
    .WithJob(new Job("IOS tests", 45).withBranch("branch-name"))
    .WithCustomFields(new CustomField("programmer", "Samson"))
    .WithContextTags("Regression")
    .WithWebDriver(driver)
    .Build();
ReportiumClient reportiumClient = new ReportiumClientFactory.CreatePerfectoReportiumClient(perfectoExecutionContext);

Add optional functionality

Reporting tags

Tags are used as a freestyle text for filtering the reports in the Reporting app.
The tags are added when creating the Reportium client, for example: .WithContextTags("Regression")

CI job information

Job information is used to add your test runs to the CI Dashboard. Use the withJob() method of the PerfectoExecutionContext instance , supplying the Job Name and Job Number and Branch, when creating the ReportiumClient instance.

private static ReportiumClient CreateReportingClient(RemoteWebDriver driver)
{
   PerfectoExecutionContext perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
      .WithProject(new Project("My first project", "v1.0")) //optional 
      .WithContextTags(new[] { "sample tag1", "sample tag2", "c#" }) //optional 
      .WithJob(new Job("Job name", 12345, "master")) //optional 
      .WithWebDriver(driver)
      .Build();
   return PerfectoClientFactory.createPerfectoReportiumClient(perfectoExecutionContext);
}


Custom fields

Create new CustomField instances with the CustomField class constructor. Use the WithCustomFields() method to add a collection of Custom Fields to either the PerfectoExecutionContext instance or to the specific TestContext instance (either at start or end of test) as shown below.

Add test information and steps

Start a new test

[Test]
public void RemoteWebDriverExtendedTest()
{
   //Starting a test with the following context tags.
	reportiumClient.TestStart("myTestName", new TestContext.Builder()
                                        .WithContextTags(new[] { "sample tag1", "sample tag2", "c#" }) 
                                        .WithCustomFields(new[] { new CustomField("version", "OS11") })
                                        .Build());
	...
}

Add test steps

Separate your test into logical groupings of actions as logical steps. Each step is labeled and appears in the Execution Report together with the component actions. When implementing the test, indicate the beginning of the logical step with the StepStart() method, providing the label of the step, and (optionally) the StepEnd() method to indicate the end of the logical step (for the report). The StepEnd() method supports an optional message parameter that would be included in the execution report.

[Test]
public void MyTest()
{
	reportiumClient.TestStart("myTest", new TestContext("Sanity"));

	//test step - login to app
	reportiumClient.StepStart("Login to application");
	IWebElement username = driver.FindElement(By.Id("username"));
	username.SendKeys("myUser");
	driver.FindElement(By.Name("submit")).Click();
	reportiumClient.StepEnd();  // the message parameter is optional

	//test step - open a premium acct
	reportiumClient.StepStart("Open a premium account");
	IWebElement premiumAccount = driver.FindElement(By.Id("premium-account"));
	Assert.AreEqual(premiumAccount.Text, "PREMIUM");
	premiumAccount.Click();
	reportiumClient.StepEnd("Finished step");

	reportiumClient.StepStart("Transfer funds");
	...
}

Add assertions to the execution report

At various points within a test execution, the script may perform verification of different test conditions. The result of these verifications may be added to the test report by using the reportingAssertion() method of the ReportiumClient instance. When using this method, the script includes two parameters:

  • A message string used to label the assertion
  • A boolean value indicating the result of the verification operation
//test step - open a premium account
reportiumClient.StepStart("Open a premium account");
IWebElement premiumAccount = driver.FindElement(By.Id("premium-account"));
if (premiumAccount.Text.Equals("premium", StringComparison.InvariantCultureIgnoreCase))
{
	reportiumClient.ReportiumAssert("Check for Premium account", true);
	premiumAccount.Click();
}
else
{
	reportiumClient.ReportiumAssert("Check for Premium account", false);
}
reportiumClient.StepEnd(); // the message parameter is optional

Stop the test

When the test is completed, supply an indication of the final outcome of the test by generating a TestResult instance. The TestResultFactory class supports:

  • createSuccess method - that notifies the reporting server that the test resulted in a successful status.
  • createFailure method - that notifies the reporting server that the test resulted in a unsuccessful status and supports adding a notification message that is displayed in the test report.
    You can also provide a failure reason or depend on Smart Reporting analysis to identify the failure reason.
[Test]
public void MyTest()
{
    string failR = "Element not found";

    reportiumClient.TestStart("myTest", new TestContext("Sanity"));

    try
    {
        //test step - login to app
        reportiumClient.StepStart("Login to application");
        IWebElement username = driver.FindElement(By.Id("username"));
        username.SendKeys("myUser");
        driver.FindElement(By.Name("submit")).Click();
        reportiumClient.StepEnd();  // the message parameter is optional

        //test step - open a premium acct
        reportiumClient.StepStart("Open a premium account");
        IWebElement premiumAccount = driver.FindElement(By.Id("premium-account"));
        Assert.AreEqual(premiumAccount.Text, "PREMIUM");
        premiumAccount.Click();
        reportiumClient.StepEnd("Finished step");

        reportiumClient.StepStart("Transfer funds");
        ...

        //stopping the test - success
        reportiumClient.TestStop(TestResultFactory.CreateSuccess());
    }
    catch (Exception ex)
    {
        //stopping the test - failure
        reportiumClient.TestStop(TestResultFactory.CreateFailure(ex.ToString(), ex, failR));
    }
        
        
}

In addition to providing the status of the test result, you can provide additional tags and custom fields to the test, for example to add indications of the paths that caused the result or the reason for stopping the test. Use the TestContext to add additional tags and custom fields:

string testStopTag = "Failure reason test";
CustomField testStopCustomField = new CustomField("source", "sdk");
//Code for the test end checking
//stopping the test - failure
TestContext testContextEnd = new TestContext.Builder()
                    .WithTestExecutionTags(testStopTag)
                    .WithCustomFields(testStopCustomField)
                    .Build();
reportiumClient.TestStop(TestResultFactory.CreateFailure(expectedFailureMessage, new Exception(eMessage), failReason), testContextEnd);

Get the report URL

Get the report link by calling the reporting client, as shown here.

[ClassCleanup]
public static void classCleanUpMethod()
{
	driver.Close();

  	driver.Quit();
  // Retrieve the URL of the Single Test Report, can be saved to your execution summary and used to download the report at a later point
	String reportURL = reportiumClient.getReportUrl();
  // For documentation on how to export reporting PDF, see https://github.com/perfectocode/samples/wiki/reporting
   	String reportPdfUrl = (String)(driver.Capabilities.GetCapability("reportPdfUrl"));
   }
   
}

A single report for multiple executions

You can create a reporting client for multiple drivers. This results in a single execution report that combines all drivers executions and will result in unified report that will contain all executions as a single merged execution.

This type of flow is for scenarios where you want to create a unified execution to multiple drivers flows. For example: One driver that corresponds to a taxi driver that receives requests from passengers and one driver that corresponds to the passenger that asks for a taxi.

Use the ReportiumClientFactory class createPerfectoReportiumClient() method to create the DigitalZoom ReportiumClient instance. The reporting client is responsible to gather basic information about the test and transmit it to the Smart Reporting system

Before creating the ReportiumClient, you should create the instance of the automation driver (either SeleniumDriver or one of the Appium drivers), and use a PerfectoExecutionContext to supply the link to the factory class creating the client instance. Use the withWebDriver() method multiple times to supply the link of the drivers instances and their aliases (Optional). The context supports all of the optional settings described below, in addition to supplying the drivers links. Use the build() method to create the context object's instance and supply this to the createPerfectoReportiumClient() method when creating the ReportiumClient instance.

PerfectoExecutionContext perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
            .WithProject(new Project("Sample Reportium project", "1.0"))
            .WithJob(new Job("IOS tests", 45, "branch-name"))
            .WithCustomFields(new[] { new CustomField("programmer", "Samson") })
            .WithContextTags(new[] { "Regression"})
            .WithWebDriver(driver1, "Alias1")
            .WithWebDriver(driver2, "Alias2")
            .WithWebDriver(driver3, "Alias3")
            .Build();
ReportiumClient reportiumClient =  PerfectoClientFactory.CreatePerfectoReportiumClient(perfectoExecutionContext);

GitHub Samples

NUnit

SpecFlow

UnitTesting

Browse Perfecto GitHub repo for complete C# Reporting samples