c# - 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。
任何帮助将不胜感激
谢谢
解决方案
推荐阅读
- git - Git:当用户连接到 git 时在服务器上运行脚本
- c# - 使用 EF6 存储数据时出现“不允许从数据类型 nvarchar(max) 到 varbinary 的隐式转换”错误
- java - Selenium Java-空值的文本字段验证和日期验证
- html - 如何将背景颜色固定到标题?
- bash - 使用 bash 变量作为文件参数
- mysql - SQL 中的默认状态
- java - 如何在 JUnit 测试中强制执行模拟自动装配字段的覆盖方法?
- c - 将指向结构的指针保存到指针数组中
- node.js - 刷新页面时如何重新加载mysql表
- java - 由于未解决的依赖关系,无法创建新的 Play 项目 - SunCertPathBuilderException:无法找到有效的认证