anshooarora / extentreports-csharp

Community version of Extent API for .NET has moved to https://github.com/extent-framework/
http://extentreports.com/
Other
47 stars 43 forks source link

Cannot get screencapture working - is there somehting I need to install? #51

Open jloyzaga opened 7 years ago

jloyzaga commented 7 years ago

code is below `using AventStack.ExtentReports; using AventStack.ExtentReports.Reporter; using AventStack.ExtentReports.Reporter.Configuration; using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System;

namespace FrameworkForJoe { [TestFixture] public class SampleTest { private static IWebDriver driver; private static ExtentReports extent; private static ExtentHtmlReporter htmlReporter; private static ExtentTest test;

    [OneTimeSetUp]
    public void SetupReporting()
    {
        htmlReporter = new ExtentHtmlReporter(@"C:\Git\joesreport.html");

        htmlReporter.Configuration().Theme = Theme.Standard;

        htmlReporter.Configuration().DocumentTitle = "JoesDocument";

        htmlReporter.Configuration().ReportName = "JoesReport";
        htmlReporter.Configuration().JS = "$('.report-name').text('    Test Report').prepend('<img src=file://///corp.capgemini.com/au/DATA/Sydney/Disciplines/Testing/Capgemini_logo_high_res.jpg width=50% height=100%>')";
        extent = new ExtentReports();

        extent.AttachReporter(htmlReporter);
        /*extent.(Status.Info, "HTML", "Usage: BOLD TEXT");*/
       /* htmlReporter.Configuration().ChartLocation = ChartLocation.Bottom;*/

    }

    [SetUp]
    public void InitBrowser()
    {
        driver = new ChromeDriver();
        driver.Manage().Window.Maximize();            
    }

    [Test]
    public void PassingTest()
    {
        test = extent.CreateTest("Passing test");
        test.AssignCategory("Regression");
        driver.Navigate().GoToUrl("http://www.google.com");

        try {
            Assert.IsTrue(true);
            test.Pass("Assertion passed");
            /*test.Fail("Assertion failed");*/
            test.Fail("details").AddScreenCaptureFromPath("screenshot.png");
            test.Fail("details", MediaEntityBuilder.CreateScreenCaptureFromPath("screenshot.png").Build());
            test.Fail("details").AddScreenCaptureFromPath("screenshot.png");
            test.Log(Status.Info, "This gives log info if needed");
            test.Info("This step shows usage of info(details)");

        }
        catch (AssertionException)
        {
            test.Fail("Assertion failed");
            test.Fail("details").AddScreenCaptureFromPath("screenshot.png");
            test.Fail("details", MediaEntityBuilder.CreateScreenCaptureFromPath("screenshot.png").Build());
            test.Fail("details").AddScreenCaptureFromPath("screenshot.png");
            test.Log(Status.Info, "This gives log info if needed");
            test.Info("This step shows usage of info(details)");

            throw;
        }
    }

    [Test]
    public void FailingTest()
    {
        test = extent.CreateTest("Failing test");
        test.AssignCategory("Regression");

        driver.Navigate().GoToUrl("http://www.yahoo.com");

        try
        {
            Assert.IsTrue(false);
            test.Pass("Assertion passed");
        }
        catch (AssertionException)
        {
            test.Fail("Assertion failed");
            test.Fail("Assertion failed");
            test.Fail("details").AddScreenCaptureFromPath("screenshot.png");
            test.Fail("details", MediaEntityBuilder.CreateScreenCaptureFromPath("screenshot.png").Build());
            test.Fail("details").AddScreenCaptureFromPath("screenshot.png");
            test.Log(Status.Info, "This gives log info if needed");
            test.Info("This step shows usage of info(details)");

            throw;
        }
    }

    [TearDown]
    public void CloseBrowser()
    {
        driver.Quit();
    }

    [OneTimeTearDown]
    public void GenerateReport()
    {
        extent.Flush();
    }
}

} `

jloyzaga commented 7 years ago

image missing icon showing where capture should be

jloyzaga commented 7 years ago

joesreport.zip

xenaree commented 7 years ago

If the image is showing with missing icon, it's probably because the path you specified wasn't right. I'm using the following method to capture screenshot:

public static void ScreenShot(IWebDriver driver, string imgName)
        {
            string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string projectPath = new Uri(actualPath).LocalPath;            
            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile(projectPath + @"com.seloger.resources\screenshots\" +imgName+".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

In your test, just call the screenshot method then add the screenshot to the log.

ScreenShot(_driver, "exception");
_test.Log(LogStatus.Error, _test.AddScreenCapture(projectPath + @"com.seloger.resources\screenshots\exception.jpg"));
meghaagarwal2005 commented 7 years ago

Your screenshots should be placed in the same location as your Extent report or the path of the screenshot should be relative to the path of your report