Selenium C#ITakesScreenshot尝试在catch块中截取屏幕截图时超时
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium C#ITakesScreenshot尝试在catch块中截取屏幕截图时超时相关的知识,希望对你有一定的参考价值。
我在尝试通过测试失败时截取屏幕截图时遇到问题。尝试在故障情况下截屏时出现超时错误。它在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;
}
}
当无法找到该元素时,它将捕获块,并且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:UsersLogging.cs:line 66
at DashboardPage.get_Verifylogin() in C:UsersDasboardPage.cs:line 65
at Tests() in C:UsersSmokeTestsTests.cs:line 33
Inner Exception 1:
WebException: The operation has timed out
答案
通过使用其他选择器(CssSelector)和WebDrverWait修复了此问题。最初的例外是超时,但无法使用XPath查找元素。
public static bool FindElement
{
get
{
try
{
WebDriverWait wait = new WebDriverWait(Driver.BrowserInstance, TimeSpan.FromSeconds(10));
var inputspcmodel = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector($"div[id ='modelSearch-container'] div[class='k-widget k-multiselect k-multiselect-clearable'] div input")));
inputspcmodel.Click();
return true;
}
catch (Exception ex)
{
Logging.Error("Not able to find element " + ex.ToString());
Logging.ErrorScreenshot();
}
return false;
}
}
以上是关于Selenium C#ITakesScreenshot尝试在catch块中截取屏幕截图时超时的主要内容,如果未能解决你的问题,请参考以下文章
C#.NET Bamboo 和带 Selenium 的回归/集成测试架构
使用SelectByText(部分)与C#Selenium WebDriver绑定似乎不起作用