范围报告4.0.3-捕获屏幕截图不起作用-C#,Specflow

时间:2019-04-30 01:12:51

标签: c# specflow extentreports

我正在尝试将屏幕截图捕获添加到扩展报告。我可以使用Selenium Webdriver捕获屏幕截图,但是将屏幕截图附加到扩展报告无法正常工作。

[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;
        }
    }

1 个答案:

答案 0 :(得分:0)

我能够使用MediaEntityBuilder解决问题

https://extentreports.com/docs/versions/4/java/

MediaModelProvider mediaModel = 
MediaEntityBuilder.createScreenCaptureFromPath("screenshot.png").build();
test.fail("details", mediaModel);

// or:
test.fail("details", 
MediaEntityBuilder.createScreenCaptureFromPath("screenshot.png").build());

您需要在步骤中包括屏幕截图

step = scenario.CreateNode<Then>(stepType + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message,MediaEntityBuilder.CreateScreenCaptureFromPath(screenshotPath).Build());