C#
Following are the steps for implementing the reporting framework in C#.
On this page:
Download the client
Download the Reporting SDK client for your programming languages and framework.
Set up Smart Reporting
Add the following elements to your automation code to take advantage of Perfecto Smart Reporting.
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;
Create the reporting client
Use the ReportiumClientFactory class's 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 reporting tags (optional)
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")
Add CI job information (optional)
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)
{
string[] contextTags = ["Regression"];
PerfectoExecutionContext perfectoExecutionContext = new PerfectoExecutionContext.PerfectoExecutionContextBuilder()
.WithProject(new Project("Appium CSharp Project", "1.0")) //optional
.WithJob(new Job("Tests", 1) //optional //optional
.WithBranch("branch-name"))
.WithCustomFields(new CustomField[] { new("language", "csharp") }) //optional
.WithContextTags(contextTags) //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
In the following sections, you learn how to manage the lifecycle of an individual test in Smart Reporting. You start a test to establish a reporting boundary, report logical steps and actions as the test executes, and then close the test when execution is complete. This structure enables Smart Reporting to group steps, artifacts, and results under a single test entry for accurate analysis in the Test Analysis view.
Start a new test
[Test]
public void RemoteWebDriverExtendedTest()
{
//Starting a test with the following context tags.
var testContext = new Reportium.Test.TestContext.Builder()
.WithTestExecutionTags(new[] { "sample tag1", "sample tag2", "c#" })
.WithCustomFields(new[] { new CustomField("version", "OS11") })
.Build();
reportiumClient.TestStart("myTestName", testContext);
}
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.
string testStopTag = "Failure reason test";
CustomField testStopCustomField = new CustomField("source", "sdk");
//Code for the test end checking
//stopping the test - failure
var testContextEnd = new Reportium.Test.TestContext.Builder()
.WithTestExecutionTags(testStopTag)
.WithCustomFields(testStopCustomField)
.Build();
reportiumClient.TestStop(TestResultFactory.CreateFailure(expectedFailureMessage, new Exception(eMessage), failReason), testContextEnd);
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
Browse the Perfecto GitHub repo for complete C# Reporting samples.