Ricerca dello stato del browser

Quando si registrano script di test funzionali, se si riscontra che alcuni controlli non sono stati rilevati dalla registrazione, è possibile verificare se il browser utilizzato durante la registrazione era in uno stato pronto per la registrazione. Analogamente, se si riscontrano problemi durante la riproduzione, è possibile verificare lo stato del browser. È possibile adattare allo scopo l'API find() dinamica ed utilizzare il metodo Html.HtmlBrowser.

Questo esempio mostra come adattare l'API find() dinamica ed utilizzare il metodo Html.HtmlBrowser per verificare lo stato di un browser durante la registrazione o la riproduzione.

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

Feedback