Search code examples
c#automated-testsatata

Pass custom screenshot consumer though json config


Hi,

I use Atata framework with ExtentReports, based on this project: https://github.com/atata-framework/atata-samples/tree/master/ExtentReports

Now I want switch from fluent context build to json config. My fluent context build looks like:

    AtataContext.GlobalConfiguration
        .UseChrome()
            .WithArguments("--start-maximized")
        .WithLocalDriverPath()
        .WithFixOfCommandExecutionDelay()
        .UseCulture("en-US")
        .UseAllNUnitFeatures()
        .AddDebugLogging()
        .AddScreenshotFileSaving()
            .WithArtifactsFolderPath()
        .AddLogConsumer(new ExtentLogConsumer())
            .WithMinLevel(LogLevel.Info)
        .EventSubscriptions.Add(new ExtentScreenshotFileEventHandler());
    
    AtataContext.GlobalConfiguration.AutoSetUpDriverToUse();

I can implement via json config almost all, except this line:

.EventSubscriptions.Add(new ExtentScreenshotFileEventHandler());

My config:

{
  "driver": {
    "type": "chrome",
    "alias": "chrome",
    "options": {
      "arguments": [ "start-maximized" ],
      "userProfilePreferences": {
        "download.default_directory": "{artifacts}"
      }
    }
  },
  "culture": "en-US",
  "useAllNUnitFeatures": true,
  "logConsumers": [
    {
      "type": "nlog-file",
      "folderPath": "{artifacts}",
      "minLevel": "Debug"
    },
    {
      "type": "AtataUITests1.Core.Reporting.ExtentLogConsumer, AtataUITests1",
      "minLevel": "Info"
    }
  ],
  "screenshotConsumers": [
    {
      "type": "file",
      "folderPath": "{artifacts}"
    }
  ]
}

When I try to add new screenshotConsumer to json:

  "screenshotConsumers": [
    {
      "type": "file",
      "folderPath": "{artifacts}"
    },
    {
      "type": "AtataUITests1.Core.Reporting.ExtentScreenshotFileEventHandler, AtataUITests1"
    }
  ]

it shows error:

System.InvalidCastException : Unable to cast object of type 'AtataUITests1.Core.Reporting.ExtentScreenshotFileEventHandler' to type 'Atata.IScreenshotConsumer'.

My question is: is it possible to pass this custom screenshot consumer via json?

Thanks


Solution

  • ExtentScreenshotFileEventHandler is not a screenshot consumer but an event handler. So it should be placed in "eventSubscriptions" section as below:

    "eventSubscriptions": [
      {
        "handlerType": "AtataUITests1.Core.Reporting.ExtentScreenshotFileEventHandler, AtataUITests1"
      }
    ]
    

    There is also an example on Atata.Configuration.Json / JSON Schema section.