Reputation: 35
Is there any approach of taking screenshot after every Assert verification? Screenshot should be taken irrespective of assert pass or fail.
Need strong suggestions
Upvotes: 0
Views: 692
Reputation: 72
If you are using Nunit. It supports multiple asserts.
Excerpt from the documentation (https://github.com/nunit/docs/wiki/Multiple-Asserts)
The multiple assert block may contain any arbitrary code, not just asserts.
Multiple assert blocks may be nested. Failure is not reported until the outermost block exits.
If the code in the block calls a method, that method may also contain multiple assert blocks.
Sample Use:
[Test]
public void ComplexNumberTest()
{
ComplexNumber result = SomeCalculation();
Assert.Multiple(() =>
{
Assert.AreEqual(5.2, result.RealPart, "Real part");
Assert.AreEqual(3.9, result.ImaginaryPart, "Imaginary part");
});
}
Hope this helps.
Upvotes: 0
Reputation: 5909
Yes, you can use Driver.TakeScreenshot();
You will need to find a file path to save your screenshot to, and save the file as well.
var screenshot = Driver.TakeScreenshot();
var filePathToSave = "C:\\Users\\YourFilePathHere";
// format as .png
screenshot.SaveAs(filePathToSave, ImageFormat.Png);
I recommend wrapping this in a method and calling it whenever you need to:
public void TakeScreenshot()
{
var screenshot = Driver.TakeScreenshot();
var filePathToSave = "C:\\Users\\YourFilePathHere";
// format as .png
screenshot.SaveAs(filePathToSave, ImageFormat.Png);
}
Then you can use it after an Assert
like this:
Assert.IsTrue(something);
Driver.TakeScreenshot();
You will also need to handle the case where your Assert
statement fails -- so you will need to implement this in a [TearDown]
method as well, to ensure the screenshot gets taken even when the test fails:
[TearDown]
public void TearDown()
{
// take screenshot
TakeScreenShot();
// close and quit driver
Driver.Close();
Driver.Quit();
}
Upvotes: 1