【发布时间】:2022-08-24 19:06:44
【问题描述】:
我使用的是 Extent 报告 v4,当我尝试从不同的类运行多个测试时,我只能在报告中找到我的最后一个测试。例如,如果我有 10 个并行运行的测试,我只会找到最后一个运行的测试。我需要在报告中找到所有 10 项测试。我在 c# 中使用 Selenium 4。
这是我使用的代码。
public class DriverHelper
{
//public static IWebDriver driver { get; set; }
public ExtentReports extent;
public ExtentTest test;
[OneTimeSetUp]
public void Setup()
{
String workingDirectory = Environment.CurrentDirectory;
string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;
string reportPath = projectDirectory + \"//index.html\";
var htmlReporter = new ExtentHtmlReporter(reportPath);
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
extent.AddSystemInfo(\"Host Name\", \"Gold end to end\");
extent.AddSystemInfo(\"Tester\", \"Arshad\");
}
public ThreadLocal<IWebDriver> driver = new ThreadLocal<IWebDriver>();
[SetUp]
public void StartBrowser()
{
test = extent.CreateTest(TestContext.CurrentContext.Test.Name);
var browserSetup = new BrowserSetup();
driver.Value = browserSetup.SetupBrowser();
}
[TearDown]
public void Test1()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stackTrace = TestContext.CurrentContext.Result.StackTrace;
DateTime time = DateTime.Now;
string fileName = \"Screenshot_\" + time.ToString(\"h_mm_ss\") + \".png\";
if (status == TestStatus.Failed)
{
test.Fail(\"Test failed\", captureScreenshot(driver.Value, fileName));
test.Log(Status.Fail, \"Test failed with logtrace\" + stackTrace);
}
else if (status == TestStatus.Passed)
{
test.Log(Status.Pass, \"Test successful\");
}
//extent.Flush();
driver.Value.Quit();
}
[OneTimeTearDown]
public void Test2()
{
extent.Flush();
}
public MediaEntityModelProvider captureScreenshot(IWebDriver driver, String screenShotName)
{
ITakesScreenshot ts = (ITakesScreenshot)driver;
var screenshot = ts.GetScreenshot().AsBase64EncodedString;
return MediaEntityBuilder.CreateScreenCaptureFromBase64String(screenshot, screenShotName).Build();
}
}
}
标签: c# selenium browserstack extentreports