Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium C# DefaultWait IgnoreExceptionTypes does not work

Tags:

c#

selenium

I'm using DefaultWait while waiting for an WebElement to be clickable. Although TargetInvocationException is one of the exceptions in my list of exceptions to be ingnored during waiting, I still have tests failing with this exception before TimeOut period is reached. This is not what I expected.

public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
    {

        DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver)
        {
            Timeout = TimeSpan.FromSeconds(Configuration.WaitTime.TotalSeconds),
            PollingInterval = TimeSpan.FromMilliseconds(500)
        };
        fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
        fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));
            webelement.Click();

    }
like image 978
Frank Avatar asked Jan 05 '18 11:01

Frank


People also ask

Does Selenium support C language?

> Selenium WebDriver supports various programming languages like Java, Python, C#, Ruby, Perl, PHP, JavaScript, R, Objective-C and Haskell.

What is Selenium C# used for?

C# is being widely used as a language for Selenium Test Automation because you can easily write your test scripts with the help of Visual Studio, which offers access to features like, IntelliSense, debugging, basic refactors, etc.

What is C sharp Selenium?

C# is useful for automation testing because it allows the automation test engineer to develop an application with the help of Visual Studio on the . Net framework. C# is another programming language that also supports the binding with Selenium. And this language binding will be updated along with the java program.

Which is better Selenium with Java or C#?

When you start with Selenium I would say JAVA would be a better option for many reasons: There are a lot of opportunities for Selenium with Java is higher in the market as there are very fewer opportunities in C# with Selenium.


1 Answers

Try using WebDriverWait instead of DefaultWait<IWebDriver>, which is basically the same thing.

public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
{
    WebDriverWait fluentWait = new WebDriverWait(driver,Configuration.WaitTime.TotalSeconds);
    //No need to set PollingInterval, default is already 500ms
    fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
    fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));

    webelement.Click();
}

I see no need to use Interface when there is a predefined concrete class for exactly the same reason (waiting with the webDriver). Report back if the problem still persists.

Update: if it does not solve your problem, use lamba expression to deligate the function needed for Until() (public TResult Until<TResult>(Func<T, TResult> condition);)

        fluentWait.Until(driver =>
        {
            try
            {
                driver.FindElement(/*By Locator*/).Click();
            }
            catch (Exception ex)
            {
                Type exType = ex.GetType();
                if (exType == typeof(TargetInvocationException) ||
                    exType == typeof(NoSuchElementException) ||
                    exType == typeof(InvalidOperationException))
                {
                    return false; //By returning false, wait will still rerun the func.
                }
                else
                {
                    throw; //Rethrow exception if it's not ignore type.
                }
            }

            return true;
        });
like image 122
Thodoris Koskinopoulos Avatar answered Oct 25 '22 05:10

Thodoris Koskinopoulos