繁体   English   中英

截屏

[英]Take screenshot

我正在尝试对测试失败进行截图。

    [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 :对 URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/element的远程 WebDriver 服务器的 HTTP 请求在 60 秒后超时。 ----> System.Net.WebException : The operation has timed out TearDown : OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/screenshot timed out after 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#

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

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

我只使用了没有选项的 chromedriver,现在可以使用了。

_webdriverChrome = new ChromeDriver();

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

下面是截图的方法:

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

这是我的测试方法

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

  }

当它无法找到元素时,它会转到 catch 块,并且 Logging.ErrorScreenshot 方法会引发超时异常。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM