Cómo averiguar el estado del navegador

Cuando grabe scripts de prueba funcional, si le parece que algunos controles no se han adquirido en la grabación, puede verificar si el navegador que se ha utilizado durante la grabación estaba en un estado correcto para la grabación. De un modo similar, si se han producido problemas durante la reproducción, puede verificar el estado del navegador. Puede adaptar la API dynamic find() y utilizar el método Html.HtmlBrowser con este fin.

En este ejemplo se muestra cómo adaptar la API dynamic find() y utilizar el método Html.HtmlBrowser para verificar el estado de un navegador durante la grabación o la reproducción.

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

Comentarios