src/nl/knokko/gui/testing/GuiTestHelper.java
/**
	 * Clicks with the given button on the text component that shows the given text
	 * that is the closest to from. The given amount must equal the number of
	 * components that show the given text.
	 * 
	 * If the number of found components that display the given text doesn't equal
	 * the given amount, a TestException will be thrown.
	 * 
	 * @param text   The text that the component to click should display
	 * @param from   The component that the component to click must be close to
	 * @param button The button to click with
	 * @param amount The number of components that show the given text
	 */
Clicks with the given button on the text component that shows the given text
that is the closest to from. The given amount must equal the number of
components that show the given text.

If the number of found components that display the given text doesn't equal
the given amount, a TestException will be thrown.

@param text   The text that the component to click should display
@param from   The component that the component to click must be close to
@param button The button to click with
@param amount The number of components that show the given text
Clicks with the given button on the text component that shows the given text that is the closest to from . The given amount must equal the number of components that show the given text . If the number of found components that display the given text doesn ' t equal the given amount a TestException will be thrown . @param text The text that the component to click should display @param from The component that the component to click must be close to @param button The button to click with @param amount The number of components that show the given text
public void clickNearest(String text, GuiComponent from, int button, int amount) {
		int previousAmount = -1;
		for (int counter = 0; counter < 256; counter++) {
			try {
				GuiComponent main = window.getMainComponent();
				if (main instanceof TextShowingComponent) {
					Collection<TextShowingComponent.Pair> all = ((TextShowingComponent) main)
							.getShowingComponents(text);
					if (all.size() != amount) {
						System.out.println("Delay a while in clickNearest");
						delay(delayTime);
						previousAmount = all.size();
						continue;
					}
					Point2D.Float middleFrom = new Point2D.Float(from.getState().getMidX(), from.getState().getMidY());
					Point2D.Float nearest = null;
					double nearestDistance = 0;
					for (TextShowingComponent.Pair pair : all) {
						if (nearest == null) {
							nearest = pair.getPosition();
							nearestDistance = nearest.distance(middleFrom);
						} else {
							double distance = pair.getPosition().distance(middleFrom);
							if (distance < nearestDistance) {
								nearestDistance = distance;
								nearest = pair.getPosition();
							}
						}
					}
					if (nearest != null) {
						click(nearest.x, nearest.y, button);
						return;
					} else {
						throw new TestException("No component with text " + text + " can be found");
					}
				} else {
					System.out.println("Delay a while in clickNearest");
					delay(delayTime);
					previousAmount = 0;
					continue;
				}
			} catch (ConcurrentModificationException concurrency) {
				System.out.println("Delay a while in catch clickNearest");
				delay(delayTime);
			}
		}
		throw new TestException("There should have been " + amount + " components with text " + text + ", but only "
				+ previousAmount + " were found");
	}
public void clickNearest ( String text , GuiComponent from , int button , int amount ) { int previousAmount = - 1 ; for ( int counter = 0 ; counter < 256 ; counter ++ ) { try { GuiComponent main = window . getMainComponent ( ) ; if ( main instanceof TextShowingComponent ) { Collection < TextShowingComponent . Pair > all = ( ( TextShowingComponent ) main ) . getShowingComponents ( text ) ; if ( all . size ( ) != amount ) { System . out . println ( "Delay a while in clickNearest" ) ; delay ( delayTime ) ; previousAmount = all . size ( ) ; continue ; } Point2D . Float middleFrom = new Point2D . Float ( from . getState ( ) . getMidX ( ) , from . getState ( ) . getMidY ( ) ) ; Point2D . Float nearest = null ; double nearestDistance = 0 ; for ( TextShowingComponent . Pair pair : all ) { if ( nearest == null ) { nearest = pair . getPosition ( ) ; nearestDistance = nearest . distance ( middleFrom ) ; } else { double distance = pair . getPosition ( ) . distance ( middleFrom ) ; if ( distance < nearestDistance ) { nearestDistance = distance ; nearest = pair . getPosition ( ) ; } } } if ( nearest != null ) { click ( nearest . x , nearest . y , button ) ; return ; } else { throw new TestException ( "No component with text " + text + " can be found" ) ; } } else { System . out . println ( "Delay a while in clickNearest" ) ; delay ( delayTime ) ; previousAmount = 0 ; continue ; } } catch ( ConcurrentModificationException concurrency ) { System . out . println ( "Delay a while in catch clickNearest" ) ; delay ( delayTime ) ; } } throw new TestException ( "There should have been " + amount + " components with text " + text + ", but only " + previousAmount + " were found" ) ; }
Clicks with the given button on the text component that shows the given text
 that is the closest to from.
Clicks with the given button on the text component that shows the given text that is the closest to from .
// The application has added components while we were iterating over them // So just wait for the application to finish