我在试着拍一张考试失败的截图。
[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 :60秒后向远程WebDriver服务器请求URL http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/element超时。-> System.Net.WebException :操作超时了,TearDown : OpenQA.Selenium.WebDriverException :对远程WebDriver服务器的http://localhost:56184/session/1aaf976356898c52e5cd57d17d44df15/screenshot请求在60秒后超时。-> 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#发布于 2017-03-21 11:54:19
我发现了问题。由于某些原因,以下原因导致了所有这些
var options = new ChromeOptions();
options.AddArgument("no-sandbox");
_webdriverChrome = new ChromeDriver(options);我只是使用色度驱动器没有选择,它现在起作用了。
_webdriverChrome = new ChromeDriver();发布于 2020-02-12 16:52:31
我有类似的问题,同时试图采取屏幕截图测试失败。当我尝试在失败的情况下拍摄屏幕截图时,会出现超时错误。它在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 outhttps://stackoverflow.com/questions/42921855
复制相似问题