Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.2k views
by Contributing Tester (48 points)
edited by

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?

1 Answer

1 like 0 dislike
by
selected by
 
Best answer
Chome usually updates automatically. So, you should update ChromeWebDriver as well.

Update your ChromeDriver to the latest version and let us know how it goes.

https://sites.google.com/a/chromium.org/chromedriver/downloads
by Contributing Tester (48 points)
I have updated  ChromeDriver to latest 2.29 verison, It worked

Thanks


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...