когда вы используете .ignoring(NoSuchElementException.class)
, вы не избегаете возникновения исключения, вы просто игнорируете это исключение. Происходит то, что ваше FluentWait вызывает исключение, но оно игнорируется (когда вы объявляете .ignoring(NoSuchElementException.class)
).
У вас есть три варианта:
- Снимите экран в конце теста, если тест не пройден [ предпочтительнее ].
- Имейте Try-Catch, где бы вы ни использовали FluentWait или любой другой код Selenium.
- Используйте отражение, чтобы избежать захвата при возникновении события из метода, который реализует FluentWait.
Это идея после того, что мы обсудили:
private void ExceptionThrown(object sender, WebDriverExceptionEventArgs e)
{
if (e.ThrownException is NoSuchElementException)
{
// Get the stack trace from the current exception
StackTrace stackTrace = new StackTrace(e.ThrownException, true);
// Get the method stack frame index.
int stackTraceIndex = stackTrace.FrameCount - 1;
// Get the method name that caused the exception
string methodName = stackTrace.GetFrame(stackTraceIndex).GetMethod().Name;
if(methodName != "MyFindElement")
{
TakeSceenshot();
}
}
else
{
TakeSceenshot();
}
}
// This is an extension method of the DriverHelper interface
public IWebElement MyFindElement(this IWebDriver driver, By by, int timeOut = 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
// I wait until the element exist
IWebElement result = wait.Until(drv => drv.FindElement(by) != null);
// it means that the element doesn't exist, so we throw the exception
if(result == null)
{
MyPersonalException(by);
}
}
// The parameter will help up to generate more accurate log
public void MyPersonalException(By by)
{
throw new NoSuchElementException(by.ToString());
}