C# 拍摄测试失败的屏幕截图+;例外情况
你们中有谁知道拍摄测试失败和异常截图的可能解决方案吗 我在C# 拍摄测试失败的屏幕截图+;例外情况,c#,.net,unit-testing,selenium,nunit,C#,.net,Unit Testing,Selenium,Nunit,你们中有谁知道拍摄测试失败和异常截图的可能解决方案吗 我在TearDown()中添加了以下代码,但因此它也会对通过的测试进行截图,因此它不是最佳解决方案: DateTime time = DateTime.Now; string dateToday = "_date_" + time.ToString("yyyy-MM-dd") + "_time_" + time.ToString("HH-mm-ss"); Screenshot screenshot = ((ITakesScreenshot)d
TearDown()
中添加了以下代码,但因此它也会对通过的测试进行截图,因此它不是最佳解决方案:
DateTime time = DateTime.Now;
string dateToday = "_date_" + time.ToString("yyyy-MM-dd") + "_time_" + time.ToString("HH-mm-ss");
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile((settings.filePathForScreenShots + "Exception" + dateToday + ".png"), System.Drawing.Imaging.ImageFormat.Png);
我已经发现了这个想法:,使用webdriverceptioneventargs
,但出于某些原因,它也会在没有任何合理解释的情况下随机截图
我发现的其他想法是针对Java的,而不是针对我与Selenium一起使用的NUnit,因此它们是非常无用的。如果将屏幕截图逻辑放在TearDown方法中,则每次测试完成后都会调用它,无论它是成功还是失败 我使用一个基类,它有一个封装测试并捕获所有异常的函数。当测试失败时,将捕获异常并拍摄屏幕截图 我在所有Selenium测试中都使用这个基类,它看起来像这样:
public class PageTestBase
{
protected IWebDriver Driver;
protected void UITest(Action action)
{
try
{
action();
}
catch (Exception ex)
{
var screenshot = Driver.TakeScreenshot();
var filePath = "<some appropriate file path goes here>";
screenshot.SaveAsFile(filePath, ImageFormat.Png);
// This would be a good place to log the exception message and
// save together with the screenshot
throw;
}
}
}
[TestFixture]
public class FooBarTests : PageTestBase
{
// Make sure to initialize the driver in the constructor or SetUp method,
// depending on your preferences
[Test]
public void Some_test_name_goes_here()
{
UITest(() =>
{
// Do your test steps here, including asserts etc.
// Any exceptions will be caught by the base class
// and screenshots will be taken
});
}
[TearDown]
public void TearDown()
{
// Close and dispose the driver
}
}
您可以在TestNG套件文件中轻松实现这一点 创建一个屏幕截图方法,如下所示
public static void CaptureDesktop (String imgpath)
{
try
{
Robot robot = new Robot();
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRect = new Rectangle(screensize);
BufferedImage screenshot = robot.createScreenCapture(screenRect);
//RenderedImage screenshot = robot.createScreenCapture(screenRect);
ImageIO.write(screenshot, "png" , new File(imgpath));
}
在上面的方法中,我使用了robot类,这样您也可以拍摄Dekstop(窗口+网页)的屏幕截图,并且您可以在不同的监听器类中调用此方法,该类将实现ITestListener接口。在该侦听器类的OntestFailure()中调用屏幕截图方法
@Override
public void onTestFailure(ITestResult arg0) {
String methodname = arg0.getMethod().getMethodName();
String imgpath = "./Screenshot/"+methodname+".jpg";
Guru99TakeScreenshot.CaptureDesktop(imgpath);
}
这个代码对我有用。但这段代码是用JAVA编写的。我希望这在C#中能起作用;如果不能,我希望这段代码能帮助您在我使用的C#中使用。这提供了onetimeeardown
方法,该方法能够访问TestContext
,包括先前执行的测试的状态。不要使用TearDown
,因为它在测试失败后不会执行;)
为了更公正,这里是MSTest的代码
public TestContext TestContext { get; set; }
[TestCleanup]
public void TestCleanup()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
{
var screenshotPath = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss.fffff}.png";
MyDriverInstance.TakeScreenshot().SaveAsFile(screenshotPath);
TestContext.AddResultFile(screenshotPath);
}
}
定制一点ExtentReport可以提供非常有用的报告,在测试失败时准确捕获异常+屏幕截图。屏幕截图可以放在异常旁边,用户可以使用该异常来了解发生错误时网站在做什么 报告示例 试验 后置法
@AfterMethod
public void getResult(ITestResult result) throws Exception{
if(result.getStatus() == ITestResult.FAILURE)
{
extentlogger.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
try {
// get path of captured screenshot using custom failedTCTakeScreenshot method
String screenshotPath = failedTCTakeScreenshot( result);
extentlogger.fail("Test Case Failed Snapshot is below " + extentlogger.addScreenCaptureFromPath(screenshotPath));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
如果您能够捕获测试的名称并将其包含在屏幕截图的文件名中,那就太好了。“但我不明白用这种方法怎么能做到?”弗兰克。在本例中,您应该能够通过UITest方法中的反射来实现这一点。由于UITest方法是从测试方法调用的,因此您应该能够从UITest方法获取调用方法的名称。尝试以下方法:您确定您的异常正确地记录了行号吗?是否记录了来自正确程序集的异常?我们做了一些类似的事情,但由于使用包装器时AssemblyInfo不同,我们丢失了行号。为了澄清,我们记录了带有正确消息的异常,但行号信息丢失,并且始终为0。您的情况也是这样吗?只要任何安装方法运行时没有错误,拆卸方法就可以保证运行。我刚刚找到了你的答案,并使用了if语句,没有任何问题。不过,我认为这应该是公认的答案。如果我错了,请纠正我,但我认为
onetimereadown
是在夹具中的所有测试完成之后进行的。因此,现在获取任何屏幕截图都为时已晚。我同意trailmax。当其他东西或其他测试点击其他东西时,就太晚了。它应该在catch块中完成,我喜欢上面示例中的global try catch示例,此外,您可以在扩展方法中使用它。您从哪里获得范围
对象?
@Test (enabled=true)
public void verifySearch() {
extentlogger = extent.createTest("To verify verifySearch");
//Your other code here.....
soft.assertEquals("xxx", "xxxx");
soft.assertAll();
}
@AfterMethod
public void getResult(ITestResult result) throws Exception{
if(result.getStatus() == ITestResult.FAILURE)
{
extentlogger.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
try {
// get path of captured screenshot using custom failedTCTakeScreenshot method
String screenshotPath = failedTCTakeScreenshot( result);
extentlogger.fail("Test Case Failed Snapshot is below " + extentlogger.addScreenCaptureFromPath(screenshotPath));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}