0

I'm currently having issues with taking a screenshot - I want to attach this to an extent report for my automation tests.

I've created the following method for taking the screenshot:

 public static string TakeScreenshot()
    {
        string path1 = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
        string path = path1 + "Screenshot\\" + "screenshot" + ".png";
        Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
        screenshot.SaveAsFile(path, ScreenshotImageFormat.Png);
        return path;
    }

I'm getting a System.NullReferenceException (System.NullReferenceException: 'Object reference not set to an instance of an object.') on this line of code: Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();

I've tried different methods for taking the screenshot but I've had no luck:

 public static string Capture()
    {
        ITakesScreenshot ts = Driver as ITakesScreenshot;
        Screenshot screenshot = ts.GetScreenshot();
        string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
        string finalpth = pth.Substring(0, pth.LastIndexOf("bin")) + "screenShotName" + ".png";
        string localpath = new Uri(finalpth).LocalPath;
        screenshot.SaveAsFile(localpath, ScreenshotImageFormat.Png);
        return localpath;
    }

This is how I'm trying to attach the screenshot to my report:

 else if (_scenarioContext.TestError != null)
        {
            if (stepType == "Given")
            {
                _currentScenarioName.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                string path = AllPages.TakeScreenshot();
                _currentScenarioName.AddScreenCaptureFromPath(path);
            }
            else if (stepType == "When")
            {
                _currentScenarioName.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                string path = AllPages.TakeScreenshot();
                _currentScenarioName.AddScreenCaptureFromPath(path);
            }

Message: Object reference not set to an instance of an object.

Stack Trace: System.NullReferenceException: Object reference not set to an instance of an object. AllPages.TakeScreenshot() line 24 Hooks.AfterEachStep() line 60 TestExecutionEngine.FireEvents(HookType hookType) line 404 TestExecutionEngine.FireScenarioEvents(HookType bindingEvent) line 365 RunnerTestExecutionEngine.FireScenarioEvents(HookType bindingEvent) TestExecutionEngine.OnStepEnd() line 345 RunnerTestExecutionEngine.OnStepEnd() TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance) line 572 TestExecutionEngine.Step(StepDefinitionKeyword stepDefinitionKeyword, String keyword, String text, String multilineTextArg, Table tableArg) line 686 RunnerTestExecutionEngine.Step(StepDefinitionKeyword stepDefinitionKeyword, String keyword, String text, String multilineTextArg, Table tableArg) TestRunner.And(String text, String multilineTextArg, Table tableArg, String keyword) line 95 DataSetUpFeature.CompleteE_Sign() line 108 StaticOrInstanceMethodExecutor.ExecuteInternal(ITestThreadExecutionContext testThreadExecutionContext) StaticOrInstanceMethodExecutor.Execute(ITestThreadExecutionContext testThreadExecutionContext) TestNodeTask.Execute()

Any help would be greatly appreciated

Thanks

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49

0 Answers0