截图

我正试图截取测试失败的截图 .

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

并且捕获功能是

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

我收到了一个错误

结果消息:OpenQA.Selenium.WebDriverException:远程WebDriver服务器对URL http:// localhost:56184 / session / 1aaf976356898c52e5cd57d17d44df15 / element的HTTP请求在60秒后超时 . ----> System.Net.WebException:操作已超时TearDown:OpenQA.Selenium.WebDriverException:远程WebDriver服务器对URL http:// localhost:56184 / session / 1aaf976356898c52e5cd57d17d44df15 /截屏的HTTP请求超时后60秒----> System.Net.WebException:操作已超时

问题是,只要我从 TearDown() 调用 capture() 方法,它就无法截取屏幕截图 . 如果我只是通过在新测试中运行它来调用 capture() ,它就像一个魅力 . 在调试模式下,我可以看到它在这一行失败了: Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot(); 我错过了什么?

编辑:我看过 ((ITakesScreenshot)Webdrievr) 并收到错误:

错误CS0103:当前上下文中不存在名称“Webdrievr”

调用堆栈:

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

回答(1)

2 years ago

我发现了这个问题 . 出于某种原因,以下原因造成了这一切

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

我只是使用了chromedriver没有选项,它现在工作 .

_webdriverChrome = new ChromeDriver();