Friday, June 26, 2009

Check if control is Enabled

public bool IsEnabled(string controlId)
    {
        bool retValue = false;
        try
        {
            string disabled = _selObj.GetEval("selenium.browserbot.getCurrentWindow().getElementById('" + controlId + "').disabled");
            retValue = disabled.ToLower() == "true" ? false : true;
            if (retValue)
                Log.Info(DateTime.Now.ToString() + " : Control " + controlId + " is enabled.");
            else
                Log.Info(DateTime.Now.ToString() + " : Control " + controlId + " is disabled.");
        }
        catch (Exception ex)
        {
            Log.Error(DateTime.Now.ToString() + " : Error occured while determining if control " + controlId + " is enabled");
            throw new Exception("Error occured at SeleniumPage.IsEnabled on '" + controlId + "'", ex);
        }
    return retValue;
}

Avoiding WaitForPageLoad

public bool WaitForBrowserStability(int maxWait)
{
    bool retValue = false;
    DateTime dtStart = DateTime.Now;
    do
    {
        System.Threading.Thread.Sleep(sleepTime);
        if (IsBrowserLoaded())
        {
            retValue = true;
            break;
        }
    } while (((TimeSpan)DateTime.Now.Subtract(dtStart)).TotalMilliseconds < maxWait);
    return retValue;
}
public bool IsBrowserLoaded()
{
    try
    {
        return ("true" == _selObj.GetEval("((\"complete\" == selenium.browserbot.getCurrentWindow().document.readyState) && (null == selenium.browserbot.getCurrentWindow().event))"));
    }
    catch (SeleniumException selExc)
    {
        Log.Warn(DateTime.Now.ToString() + " : Selenium error encountered. " + selExc.Message);
        _selObj.SelectWindow("");
        return false;
    }
    catch (Exception exc)
    {
        throw exc;
    }
}

Monday, June 22, 2009

Selecting Popup window without Id

public bool SelectTopWindow()
{
    try
    {
        string[] arr = GetAllWindowNames();
        if (arr.Length != 0)
        {
            System.Threading.Thread.Sleep(1000);
            _selObj.SelectWindow(arr.GetValue(arr.Length - 1).ToString());
            return true;
        }
    }
    catch (Exception exc)
    {
        return false;
    }
}