ブラウザーの状態の検出

機能テスト・スクリプトを記録しているときに、一部のコントロールが記録に含まれていないことが分かった場合、記録中に使用しているブラウザーで記録する用意ができているかを確認できます。また、再生中に問題が発生する場合にも同様に、ブラウザーの状態を確認できます。そのために、動的 find() API を導入し、Html.HtmlBrowser メソッドを使用することができます。

次のサンプルは、動的 find() API を導入し、Html.HtmlBrowser メソッドを使用して、記録または再生中にブラウザーの状態を確認する方法を示しています。

	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(delay);
		// 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;
	}
}

フィードバック