C# 截图

C# 截图,c#,selenium,selenium-webdriver,screenshot,C#,Selenium,Selenium Webdriver,Screenshot,我正试着截图看看考试失败的原因 [TearDown] public void TearDown() { var status = TestContext.CurrentContext.Result.Outcome.Status; var stackTrace = "<pre>" + TestContext.CurrentContext.Result.Message + "</pre>"; var er

我正试着截图看看考试失败的原因

    [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();}
我犯了个错误

结果消息:
OpenQA.Selenium.WebDriverException:对远程WebDriver服务器的URL HTTP请求在60秒后超时。 ---->System.Net.WebException:操作已超时 拆卸:OpenQA.Selenium.WebDriverException:对远程WebDriver服务器的URL HTTP请求在60秒后超时。 ---->System.Net.WebException:操作已超时

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

编辑: 我观看了
((ITakesScreenshot)Webdrievr)
,发现一个错误:

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

调用堆栈:

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

我发现了问题。由于某种原因,以下原因导致了这一切

_webdriverChrome = new ChromeDriver();
我刚刚使用了chromedriver,没有任何选择,现在它可以工作了

public class Logging
    {
      public static void ErrorScreenshot()
        {
        //Take the screenshot
        Screenshot ssh = ((ITakesScreenshot)Driver.BrowserInstance).GetScreenshot();
        //Save the screenshot
        ssh.SaveAsFile("C:/Users/", ScreenshotImageFormat.Png);
        } 
     }

我在尝试拍摄测试失败的截图时遇到了类似的问题。当我尝试在失败条件下拍摄屏幕截图时,出现超时错误。它在try块中工作正常,但在catch块中超时。我不使用Chrome选项作为上述解决方案。任何帮助都将不胜感激

以下是截图的拍摄方法:

public static bool FindElement
    {
      get
      {
          try
          {
             var element = Driver.BrowserInstance.FindElement(By.XPath("  "));
             if (element != null)
             {
               return true;
             }
          }
          catch (Exception ex)
          {
             Logging.ErrorScreenshot();
             Logging.Error("Not able to find element" + ex.ToString());
          }
          return false;
     }

  }
这是我的测试方法

Error details below:

OpenQA.Selenium.WebDriverException
  HResult=0x80131500
  Message=The HTTP request to the remote WebDriver server for URL http://localhost:55418/session/f3dbde1645dd91e453c5823d72199ea9/screenshot timed out after 60 seconds.
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.GetScreenshot()
   at Logging.ErrorScreenshot() in C:\Users\Logging.cs:line 66
   at DashboardPage.get_Verifylogin() in C:\Users\DasboardPage.cs:line 65
   at Tests() in C:\Users\SmokeTests\Tests.cs:line 33

Inner Exception 1:
WebException: The operation has timed out
当它找不到元素时,它会转到catch块,Logging.ErrorScreenshot方法会抛出超时异常


你是否检查了
ITakesScreenShot
的演员阵容?你是什么意思?我该怎么做?正如我所说,如果我只是作为一个独立的测试运行它,而不是在拆卸下运行,它就可以工作了
\u webdriverChrome.Close()
拆卸之前的任何其他位置
?在调试中,将(iTakeScreenshot)Webdrievr添加到手表中,并查看结果。但是演员阵容必须是好的,我错过了你得到的超时错误。@伙计,我在测试中退出了驱动程序,但我甚至没有达到这一点。这项测试以前失败过很多次。我正在寻找一个文本(作为一个元素),一旦驱动程序找不到它,测试就会失败并转到拆卸。这个问题表明,没有沙盒的论点实际上是
“--no sandbox”
,可能是这样的,但不能完全信服。
public static bool FindElement
    {
      get
      {
          try
          {
             var element = Driver.BrowserInstance.FindElement(By.XPath("  "));
             if (element != null)
             {
               return true;
             }
          }
          catch (Exception ex)
          {
             Logging.ErrorScreenshot();
             Logging.Error("Not able to find element" + ex.ToString());
          }
          return false;
     }

  }
Error details below:

OpenQA.Selenium.WebDriverException
  HResult=0x80131500
  Message=The HTTP request to the remote WebDriver server for URL http://localhost:55418/session/f3dbde1645dd91e453c5823d72199ea9/screenshot timed out after 60 seconds.
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.GetScreenshot()
   at Logging.ErrorScreenshot() in C:\Users\Logging.cs:line 66
   at DashboardPage.get_Verifylogin() in C:\Users\DasboardPage.cs:line 65
   at Tests() in C:\Users\SmokeTests\Tests.cs:line 33

Inner Exception 1:
WebException: The operation has timed out