Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getAnchors()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getAnchors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTMLDocumentWriteTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void writeWithSplitAnchorTag() throws Exception {
    final String html = "<html><body><script>\n"
        + "document.write(\"<a href=\'start.html\");\n"
        + "document.write(\"\'>\");\n"
        + "document.write('click here</a>');\n"
        + "</script>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(html);
    final List<HtmlAnchor> anchorList = page.getAnchors();
    assertEquals(1, anchorList.size());
    final HtmlAnchor anchor = anchorList.get(0);
    assertEquals("start.html", anchor.getHrefAttribute());
    assertEquals("click here", anchor.asText());
}
 
Example 2
Source File: DoubleEquivalenceSubmissionTest.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void acceptEquivalence(int gameId)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	// http://localhost:8080/multiplayer/
	HtmlPage playPage = browser.getPage("http://localhost:8080" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	HtmlAnchor acceptEquivalenceLink = null;
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?acceptEquivalent=")) {
			acceptEquivalenceLink = a;
			break;
		}
	}

	if (!acceptEquivalenceLink.getHrefAttribute().startsWith("http://localhost:8080/")) {
		acceptEquivalenceLink.setAttribute("href",
				"http://localhost:8080/" + acceptEquivalenceLink.getHrefAttribute());
	}

	System.out.println(
			"DoubleEquivalenceSubmissionTest.HelperUser.acceptEquivalence() Accepting equivalence on game "
					+ gameId);
	acceptEquivalenceLink.click();
}
 
Example 3
Source File: DoubleEquivalenceSubmissionTest.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void claimEquivalenceOnLine(int gameId, int line)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	HtmlPage playPage = browser.getPage("http://localhost:8080" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	HtmlAnchor claimEquivalenceLink = null;
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?equivLines=" + line)) {
			claimEquivalenceLink = a;
			break;
		}
	}

	if (!claimEquivalenceLink.getHrefAttribute().startsWith("http://localhost:8080/")) {
		claimEquivalenceLink.setAttribute("href",
				"http://localhost:8080/" + claimEquivalenceLink.getHrefAttribute());
	}
	claimEquivalenceLink.click();
}
 
Example 4
Source File: DoubleEquivalenceSubmissionTest.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void joinOpenGame(int gameID, boolean isAttacker) throws FailingHttpStatusCodeException, IOException {
	HtmlPage openGames = browser.getPage("http://localhost:8080" + Paths.GAMES_OVERVIEW);

	// Really we can simply click on that link once we know the gameID,
	// no need to go to openGame page
	HtmlAnchor joinLink = null;
	for (HtmlAnchor a : openGames.getAnchors()) {
		if (a.getHrefAttribute().contains(
				Paths.BATTLEGROUND_GAME + "?" + ((isAttacker) ? "attacker" : "defender") + "=1&gameId=" + gameID)) {
			joinLink = a;
			break;
		}
	}
	if (!joinLink.getHrefAttribute().startsWith("http://localhost:8080/")) {
		joinLink.setAttribute("href", "http://localhost:8080/" + joinLink.getHrefAttribute());
	}
	HtmlPage page = joinLink.click();
}
 
Example 5
Source File: HelperUser.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the landing HtmlPage after joining the game
 *
 * @param gameID
 * @param isAttacker
 * @return
 * @throws FailingHttpStatusCodeException
 * @throws IOException
 */
public HtmlPage joinOpenGame(int gameID, boolean isAttacker) throws FailingHttpStatusCodeException, IOException {
	HtmlPage openGames = browser.getPage(codedefendersHome + Paths.GAMES_OVERVIEW);

	// Really we can simply click on that link once we know the gameID,
	// no need to go to openGame page
	HtmlAnchor joinLink = null;
	for (HtmlAnchor a : openGames.getAnchors()) {
		if (a.getHrefAttribute()
				.contains(Paths.BATTLEGROUND_GAME + "?" + ((isAttacker) ? "attacker" : "defender") + "=1&gameId=" + gameID)) {
			joinLink = a;
			break;
		}
	}
	if (!joinLink.getHrefAttribute().startsWith(codedefendersHome + "/")) {
		joinLink.setAttribute("href", codedefendersHome + "/" + joinLink.getHrefAttribute());
	}

	return joinLink.click();
}
 
