Finding the state of the browser

When you record functional test scripts, if you find that some controls were not picked up by the recording, you can verify whether the browser used during the recording was in a ready state for recording. Similarly, if you encountered problems during playback, you can verify the state of the browser. You can use the dynamic find() API and use the Html.HtmlBrowser method for this purpose.

This example shows you how to use the dynamic find() API and use the Html.HtmlBrowser method to verify the state of a browser during recording or playback.
Note: This example assumes a single instance of the browser. You can use this example iteratively when multiple instances of the browser are running.
	public void testMain(Object[] args) 
	{
		//This sample verifies whether the Browser is in ready state or not.
		// To run this script, start a single instance of the browser, Internet Explorer or Mozilla Firefox.

		startBrowser("http://www.google.com");
		sleep(5);
		// Checking Browser class and when it is found, returns to Test Object
		TestObject[]  to = find(atChild(".class", "Html.HtmlBrowser"));
		// Found one or more Test Object
		if(to.length> 0)
		{
			// Cast into BrowserTestObject
			BrowserTestObject bto = (BrowserTestObject)to[0];

			//Wait for the browser to be ready 
			// parameter, browsser test object, state of the browser, timeout& delay in seconds
			boolean isBrowserReady = waitForBrowserTobeReady(bto, 4, 240, 10);
			if(isBrowserReady)
			{

				// Performing a find operation and saving the returned object in the TestObject array.
				TestObject[] googleButton =  bto.find(atDescendant(".class" ,"Html.INPUT.submit",".value","I'm Feeling Lucky"));

				if(googleButton.length ==0 )
				{
					System.out.println("None found");
					return;
				}
				//Click the first test object found.
				((GuiTestObject)googleButton[0]).click();

			}
			else
			{
				System.out.println("Browser didn't come to ready State");
			}
			unregisterAll();


		}
		else
		{
			System.out.println("No browser instance found");
		}
	}

	/*
	 * 
	 * waitForBrowserTobeReady
	 * param : 
	 * This method waits for the browser to come to the readyState within a specified time range
	 * BrowserTestObject as bto
	 * readyState as 4
	 * timeout as 120 seconds
	 * delay as 10 seconds
	 */

	static boolean waitForBrowserTobeReady(BrowserTestObject bto, int readyState, int timeout, int delay)
	{
		//Check is browser is ready
		boolean isBrowserReady = false;

		// Number of tries with a delay
		int noOfTries = timeout/delay;

		for(int i=0; i < noOfTries; i++)
		{
			try
			{

				//Possible .readyState property values for the browser
				// 0 - Uninitalized
				//1,2 - LOADING
				//3 - LOADED
				//4 - ENABLE/VISIBLE/READY
				int browserState = ((Integer)(bto.getProperty(".readyState"))).intValue();
				if(browserState>= readyState)
				{
					isBrowserReady = true;
					break;
				}
			}
			//Catch exception if any
			catch(Exception e)
			{
				break;
			}
			sleep(delay);
		} 
		//Return successful of browser ready state is true
		return isBrowserReady;
	}
}