C# Selenium 'ExpectedConditions is obsolete'

前端 未结 7 1815
灰色年华
灰色年华 2020-11-28 04:51

When trying to explicitly wait for an element to become visible using ExpectedConditions, Visual Studio warns me that it is now obsolete and will be removed from Selenium so

7条回答
  •  忘掉有多难
    2020-11-28 05:21

    The answers to change to anonymous function is the most correct one. Or write your own class of your own, needed, wait conditions. An example of using an anonymous function for the explicit scenario above would be something like...

    var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
    wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
    var element = wait.Until(() => 
    {
        var e = Driver.FindElement(By.Id("content-section"));
        if(e.Displayed)
            return e;
    });
    

    And at that point, the function itself could be off on its own in some class in your solution that you can call. The nice thing with this is that you can modify as needed; I have seen several cases where really poorly made websites end up breaking how the ExpectedConditions work, and that was solved with the team writing our own function.

    As per the C# contributor:

    With respect to ExpectedConditions, again, this was an addition that was created in .NET solely because "Java has it." At the time the ExpectedConditions class in Java was created, the syntax for creating a lambda function (or something that acted like one) was particularly arcane and difficult to understand. In that case, a helper class made lots of sense for the Java bindings. However, C# isn't Java. In C#, the syntax for creating lambda functions ("anonymous methods" in the language of Microsoft's documentation) has been well understood by C# developers for many years, and is a standard tool in their arsenal.

    In this case, the question of code verbosity does have some merit, but since wait conditions are rarely one-size-fits-all, it would be a much cleaner approach for users to develop their own conditions class that has the wait conditions they're interested in. This, however, is something users have an aversion to. Additionally, the thought of a 'standard' collection of implementations of specific wait conditions seems to be a good idea on its face, but there is a great deal of variation on the way users want any given condition to work. Having a collection of wait conditions might be a good thing, but the Selenium project is not the place for it.

    http://jimevansmusic.blogspot.com/2018/03/deprecating-parts-of-seleniums-net.html

提交回复
热议问题