6

I am running a test using Selenium WebDriver and if a user doesn't have access rights a div does not exist on the page. I am trying to do a wait so that if the item is displayed it returns true but if it hits the timeout it returns false.

    public bool SummaryDisplayed()
    {
        var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
        var myElement = wait.Until(x => x.FindElement(By.Id("summaryPage")));
        return  myElement.Displayed;
    }

I don't want to use Thread.Sleep because if the element is there after 2 seconds I want it to proceed. But if the element isn't there after 5 seconds it should return false. I don't want it to throw an exception, in some test cases I am expecting it to not exist. Is there a way I can suppress the exception and return false after the timeout? Thanks

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Rochelle C
  • 928
  • 3
  • 10
  • 22
  • Sounds to me like it needs to be two different properties/methods. This is directly against what the `WebDriverWait` is about, so you could always go back down to a plain old-fashioned `while` loop. Can you give a use case for when it will be present and when it won't be? – Arran Oct 24 '13 at 18:48

3 Answers3

10

This will work for you, I think.

public bool SummaryDisplayed()
{
    try
    {
        var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
        var myElement = wait.Until(x => x.FindElement(By.Id("summaryPage")));
        return  myElement.Displayed;
    }
    catch
    {
        return false;
    }
}
Richard
  • 8,961
  • 3
  • 38
  • 47
  • This is simple, I like it. I ended up creating an extension method that uses this to see if the element exists or not. If that returns true then I check to see if it is displayed. – Rochelle C Oct 25 '13 at 11:19
5

You can use a generic extension method to wait for an element to be visible within a specified wait; and then just call it from your methods as below:

public bool SummaryDisplayed()
{
    var summaryElement = driver.WaitGetElement(By.Id("summaryPage"), 5, true);
    return (summaryElement !=null);
} 

The extension method simply wraps the WebDriverWait and waits for the element to exist/be displayed, depending on what you asked for. If element is not found within the specified wait, it returns null. I use this extension instead of FindElement() in my frameworks.

public static IWebElement WaitGetElement(this IWebDriver driver, By by, int timeoutInSeconds, bool checkIsVisible=false)
{
IWebElement element;
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));

try
{
    if (checkIsVisible)
    {
        element = wait.Until(ExpectedConditions.ElementIsVisible(by));
    }
    else
    {
        element = wait.Until(ExpectedConditions.ElementExists(by));
    }
}
catch (NoSuchElementException) { element = null; }
catch (WebDriverTimeoutException) { element = null; }
catch (TimeoutException) { element = null; }

return element;
}
Faiz
  • 3,216
  • 1
  • 19
  • 25
-1

Try using Autoreset events in system.Threading.

public static  AutoResetEvent messsageEvent= new AutoResetEvent(false);
Public bool summaryDisplay()
{
var value=SomemethodCall()
messsageEvent.Reset()//This blocks the Thread
if(value.Exists)
{
messageEvent.Set();//This releases the Thread
}
}

This is one more method messageEvent.WaitOne(millisecond, Bool value);

I think this wil help you

L202
  • 95
  • 2
  • 11
  • This looks like an overkill solution. It's like buying a girl a Bentley on the first date. Will it work? I guess, yes. Is it worth it? Obviously, not. – Arman May 18 '17 at 13:58