我正试图截取测试失败的截图。
[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秒后超时。 ----&GT; System.Net.WebException:操作已超时 TearDown:OpenQA.Selenium.WebDriverException:对URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/screenshot的远程WebDriver服务器的HTTP请求在60秒后超时。 ----&GT; System.Net.WebException:操作已超时
只要我从capture()
调用TearDown()
方法,它就无法截取屏幕截图。
如果我只是通过在新测试中运行来调用capture()
,它就像一个魅力。
在调试模式下,我可以看到它在此行失败:Screenshot ss = ((ITakesScreenshot)Webdrievr).GetScreenshot();
我错过了什么?
编辑:
我看过((ITakesScreenshot)Webdrievr)
并收到错误:
错误CS0103:当前上下文中不存在“Webdrievr”名称
调用堆栈:
> Assign_Represnt.dll!Assign_Represnt.GetScreenShot.Capture(OpenQA.Selenium.IWebDriver Webdrievr) Line 22 C#
答案 0 :(得分:0)
我发现了问题。由于某种原因,以下引起了所有这些
var options = new ChromeOptions();
options.AddArgument("no-sandbox");
_webdriverChrome = new ChromeDriver(options);
我刚刚使用了没有选项的chromedriver,现在可以使用了。
_webdriverChrome = new ChromeDriver();
答案 1 :(得分:0)
在尝试在测试失败时进行屏幕截图时,我遇到类似的问题。当我尝试在失败情况下截图时,出现超时错误。它在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;
}
}
当找不到元素时,它将捕获块,并且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