简体   繁体   English

截屏

[英]Take screenshot

I'm trying to take a screenshot on test failure.我正在尝试对测试失败进行截图。

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

and the capture function is和捕获功能是

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

I'm getting an error我收到一个错误

Result Message:结果信息:
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/element timed out after 60 seconds. 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 seconds. ----> 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 : The operation has timed out ----> System.Net.WebException : 操作超时

The thing is that it fails taking the screenshots as long as I'm calling the capture() method from TearDown() .问题是,只要我从TearDown()调用capture()方法,它就无法截取屏幕截图。 If I'm just calling the capture() by running it within a new test, it works like a charm.如果我只是通过在新测试中运行它来调用capture() ,它就像一个魅力。 In debugging mode, I can see it fails on this row: Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot();在调试模式下,我可以看到它在这一行失败: Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot(); what am I missing?我错过了什么?

EDIT: I have watched the ((ITakesScreenshot)Webdrievr) and getting an error:编辑:我看过((ITakesScreenshot)Webdrievr)并收到错误:

error CS0103: The name 'Webdrievr' does not exist in the current context错误 CS0103:当前上下文中不存在名称“Webdrievr”

Call Stack:调用堆栈:

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

I found the problem .我发现了问题。 for some reason the following caused all of this出于某种原因,以下原因导致了这一切

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

I just used the chromedriver without options and it works now.我只使用了没有选项的 chromedriver,现在可以使用了。

_webdriverChrome = new ChromeDriver();

I am having similar issue while trying to take screenshot upon test failure.我在尝试在测试失败时截取屏幕截图时遇到了类似的问题。 I get timed out error when I try to take the screenshot on failure condition.当我尝试在失败情况下截取屏幕截图时出现超时错误。 It works fine in try block but timed out in catch block.它在 try 块中工作正常,但在 catch 块中超时。 I am not using Chrome options as the solution provided above.我没有使用 Chrome 选项作为上面提供的解决方案。 Any help would be appreciated.任何帮助,将不胜感激。

Below is the method to take screenshot:下面是截图的方法:

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

This is my test method这是我的测试方法

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

  }

when it is not able to find the element it goes to catch block and there Logging.ErrorScreenshot method throws a timed out exception.当它无法找到元素时,它会转到 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