In Visual Studio, with C#-Selenium, I am not able to take screenshot, If script fail due to any exception in TestScript using Chrome browser only (For IE and Firefox - In same condition code working fine, Also If Script fail due to some Assert- Same code working file)
[TestCleanup]
public void TestCleanup()
{
string ssFolder = Path.Combine(Context.TestLogsDir, "Screenshots");
string idxStr = "0";
if (Context.DataRow != null)
{
var index = Context.DataRow.Table.Rows.IndexOf(Context.DataRow);
idxStr = index.ToString();
}
var screenshotFileName = $"{Path.Combine(ssFolder, Context.TestName)}_{idxStr}.png";
if (Context.CurrentTestOutcome == UnitTestOutcome.Failed)
{
// prep folder if needed
if (Directory.Exists(ssFolder) == false)
{
Directory.CreateDirectory(ssFolder);
}
// Now take a screenshot
DriverUtils.Instance.WebDriverInstance.TakeScreenshot(screenshotFileName, ImageFormat.Png);
TestUtils.log.Info($"**SCREENSHOT @ '{screenshotFileName}'**");
Context.AddResultFile(screenshotFileName);
}
TestUtils.log.Debug($"END TEST: {Context.FullyQualifiedTestClassName}.{Context.TestName}");
TestUtils.log.Debug("=================================================================");
DriverUtils.Instance.Kill();
}
//TakeScreenshot Method definition
public static void TakeScreenshot(this IWebDriver driver, string FileLocationName, ImageFormat imageFormat)
{
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(FileLocationName, imageFormat);
}
Exception: StackTrace :
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.GetScreenshot()
at DDS.Test.UITesting.Common.Extensions.SeleniumExtensions.TakeScreenshot(IWebDriver driver, String FileLocationName, ImageFormat imageFormat) in D:\src\XXX66\test\common\XXX.Test.UITesting.Common\XXXX.Test.UITesting.Common\Extensions\SeleniumExtensions.cs:line 143
at XXX.XXXX.Test.Common.BrowserInstancePerTestBase.TestCleanup() in D:\XXXX_Code\XXXX_Development\CRM\test\XXX.XXXX.Test\XXX.XXXX.Test.Common\BrowserInstancePerTestBase.cs:line 90
Step:
1. If there is some exception in Test Script, them after exception code goes to TestCleanup() method successfully :
2. After esception Code go to the : [TestCleanup] public void TestCleanup() method
3. After exception Code go to in : if (Context.CurrentTestOutcome == UnitTestOutcome.Failed)
4. As code comes to TakeScreenshot() - Another exception will occured and code exit from TestCleanup() method without completing this method.
I have debug the code and find that the issue ocured when code goto the GetScreenshot() method
This issue occured only for ChromeBrowser, with ChromeWebDriver Version : 2.27
For Firefox and IE, same code in same condition working fine and able to take screenshot in all condition
Anyone please help?