首页 > 解决方案 > NullReferenceException 截图 ExtentReport C#

问题描述

我目前在截屏时遇到问题 - 我想将此附加到我的自动化测试的范围报告中。

我创建了以下截屏方法:

 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;
    }

我在这行代码中得到了 System.NullReferenceException (System.NullReferenceException: 'Object reference not set to an instance of an object.'):Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();

我尝试了不同的方法来截取屏幕截图,但我没有运气:

 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;
    }

这就是我尝试将屏幕截图附加到我的报告的方式:

 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);
            }

消息:对象引用未设置为对象的实例。

堆栈跟踪:System.NullReferenceException:对象引用未设置为对象的实例。AllPages.TakeScreenshot() 第 24 行 Hooks.AfterEachStep() 第 60 行 TestExecutionEngine.FireEvents(HookType hookType) 第 404 行 TestExecutionEngine.FireScenarioEvents(HookType bindingEvent) 第 365 行 RunnerTestExecutionEngine.FireScenarioEvents(HookType bindingEvent) TestExecutionEngine.OnStepEnd() 第 345 行 RunnerTestExecutionEngine.OnStepEnd( ) TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance) 第 572 行 TestExecutionEngine.Step(StepDefinitionKeyword stepDefinitionKeyword, String keyword, String text, String multilineTextArg, Table tableArg) 第 686 行 RunnerTestExecutionEngine.Step(StepDefinitionKeyword stepDefinitionKeyword, String keyword, String text, String multilineTextArg,表 tableArg) TestRunner。

任何帮助将不胜感激

谢谢

标签: c#selenium-webdriverscreenshotspecflowextentreports

解决方案


推荐阅读