Example 6
Source File: HelperUser.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void claimEquivalenceOnLine(int gameId, int line)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	HtmlPage playPage = browser.getPage(codedefendersHome + "" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	HtmlAnchor claimEquivalenceLink = null;
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?equivLines=" + line)) {
			claimEquivalenceLink = a;
			break;
		}
	}

	if (!claimEquivalenceLink.getHrefAttribute().startsWith(codedefendersHome + "/")) {
		claimEquivalenceLink.setAttribute("href",
				codedefendersHome + "/" + claimEquivalenceLink.getHrefAttribute());
	}
	claimEquivalenceLink.click();
}
 
Example 7
Source File: HelperUser.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void acceptEquivalence(int gameId)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	// codedefendersHome+"/multiplayer/
	HtmlPage playPage = browser.getPage(codedefendersHome + "" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	HtmlAnchor acceptEquivalenceLink = null;
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?acceptEquivalent=")) {
			acceptEquivalenceLink = a;
			break;
		}
	}

	if (!acceptEquivalenceLink.getHrefAttribute().startsWith(codedefendersHome + "/")) {
		acceptEquivalenceLink.setAttribute("href",
				codedefendersHome + "/" + acceptEquivalenceLink.getHrefAttribute());
	}

	System.out
			.println("DoubleEquivalenceSubmissionTest.HelperUser.acceptEquivalence() Accepting equivalence on game "
					+ gameId);
	acceptEquivalenceLink.click();
}
 
Example 8
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain a link with the specified text. The
 * specified text may be a substring of the entire text contained by the link.
 *
 * @param page the page to check
 * @param text the text which a link in the specified page is not expected to contain
 */
public static void assertLinkNotPresentWithText(final HtmlPage page, final String text) {
    boolean found = false;
    for (final HtmlAnchor a : page.getAnchors()) {
        if (a.asText().contains(text)) {
            found = true;
            break;
        }
    }
    if (found) {
        final String msg = "The page contains a link with text '" + text + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 9
Source File: DoubleEquivalenceSubmissionTest.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void assertThereIsAnEquivalenceDuel(int gameId)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	HtmlPage playPage = browser.getPage("http://localhost:8080" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?acceptEquivalent=")) {
			return;
		}
	}
	Assert.fail("On game " + gameId + " there is no equivalence duels open");

}
 
Example 10
Source File: DoubleEquivalenceSubmissionTest.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void assertNoMoreEquivalenceDuels(int gameId)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	HtmlPage playPage = browser.getPage("http://localhost:8080" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?acceptEquivalent=")) {
			Assert.fail("On game " + gameId + " there is still an equivalence duel open");
		}
	}
}
 
Example 11
Source File: HelperUser.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void assertThereIsAnEquivalenceDuel(int gameId)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	HtmlPage playPage = browser.getPage(codedefendersHome + "" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?acceptEquivalent=")) {
			return;
		}
	}
	Assert.fail("On game " + gameId + " there is no equivalence duels open");

}
 
Example 12
Source File: HelperUser.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void assertNoMoreEquivalenceDuels(int gameId)
		throws FailingHttpStatusCodeException, MalformedURLException, IOException {
	HtmlPage playPage = browser.getPage(codedefendersHome + "" + Paths.BATTLEGROUND_GAME + "?gameId=" + gameId);
	for (HtmlAnchor a : playPage.getAnchors()) {
		if (a.getHrefAttribute().contains(Paths.BATTLEGROUND_GAME + "?acceptEquivalent=")) {
			Assert.fail("On game " + gameId + " there is still an equivalence duel open");
		}
	}
}
 
