Wednesday, September 25, 2019

Selenium-screenshot,extent report



  • C#- TakeScreenShot
https://www.guru99.com/take-screenshot-selenium-webdriver.html

public void TakeScreenshot()
{
    try
    {         
        Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile(@"D:\Screenshots\SeleniumTestingScreenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        throw;
    }
}


  • Extent Report 2.41
https://extentreports.com/docs/versions/2/net/

public class BasicReport
    {
        public ExtentReports extent;
        public ExtentTest test;

        [OneTimeSetUp]
        public void StartReport()
        {
            string projectPath=".......";
           string reportPath = @"E:Reports\MyOwnReport.html";

            extent = new ExtentReports(reportPath, true);
            extent
            .AddSystemInfo("Host Name", "Krishna")
            .AddSystemInfo("Environment", "QA")
            .AddSystemInfo("User Name", "Krishna Sakinala");
            extent.LoadConfig(projectPath + "extent-config.xml");
        }
       
        [Test]
        public void DemoReportPass()
        {
            test = extent.StartTest("DemoReportPass");
            Assert.IsTrue(true);
            test.Log(LogStatus.Pass, "Assert Pass as condition is True");
        }

        [Test]
        public void DemoReportFail()
        {
            test = extent.StartTest("DemoReportFail");
            Assert.IsTrue(false);
            test.Log(LogStatus.Pass, "Assert Fail as condition is False");
        }

        [TearDown]
        public void GetResult()
        {
            var status = TestContext.CurrentContext.Result.Outcome.Status;
            var stackTrace = "<pre>" + TestContext.CurrentContext.Result.StackTrace + "</pre>";
            var errorMessage = TestContext.CurrentContext.Result.Message;

            if (status == TestStatus.Failed)
            {
                test.Log(LogStatus.Fail, stackTrace + errorMessage);
            }
            extent.EndTest(test);
        }

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

    }
ExtentReports extent = new ExtentReports(file-path, false);

// new instance
var extent = new ExtentReports(file-path);

// starting test
var test = extent.StartTest("Test Name", "Sample description");

// step log
test.Log(LogStatus.PASS, "Step details");

// ending test
extent.EndTest(test);

// writing everything to document

extent.Flush();


  1. ExtentReports will create the report file.
  2. ExtentTest will log the information in the report.
  3. StartTest() method of ExtentReports class is the starting point of the test and it will return the ExtentTest object.
  4. We need to capture that object into ExtentTest object.
  5. Used this reference to log the information into the report.
  6. ExtentReports object will be used to add the report information like Title, Header and Theme etc..
  7. And the above configuration need to passed from the external XML file using LoadConfig() method of ExtentReports class. It will take the XML file path as argument.
  8. EndTest() method of ExtentReports will stop capturing information about the test log.
  9. Flush() method of ExtentReports will push/write everything to the document.
  10. Close() method of ExtentReports will clear/close all resource of the ExtentReports object


  • Extent Report 4

public class Main {
    public static void main(String[] args) {
        // start reporters
        var reporter = new ExtentHtmlReporter("path/to/directory/");

        // create ExtentReports and attach reporter(s)
        var extent = new ExtentReports();
        extent.AttachReporter(htmlReporter);

        // creates a test
        var test = extent.CreateTest("MyFirstTest", "Sample description");

        // log(Status, details)
        test.Log(Status.Info, "This step shows usage of log(status, details)");

        // info(details)
        test.Info("This step shows usage of info(details)");

        // log with snapshot
        test.Fail("details",
MediaEntityBuilder.CreateScreenCaptureFromPath("screenshot.png").Build());

        // test with snapshot
        test.AddScreenCaptureFromPath("screenshot.png");

        // calling flush writes everything to the log file
        extent.Flush();
    }
}




No comments:

Post a Comment

API interview questions

  https://www.katalon.com/resources-center/blog/web-api-testing-interview-questions/ Top 50+ Web API Testing Interview Questions [Ultimate l...