Powered By Blogger

Search Here!

Showing posts with label wait for element. Show all posts
Showing posts with label wait for element. Show all posts

Tuesday, April 24, 2012

Selenium WebDriver: Wait Until Element

How to wait for an element till it comes on the page. C# code to wait for an element in a very generic manner using Selenium WebDriver.

public void WaitUntilElement(By by)
{
Boolean elementFound = false;
Stopwatch sw = new Stopwatch();
sw.Start();
int minutesToWait = Int32.Parse(ConfigurationManager.AppSettings["WaitUntillElementMinutes"]);
while (sw.Elapsed.Minutes < minutesToWait)
{
try
{
var elements = WebDriver.FindElements(by);
if (elements != null && elements.Count > 0)
{
elementFound = true;
break;
}
}
catch (Exception)
{ }
}
if (!elementFound)
{
throw new TimeoutException(string.Format("The expected element '{0}' has not found nder specified time interval of '{1}' minute(s).", by, ConfigurationManager.AppSettings["WaitUntillElementMinutes"]));
}
sw.Stop();
sw = null;
}

Selenium RC: Wait Until Element

How to wait for an element till it comes on the page. C# code to wait for an element in a very generic manner using Selenium RC.


public static void WaitUntilElement(string elementValue)       
 {
ISelenium Selenium = GetSeleniumInstance();            
Boolean ElementFound = false;            
Stopwatch sw = new Stopwatch();            
sw.Start();           
int MinutesToWait = Int32.Parse(ConfigurationManager.AppSettings["WaitUntillElementMinutes"]);            
while (sw.Elapsed.Minutes < MinutesToWait)            
{                
try                
{                    
if (Selenium.IsElementPresent(elementValue))                    
{                    
ElementFound = true;                        
break;                    
}                
}                
catch (Exception)                
{ }            
}            
if (!ElementFound)           
{
throw new TimeoutException(string.Format("The expected element '{0}' has not found under specified time interval of '{1}' minute(s).",elementValue, ConfigurationManager.AppSettings["WaitUntillElementMinutes"]));            
}
sw.Stop();
sw = null;
}