Example 13
Source File: DemoApplicationTests.java    From keycloak-springsecurity5-sample with GNU General Public License v3.0 5 votes vote down vote up
private void assertLoginPage(HtmlPage page) throws Exception {
	assertThat(page.getTitleText()).isEqualTo("Login Page");

	int expectedClients = 5;

	List<HtmlAnchor> clientAnchorElements = page.getAnchors();
	assertThat(clientAnchorElements.size()).isEqualTo(expectedClients);

	ClientRegistration googleClientRegistration = this.clientRegistrationRepository.findByRegistrationId("google");
	ClientRegistration githubClientRegistration = this.clientRegistrationRepository.findByRegistrationId("github");
	ClientRegistration facebookClientRegistration = this.clientRegistrationRepository.findByRegistrationId("facebook");
	ClientRegistration oktaClientRegistration = this.clientRegistrationRepository.findByRegistrationId("okta");
       ClientRegistration keycloakClientRegistration = this.clientRegistrationRepository.findByRegistrationId("keycloak");

	String baseAuthorizeUri = AUTHORIZATION_BASE_URI + "/";
	String googleClientAuthorizeUri = baseAuthorizeUri + googleClientRegistration.getRegistrationId();
	String githubClientAuthorizeUri = baseAuthorizeUri + githubClientRegistration.getRegistrationId();
	String facebookClientAuthorizeUri = baseAuthorizeUri + facebookClientRegistration.getRegistrationId();
	String oktaClientAuthorizeUri = baseAuthorizeUri + oktaClientRegistration.getRegistrationId();
       String keycloakClientAuthorizeUri = baseAuthorizeUri + keycloakClientRegistration.getRegistrationId();

	for (int i=0; i<expectedClients; i++) {
		assertThat(clientAnchorElements.get(i).getAttribute("href")).isIn(
			googleClientAuthorizeUri, githubClientAuthorizeUri,
			facebookClientAuthorizeUri, oktaClientAuthorizeUri,
               keycloakClientAuthorizeUri);
		assertThat(clientAnchorElements.get(i).asText()).isIn(
			googleClientRegistration.getClientName(),
			githubClientRegistration.getClientName(),
			facebookClientRegistration.getClientName(),
			oktaClientRegistration.getClientName(),
               keycloakClientRegistration.getClientName());
	}
}
 
Example 14
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain a link with the specified text. The
 * specified text may be a substring of the entire text contained by the link.
 *
 * @param page the page to check
 * @param text the text which a link in the specified page is not expected to contain
 */
