com.gargoylesoftware.htmlunit.TextPage Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.TextPage. 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: ServletBasicIT.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testGetWithCorrectCredentials() throws Exception {
    DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
    credentialsProvider.addCredentials("test", "pass");
    
    webClient.setCredentialsProvider(credentialsProvider);
    TextPage page = webClient.getPage(base + "/protected/servlet");
    
    assertTrue(page.getContent().contains("This is a protected servlet"));
}
 
Example #2
Source File: SimpleServletServerTest.java    From wildfly-samples with MIT License 5 votes vote down vote up
@Test
public void testMyServlet() throws IOException, ServletException {
    SimpleServletServer server = new SimpleServletServer();
    server.start();
    WebClient webClient = new WebClient();
    TextPage page = webClient.getPage(BASE + "/MyServlet");
    assertEquals("Hello World from GET", page.getContent());
    
    WebRequest request = new WebRequest(new URL(BASE + "/MyServlet"), HttpMethod.POST);
    page = webClient.getPage(request);
    assertEquals("Hello World from POST", page.getContent());
    server.stop();
}
 
Example #3
Source File: SimpleServletServerTest.java    From wildfly-samples with MIT License 5 votes vote down vote up
@Test
public void testMyAnotherServlet() throws IOException, ServletException {
    SimpleServletServer server = new SimpleServletServer();
    server.start();
    WebClient webClient = new WebClient();
    TextPage page = webClient.getPage(BASE + "/MyAnotherServlet");
    assertEquals("Howdy World from GET", page.getContent());
    
    WebRequest request = new WebRequest(new URL(BASE + "/MyAnotherServlet"), HttpMethod.POST);
    page = webClient.getPage(request);
    assertEquals("Howdy World from POST", page.getContent());
    server.stop();
}
 
Example #4
Source File: HelloWorldServerTest.java    From wildfly-samples with MIT License 5 votes vote down vote up
@Test
public void testApp() throws IOException {
    HelloWorldServer server = new HelloWorldServer();
    server.start();
    WebClient webClient = new WebClient();
    TextPage page = webClient.getPage("http://localhost:8080");
    assertEquals("Hello World", page.getContent());
    server.stop();
}
 
Example #5
Source File: RequestLocaleSupportTest.java    From trimou with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequestLocaleSupport()
        throws FailingHttpStatusCodeException, MalformedURLException,
        IOException {
    WebClient webClient = new WebClient();
    WebRequest request = new WebRequest(new URL(contextPath, "test"));
    request.setAdditionalHeader("Accept-Language", "cs");
    TextPage page = webClient.getPage(request);
    assertEquals("cs:::fr", page.getContent());
}
 
Example #6
Source File: ServletTest.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
private String executeSimpleRest(String command) throws IOException {
    final String startUrl = base.toExternalForm() + "jbatch/rest/" + command;
    final WebClient webClient = newWebClient();
    final TextPage page = webClient.getPage(startUrl);
    String textContent = page.getContent();
    assertNotNull(textContent);
    assertTrue(textContent.contains("\nOK\n"));
    extractExecutionId(textContent);

    return textContent;
}
 
