Search code examples
c#seleniumselenium-webdriverscreenshot

Take screenshot


I'm trying to take a screenshot on test failure.

    [TearDown]
    public void TearDown()
    {
        var status = TestContext.CurrentContext.Result.Outcome.Status;
        var stackTrace = "<pre>" + TestContext.CurrentContext.Result.Message + "</pre>";
        var errorMessage = TestContext.CurrentContext.Result.Message;
        if (status == NUnit.Framework.Interfaces.TestStatus.Failed)
        {
            test.Log(LogStatus.Fail, status + errorMessage);
            var ScreenShotPath = GetScreenShot.Capture(_webdriverChrome);
            test.Log(LogStatus.Fail, "Screen Shot Below: "+test.AddScreenCapture(ScreenShotPath));
        }
        else if (status == NUnit.Framework.Interfaces.TestStatus.Passed)
        {
            test.Log(LogStatus.Pass, status + errorMessage);
        }
        extent.EndTest(test);
        _webdriverChrome.Quit();}

and the capture function is

 public static string Capture(IWebDriver Webdrievr)
    {
        string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
        string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
        string projectPath = new Uri(actualPath).LocalPath;

        Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot();
        string screenshot = ss.AsBase64EncodedString;
        byte[] screenshotAsByteArray = ss.AsByteArray;
        ss.SaveAsFile(projectPath + "ErrorReportScreenshot\\ErrorScreenshot.jpeg", ScreenshotImageFormat.Jpeg); //use any of the built in image formating
        string _fullPathToReturn = projectPath + "ErrorReportScreenshot";
        return _fullPathToReturn;
    }

I'm getting an error

Result Message:
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/element timed out after 60 seconds. ----> System.Net.WebException : The operation has timed out TearDown : OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/screenshot timed out after 60 seconds. ----> System.Net.WebException : The operation has timed out

The thing is that it fails taking the screenshots as long as I'm calling the capture() method from TearDown(). If I'm just calling the capture() by running it within a new test, it works like a charm. In debugging mode, I can see it fails on this row: Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot(); what am I missing?

EDIT: I have watched the ((ITakesScreenshot)Webdrievr) and getting an error:

error CS0103: The name 'Webdrievr' does not exist in the current context

Call Stack:

>   Assign_Represnt.dll!Assign_Represnt.GetScreenShot.Capture(OpenQA.Selenium.IWebDriver Webdrievr) Line 22 C#

Solution

  • I found the problem . for some reason the following caused all of this

    var options = new ChromeOptions();
    options.AddArgument("no-sandbox");
    _webdriverChrome = new ChromeDriver(options);
    

    I just used the chromedriver without options and it works now.

    _webdriverChrome = new ChromeDriver();