public void clickLink()

in wicket-tester/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java [1912:2054]


	public void clickLink(String path, boolean isAjax)
	{
		Component linkComponent = getComponentFromLastRenderedPage(path);

		checkUsability(linkComponent, true);

		// if the link is an AjaxLink, we process it differently
		// than a normal link
		if (linkComponent instanceof AjaxLink)
		{
			// If it's not ajax we fail
			if (isAjax == false)
			{
				fail("Link " + path + "is an AjaxLink and will "
					+ "not be invoked when AJAX (javascript) is disabled.");
			}

			List<AjaxEventBehavior> behaviors = WicketTesterHelper
				.findAjaxEventBehaviors(linkComponent, "click");
			for (AjaxEventBehavior behavior : behaviors)
			{
				executeBehavior(behavior);
			}
		}
		// if the link is an AjaxSubmitLink, we need to find the form
		// from it using reflection so we know what to submit.
		else if (linkComponent instanceof AjaxSubmitLink)
		{
			// If it's not ajax we fail
			if (isAjax == false)
			{
				fail("Link " + path + " is an AjaxSubmitLink and "
					+ "will not be invoked when AJAX (javascript) is disabled.");
			}

			AjaxSubmitLink link = (AjaxSubmitLink)linkComponent;

			String pageRelativePath = link.getInputName();
			request.getPostParameters().setParameterValue(pageRelativePath, "x");

			submitAjaxFormSubmitBehavior(link,
				(AjaxFormSubmitBehavior)WicketTesterHelper.findAjaxEventBehavior(link, "click"));
		}
		// if the link is an IAjaxLink, use it (do check if AJAX is expected)
		else if (isAjax
			&& (linkComponent instanceof IAjaxLink || linkComponent instanceof AjaxFallbackLink))
		{
			List<AjaxEventBehavior> behaviors = WicketTesterHelper
				.findAjaxEventBehaviors(linkComponent, "click");
			for (AjaxEventBehavior behavior : behaviors)
			{
				executeBehavior(behavior);
			}
		}
		/*
		 * If the link is a submitlink then we pretend to have clicked it
		 */
		else if (linkComponent instanceof SubmitLink)
		{
			SubmitLink submitLink = (SubmitLink)linkComponent;

			String pageRelativePath = submitLink.getInputName();
			request.getPostParameters().setParameterValue(pageRelativePath, "x");

			serializeFormToRequest(submitLink.getForm());
			submitForm(submitLink.getForm().getPageRelativePath());
		}
		else if (linkComponent instanceof ExternalLink)
		{
			ExternalLink externalLink = (ExternalLink)linkComponent;
			String href = externalLink.getDefaultModelObjectAsString();
			try
			{
				getResponse().sendRedirect(href);
				recordRequestResponse();
				setupNextRequestCycle();
			}
			catch (IOException iox)
			{
				throw new WicketRuntimeException("An error occurred while redirecting to: " + href,
					iox);
			}
		}
		// if the link is a normal link (or ResourceLink)
		else if (linkComponent instanceof AbstractLink)
		{
			AbstractLink link = (AbstractLink)linkComponent;

			/*
			 * If the link is a bookmarkable link, then we need to transfer the parameters to the
			 * next request.
			 */
			if (link instanceof BookmarkablePageLink)
			{
				BookmarkablePageLink<?> bookmarkablePageLink = (BookmarkablePageLink<?>)link;
				try
				{
					Method getParametersMethod = BookmarkablePageLink.class
						.getDeclaredMethod("getPageParameters", (Class<?>[])null);
					getParametersMethod.setAccessible(true);

					PageParameters parameters = (PageParameters)getParametersMethod
						.invoke(bookmarkablePageLink, (Object[])null);

					startPage(bookmarkablePageLink.getPageClass(), parameters);
				}
				catch (Exception e)
				{
					throw new WicketRuntimeException("Internal error in WicketTester. "
						+ "Please report this in Wicket's Issue Tracker.", e);
				}
			}
			else if (link instanceof ResourceLink)
			{
				try
				{
					Method getURL = ResourceLink.class.getDeclaredMethod("getURL");
					getURL.setAccessible(true);
					CharSequence url = (CharSequence)getURL.invoke(link);
					executeUrl(url.toString());
				}
				catch (Exception x)
				{
					throw new RuntimeException("An error occurred while clicking on a ResourceLink",
						x);
				}
			}
			else
			{
				executeListener(link);
			}
		}
		// The link requires AJAX
		else if (linkComponent instanceof IAjaxLink && isAjax == false)
		{
			fail("Link " + path + "is an IAjaxLink and will "
				+ "not be invoked when AJAX (javascript) is disabled.");
		}
		else
		{
			fail("Link " + path + " is not an instance of AbstractLink or IAjaxLink");
		}
	}