Example #7
Source File: AttachmentTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Tests {@link Attachment#getSuggestedFilename()}.
 * @throws Exception if an error occurs
 */
@Test
public void filename() throws Exception {
    final String content = "<html>But is it really?</html>";

    final WebClient client = getWebClient();
    final MockWebConnection conn = new MockWebConnection();
    client.setWebConnection(conn);
    final List<Attachment> attachments = new ArrayList<>();
    client.setAttachmentHandler(new CollectingAttachmentHandler(attachments));

    final List<NameValuePair> headers1 = new ArrayList<>();
    headers1.add(new NameValuePair("Content-Disposition", "attachment;filename=\"hello.html\""));
    conn.setResponse(URL_FIRST, content, 200, "OK", MimeType.TEXT_HTML, headers1);
    client.getPage(URL_FIRST);
    final Attachment result = attachments.get(0);
    assertEquals(result.getSuggestedFilename(), "hello.html");
    attachments.clear();

    final List<NameValuePair> headers2 = new ArrayList<>();
    headers2.add(new NameValuePair("Content-Disposition", "attachment; filename=hello2.html; something=else"));
    conn.setResponse(URL_SECOND, content, 200, "OK", MimeType.TEXT_PLAIN, headers2);
    client.getPage(URL_SECOND);
    final Attachment result2 = attachments.get(0);
    assertEquals(result2.getSuggestedFilename(), "hello2.html");
    assertEquals(content, ((TextPage) result2.getPage()).getContent());
    attachments.clear();

    final List<NameValuePair> headers3 = new ArrayList<>();
    headers3.add(new NameValuePair("Content-Disposition", "attachment; filename="));
    conn.setResponse(URL_SECOND, content, 200, "OK", MimeType.TEXT_PLAIN, headers3);
    client.getPage(URL_SECOND);
    final Attachment result3 = attachments.get(0);
    assertNull(result3.getSuggestedFilename());
    assertEquals(content, ((TextPage) result3.getPage()).getContent());
    attachments.clear();

    final List<NameValuePair> headers4 = new ArrayList<>();
    headers4.add(new NameValuePair("Content-Disposition", "attachment"));
    final byte[] contentb = new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
    conn.setResponse(URL_THIRD, contentb, 200, "OK", "application/x-rubbish", headers4);
    client.getPage(URL_THIRD);
    final Attachment result4 = attachments.get(0);
    final InputStream result4Stream = result4.getPage().getWebResponse().getContentAsStream();
    assertNull(result4.getSuggestedFilename());
    assertEquals(result4.getPage().getWebResponse().getContentType(), "application/x-rubbish");
    HttpWebConnectionTest.assertEquals(new ByteArrayInputStream(contentb), result4Stream);
    attachments.clear();
}
 
Example #8
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;
}
 
Example #9
Source File: JmxWebHandlerTest.java    From simplejmx with ISC License 4 votes vote down vote up
@Test(timeout = 10000)
public void testSimple() throws Exception {
	WebClient webClient = new WebClient();
	HtmlPage page = webClient.getPage("http://" + WEB_SERVER_NAME + ":" + WEB_SERVER_PORT);
	assertTrue(page.asText().contains("JMX Domains"));

	String domain = "java.lang";
	HtmlAnchor anchor = page.getAnchorByText(domain);
	assertNotNull(anchor);
	page = anchor.click();
	assertTrue(page.asText().contains("Beans in domain " + domain));

	anchor = page.getAnchorByName("text");
	TextPage textPage = anchor.click();
	String bean = "type=Memory";
	assertTrue(textPage.getContent().contains(domain + ":" + bean));

	anchor = page.getAnchorByText(bean);
	page = anchor.click();
	assertTrue(page.asText().contains("Information about object " + domain + ":" + bean));

	anchor = page.getAnchorByName("text");
	textPage = anchor.click();
	assertTrue(textPage.getContent().contains("Verbose"));

	HtmlForm form = page.getFormByName("Verbose");
	assertNotNull(form);
	HtmlInput input = form.getInputByName("val");
	assertEquals("false", input.getValueAttribute());
	assertNotNull(input);
	input.setValueAttribute("true");
	HtmlElement button = (HtmlElement) page.createElement("button");
	button.setAttribute("type", "submit");
	// append the button to the form to simulate
	form.appendChild(button);
	// submit the form
	page = button.click();
	assertTrue(page.asText().contains("Information about object " + domain + ":" + bean));

	form = page.getFormByName("Verbose");
	assertNotNull(form);
	input = form.getInputByName("val");
	assertEquals("true", input.getValueAttribute());

	String operation = "gc";
	form = page.getFormByName(operation);
	assertNotNull(form);
	input = form.getInputByValue(operation);
	page = input.click();

	assertTrue(page.asText().contains("Invoking Operation " + operation));
	assertTrue(page.asText().contains(operation + " method successfully invoked."));

	anchor = page.getAnchorByName("text");
	assertNotNull(anchor);
	textPage = anchor.click();
	assertEquals(operation + " method successfully invoked.\n", textPage.getContent());
}