Driver is null when taking a screenshot

270 views Asked by At

In my spec-flow project, I have created static Screenshot class. I have also created static methods to take screen shots. But at run time, my driver is consistently null. How can I solve this problem.

Below is the code snippet:

public static class Screenshot
{        
    public static void TakeScreenshots(this IWebDriver driver, string path = @"result")
    {
        var takescreenshot = (driver as ITakesScreenshot) != null;
        if (!takescreenshot)
            return;
        var filename = string.Empty + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
        filename = path + @"\" + filename + ".png";
        var ss = ((ITakesScreenshot)driver).GetScreenshot();
        var screenshot = ss.AsBase64EncodedString;
        byte[] screenshotAsByteArray = ss.AsByteArray;
        ss.SaveAsFile(filename, ScreenshotImageFormat.Png);
    }
}

I call in my afterstep hook like so:

[AfterStep()]
public static void AfterStep()
{
    if (ScenarioContext.Current.TestError == null)
    {
        stepStatus = Status.Pass;
    }
    else
    {
        ScreenShot.TakeScreenshots(driver);
    }

    test.Log(stepStatus, stepLogText);
}
0

There are 0 answers