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;
}
}
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();
- ExtentReports will create the report file.
- ExtentTest will log the information in the report.
- StartTest() method of ExtentReports class is the starting point of the test and it will return the ExtentTest object.
- We need to capture that object into ExtentTest object.
- Used this reference to log the information into the report.
- ExtentReports object will be used to add the report information like Title, Header and Theme etc..
- 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.
- EndTest() method of ExtentReports will stop capturing information about the test log.
- Flush() method of ExtentReports will push/write everything to the document.
- Close() method of ExtentReports will clear/close all resource of the ExtentReports object
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();
}
}