Search Here!

Tuesday, April 24, 2012

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;
}

No comments:

Post a Comment