如何解决在Selenium C#中无法捕获具有范围报告4.1.0的所有测试用例
我的代码运行正常,但在“报表”中仅显示最后执行的测试用例。 有人可以在这种情况下帮助我吗?
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.IO;
namespace ClassLibrary1.Config
{
[SetUpFixture]
public abstract class ReportsGenerationClass
{
protected ExtentReports _extent;
protected ExtentTest _test;
public IWebDriver _driver;
[OneTimeSetUp]
protected void Setup()
{
var path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
var actualPath = path.Substring(0,path.LastIndexOf("bin"));
var projectPath = new Uri(actualPath).LocalPath;
DateTime time = DateTime.Now;
Directory.CreateDirectory(projectPath.ToString() + "Reports");
var reportPath = projectPath + "Reports\\ExtentReport_"+ time.ToString("h_mm_ss") +".html";
var htmlReporter = new ExtentHtmlReporter(reportPath);
_extent = new ExtentReports();
_extent.AttachReporter(htmlReporter);
_extent.AddSystemInfo("Host Name","LocalHost");
_extent.AddSystemInfo("Environment","QA");
_extent.AddSystemInfo("UserName","TestUser");
}
[OneTimeTearDown]
protected void TearDown()
{
_extent.Flush();
}
[SetUp]
public void Beforetest()
{
_driver = new ChromeDriver();
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
_driver.Manage().Window.Maximize();
_test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
}
[TearDown]
public void Aftertest()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace) ? ""
: string.Format("{ 0}",TestContext.CurrentContext.Result.StackTrace);
Status logstatus;
switch (status)
{
case TestStatus.Failed:
logstatus = Status.Fail;
DateTime time = DateTime.Now;
String fileName = "Screenshot_" +time.ToString("h_mm_ss") + ".png";
String screenShotPath = Capture(_driver,fileName);
_test.Log(Status.Fail,"Fail");
_test.Log(Status.Fail,"Snapshot below: " +_test.AddScreenCaptureFromPath("Screenshots\\" +fileName));
break;
case TestStatus.Inconclusive:
logstatus = Status.Warning;
break;
case TestStatus.Skipped:
logstatus = Status.Skip;
break;
default:
logstatus = Status.Pass;
break;
}
_test.Log(logstatus,"Test ended with " +logstatus + stacktrace);
// _extent.Flush();
// _driver.Quit();
}
public IWebDriver GetDriver()
{
return _driver;
}
public static string Capture(IWebDriver driver,String screenShotName)
{
ITakesScreenshot ts = (ITakesScreenshot)driver;
Screenshot screenshot = ts.GetScreenshot();
var pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
var actualPath = pth.Substring(0,pth.LastIndexOf("bin"));
var reportPath = new Uri(actualPath).LocalPath;
Directory.CreateDirectory(reportPath + "Reports\\" + "Screenshots");
var finalpth = pth.Substring(0,pth.LastIndexOf("bin")) + "Reports\\Screenshots\\" +screenShotName;
var localpath = new Uri(finalpth).LocalPath;
screenshot.SaveAsFile(localpath,ScreenshotimageFormat.Png);
return reportPath;
}
}
}
我的两个不同类别的测试用例
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary1.Config;
using ClassLibrary1.PageMethods;
namespace ClassLibrary1.TestCases
{
[TestFixture]
public class Login1 : ReportsGenerationClass
{
LoginPage loginPage;
[Test]
[Category("Login")]
public void test_validLogin1()
{
loginPage = new LoginPage(GetDriver());
loginPage.goToPage();
loginPage.closebrowser();
}
[Test]
[Category("Login")]
public void test_invalidLogin1()
{
loginPage = new LoginPage(GetDriver());
loginPage.goToPage();
loginPage.closebrowser();
}
}
}
第二堂课
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary1.Config;
using ClassLibrary1.PageMethods;
namespace ClassLibrary1.TestCases
{
[TestFixture]
public class LoginTest : ReportsGenerationClass
{
LoginPage loginPage;
[Test]
[Category("Login")]
public void test_validLogin()
{
loginPage = new LoginPage(GetDriver());
loginPage.goToPage();
}
[Test]
[Category("Login")]
public void test_invalidLogin()
{
loginPage = new LoginPage(GetDriver());
loginPage.goToPage();
loginPage.closebrowser();
}
}
}
执行后,两个类仅显示2个测试用例,显示内部扩展区报告。 您能帮我解决这个问题吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。