vendredi 12 avril 2019

C# Set type at ExpectedConditions return type at runtime

I'm trying to make a method where you can set any type of implicit wait. However, I do not know how to set the type of cast at runtime. I get the error 'waitCriteriaReturnType is a variable but is used like a type'. Here is my method:

    public void SetImplicitWait(int milliseconds, string waitCriteriaMethodName, object[] waitCriteriaMethodParameters)
    {
        WebDriverWait wait = new WebDriverWait(Browser.Driver, System.TimeSpan.FromMilliseconds(milliseconds));
        MethodInfo waitCriteriaMethod = typeof(ExpectedConditions).GetMethod(waitCriteriaMethodName);
        Type waitCriteriaMethodReturnType = waitCriteriaMethod.ReturnType;

        try
        {                
            waitCriteriaMethodReturnType output = (waitCriteriaMethodReturnType) waitCriteriaMethod.Invoke(null, waitCriteriaMethodParameters);  // error
            wait.Until(output);  // error
        }
        catch (OpenQA.Selenium.WebDriverTimeoutException)
        {
            ScreenReporter.LogDebug("Unable to find the element within " + milliseconds + " ms. Ignoring timeout...");
        }
    }

I want to call this method like:

SetImplicitWait(2000, "TextToBePresentInElement", new object[] { GetWebElement("a"), "TextToLookFor" });





Aucun commentaire:

Enregistrer un commentaire