【发布时间】:2020-02-12 16:49:02
【问题描述】:
我在尝试在测试失败时截屏时遇到问题。尝试在失败情况下截屏时出现超时错误。它在 try 块中工作正常,但在 catch 块中超时。任何帮助将不胜感激。
截图方法如下:
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:
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 AvbLinqAutoFramework.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
【问题讨论】:
-
原始异常是什么?您能否将记录异常的行移到屏幕截图行上方并发布原始异常是什么?
-
@GregBurghardt 感谢您为我指明正确的方向。这就是我最初在屏幕截图上方记录异常但不认为问题可能是原始异常的方式。由于我使用的是 XPath contains,它实际上是超时的。我使用了另一个选择器并且工作正常。
-
好的。嗯。我只是希望最初的异常可以指出真正的问题。虽然如果最初的异常是超时,那么这意味着 Selenium 失去了与浏览器的连接。如果没有连接到浏览器,您将无法截取屏幕截图。您可能正在尝试解决错误的问题。
标签: c# selenium webdriver screenshot