我为所有报告创建了一个基类。在该类中,我添加了一个日志状态,当它失败时应该在范围报告中添加屏幕截图。我无法显示屏幕截图。我还有一个类,用于使用三种不同的方法来截取屏幕截图。我不确定哪一个是正确的,因为它是由另一个人设置的。
using AventStack.ExtentReports;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TadsSeleniumTests.Setup
{
[SetUpFixture]
public abstract class BaseReport : ExtentManager
{
[OneTimeSetUp]
public void SetupReporting()
{
GetInstance();
}
[OneTimeTearDown]
public void GenerateReport()
{
Extent.Flush();
}
public void CreateTest(string name)
{
Test = Extent.CreateTest(name);
//Test.Log(Status.Pass, "Pass");
//Test.Log(Status.Fail, "Fail");
}
// ---------------------------------------------------------------------------------------------
// Public Methods
// ---------------------------------------------------------------------------------------------
public void LogTestStatus()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
? ""
: $"{TestContext.CurrentContext.Result.StackTrace}";
var message = string.IsNullOrEmpty(TestContext.CurrentContext.Result.Message)
? ""
: $"{TestContext.CurrentContext.Result.Message}";
Status logStatus;
switch (status)
{
case TestStatus.Failed:
logStatus = Status.Fail;
break;
case TestStatus.Inconclusive:
logStatus = Status.Warning;
break;
case TestStatus.Skipped:
logStatus = Status.Skip;
break;
default:
logStatus = Status.Pass;
break;
}
if (status==NUnit.Framework.Interfaces.TestStatus.Failed)
{
Test.Log(logStatus, "<b>" + "ERROR MESSAGE: " + "</b>" + message);
Test.Log(logStatus, "<b>" + "STACKTRACE: " + "</b>" + stacktrace);
// Take a screenshot and attach it to report on failure, ideas here, can't get any of them working yet:
string screenShotPath = TakeScreenshot.Capture(Driver, "FAILURE");
Test.Log(logStatus, "Screenshot: " + Test.AddScreenCaptureFromPath(screenShotPath));
Test.Fail("details", MediaEntityBuilder.CreateScreenCaptureFromPath(screenShotPath).Build());
}
else
{
Test.Log(logStatus, "Test ended with " + "<b>" + logStatus + "</b>");
}
}
using System;
using System.Drawing.Imaging;
using System.Reflection;
using AventStack.ExtentReports;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.Extensions;
namespace TadsSeleniumTests.Setup
{
public class TakeScreenshot
{
public static ExtentTest Test;
protected static string Capture(IWebDriver driver, string v)
{
throw new NotImplementedException();
}
// public static string Capture(IWebDriver driver, string screenShotName)
// {
// ITakesScreenshot ts = (ITakesScreenshot)driver;
// Screenshot screenshot = ts.GetScreenshot();
// string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
// string finalpth = pth.Substring(0, pth.LastIndexOf("bin")) + "ErrorScreenshots\\" + screenShotName + ".png";
// string localpath = new Uri(finalpth).LocalPath;
// screenshot.SaveAsFile(localpath, ScreenshotImageFormat.Png);
// var mediaModel = MediaEntityBuilder.CreateScreenCaptureFromPath(finalpth).Build();
// Test.Fail(screenShotName, mediaModel);
//
// return localpath;
// public static string Capture1(IWebDriver edriver, string screenshotName)
// {
// var screenshot = edriver.TakeScreenshot();
// string currentTime = DateTime.Now.ToString("MMM-d-HH.mm");
// var filePath = "C:\\Users\\..";
// screenshot.SaveAsFile(filePath + currentTime + screenshotName + ".png", ImageFormat.Png);
// return filePath;
public static string ScreenShot(IWebDriver wdriver, string imgName)
{
var screenshot = wdriver.TakeScreenshot();
string localpath = "C:\\Users\\.." + imgName + ".png";
screenshot.SaveAsFile(localpath, ScreenshotImageFormat.Png);
return localpath;
}
}