Отчеты об объеме 4.0.3 - снимок экрана не работает - c #, specflow - PullRequest
1 голос
/ 30 апреля 2019

Я пытаюсь добавить снимок экрана в отчет по экстентам.Я могу сделать снимок экрана с помощью веб-драйвера selenium, но прикрепление снимка экрана к отчету о экстентах не работает.

Код в хуке [AfterStep] фактически создаст шаг для ошибки в отчете.Я не включил код для шага передачи.

step.AddScreenCaptureFromPath(screenshotPath) 

код выполняется успешно, но не удается найти вложение в отчете.

Это код Hooks, пытающийся сгенерировать отчет для функции BDD.

    //Global Variable for Extend report
    private static ExtentTest featureName;
    private static ExtentTest scenario;
    private static ExtentTest step;
    private static ExtentReports extent;

    //Initialize extent reports
    [BeforeTestRun]
    public static void InitializeReport()
    {
    //initialize extent report before test starts
    string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

    var htmlReporter = new ExtentHtmlReporter(assemblyFolder + "\\Reports\\ExtentReport.html");
    htmlReporter.Config.Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;

    //Attach report to reporter
    extent = new ExtentReports();
    extent.AttachReporter(htmlReporter);
    }

    //generate feature name dynamically
    [BeforeFeature]
    public static void BeforeFeature()
    {
    //Create dynamic feature name
    featureName = extent.CreateTest<Feature>(FeatureContext.Current.FeatureInfo.Title);
    }

   //generate scenario name dynamically
    [BeforeScenario]
    public void Initialize()
    {

    //Create dynamic scenario name
    scenario = featureName.CreateNode<Scenario>(ScenarioContext.Current.ScenarioInfo.Title);
    }

//get the screenshot upon failure
[AfterStep]
public void InsertReportingSteps()
{

    var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();
    PropertyInfo pInfo = typeof(ScenarioContext).GetProperty("ScenarioExecutionStatus", BindingFlags.Instance | BindingFlags.Public);
    MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
    object TestResult = getter.Invoke(ScenarioContext.Current, null);

    if (ScenarioContext.Current.TestError != null)
    {
        if (stepType == "Given"){
            step = scenario.CreateNode<Given>(stepType + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
        }
        else if (stepType == "When"){
            step = scenario.CreateNode<When>(stepType + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
        }
        else if (stepType == "Then"){
            step = scenario.CreateNode<Then>(stepType + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
        }
        string screenshotPath = CaptureScreenshot.Capture(driver);

        //Capture screenshot not attaching screenshot to extent reports
        step.AddScreenCaptureFromPath(screenshotPath);
    }

}
   //flush extent reports
    [AfterTestRun]
    public static void TearDownReport()
    {
    //Flush report once test completes
    extent.Flush();
    }

class CaptureScreenshot
    {
        public static string Capture(IWebDriver driver)
        {
            ITakesScreenshot ts = (ITakesScreenshot)driver;
            Screenshot screenshot = ts.GetScreenshot();
            string screenShotName = "screenShotName_" + Calendar.getCurrentDate();
            string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string finalpth = assemblyFolder + "\\Reports\\Screenshots\\";
            string screenshotDir = finalpth + screenShotName + ".png";
            string localpath = new Uri(screenshotDir).LocalPath;
            screenshot.SaveAsFile(localpath);
            return localpath;
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...