public static void assertLinkNotPresentWithText(final HtmlPage page, final String text) {
    boolean found = false;
    for (final HtmlAnchor a : page.getAnchors()) {
        if (a.asText().contains(text)) {
            found = true;
            break;
        }
    }
    if (found) {
        final String msg = "The page contains a link with text '" + text + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 15
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page contains a link with the specified text. The specified text
 * may be a substring of the entire text contained by the link.
 *
 * @param page the page to check
 * @param text the text which a link in the specified page is expected to contain
 */
public static void assertLinkPresentWithText(final HtmlPage page, final String text) {
    boolean found = false;
    for (final HtmlAnchor a : page.getAnchors()) {
        if (a.asText().contains(text)) {
            found = true;
            break;
        }
    }
    if (!found) {
        final String msg = "The page does not contain a link with text '" + text + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 16
Source File: ExternalTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void assertVersion(final String groupId, final String artifactId, final String version)
        throws Exception {
    String latestVersion = null;
    String url = "https://repo1.maven.org/maven2/"
                    + groupId.replace('.', '/') + '/'
                    + artifactId.replace('.', '/');
    if (!url.endsWith("/")) {
        url += "/";
    }
    try (WebClient webClient = buildWebClient()) {
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);

        final HtmlPage page = webClient.getPage(url);
        for (final HtmlAnchor anchor : page.getAnchors()) {
            String itemVersion = anchor.getTextContent();
            itemVersion = itemVersion.substring(0, itemVersion.length() - 1);
            if (!isIgnored(groupId, artifactId, itemVersion)) {
                if (isVersionAfter(itemVersion, latestVersion)) {
                    latestVersion = itemVersion;
                }
            }
        }
    }
    if (!version.endsWith("-SNAPSHOT")
            || !isVersionAfter(version.substring(0, version.length() - "-SNAPSHOT".length()), latestVersion)) {
        assertEquals(groupId + ":" + artifactId, latestVersion, version);
    }
}
 
Example 17
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page contains a link with the specified text. The specified text
 * may be a substring of the entire text contained by the link.
 *
 * @param page the page to check
 * @param text the text which a link in the specified page is expected to contain
 */
public static void assertLinkPresentWithText(final HtmlPage page, final String text) {
    boolean found = false;
    for (final HtmlAnchor a : page.getAnchors()) {
        if (a.asText().contains(text)) {
            found = true;
            break;
        }
    }
    if (!found) {
        final String msg = "The page does not contain a link with text '" + text + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 18
Source File: GSWorker.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
public IStatus work(SubMonitor mon) {
	this.monitor = mon;
	IStatus s;

	mon.setTaskName("Preparing Google Scholar Session");
	s = this.initSession();
	if (!s.isOK()) {
		out.print("@Comment { Status: " + s.getMessage() + " }\n");
		out.close();
		return s;
	}

	// set monitor to a maximum of 100 pages x 10 entries
	mon.beginTask("Loading Entries", 1000);

	int last = 10;
	int start = 0;
	while (last > 0) {
		// Update Monitor
		mon.subTask("Loading Page " + ((start / 10) + 1));

		try {
			// Load Page
			out.print("@Comment { PageLoad: Offset " + start + " } \n");
			HtmlPage Results = webClient.getPage((start > 0) ? base + "&start=" + start : base);

			// Timeouts
			s = waitLikeUser();
			if (!s.isOK()) {
				out.print("@Comment { Status: " + s.getMessage() + " }\n");
				out.close();
				return s;
			}
			s = waitForJs(Results);
			if (!s.isOK()) {
				out.print("@Comment { Status: " + s.getMessage() + " }\n");
				out.close();
				return s;
			}

			// preparing sub task monitor for entries
			SubMonitor entries = monitor.newChild(10);

			// search for bib links
			last = 0;
			for (HtmlAnchor a : Results.getAnchors()) {
				// update submonitor
				entries.subTask("Loading Page " + ((start / 10) + 1) + " - Entry " + (last + 1));

				if (a.getHrefAttribute().contains("scholar.bib")) {
					// Load entry
					TextPage Result = webClient.getPage(a.getHrefAttribute());

					// Write entry
					out.print(Result.getContent() + "\n");

					// Timeouts
					s = waitLikeUser();
					if (!s.isOK()) {
						out.print("@Comment { Status: " + s.getMessage() + " }\n");
						out.close();
						return s;
					}
					s = waitForJs(Results);
					if (!s.isOK()) {
						out.print("@Comment { Status: " + s.getMessage() + " }\n");
						out.close();
						return s;
					}

					// Increment Counter
					last++;

					// increment monitor
					entries.worked(1);
				}
			}
			entries.done();
		} catch (FailingHttpStatusCodeException | IOException e) {
			out.print("@Comment { Error: " + e.getLocalizedMessage() + " }");
			out.close();
			return new Status(Status.ERROR, "de.tudresden.slr.googlescholar", "Could not load page or entry", e);
		}

		// Increment Offset
		start += 10;
	}

	out.print("@Comment { Finished }");
	out.close();
	monitor.done();
	return Status.OK_STATUS;
}