com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager. 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: WebClient.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the aggregate background JavaScript job count across all windows.
 * @return the aggregate background JavaScript job count across all windows
 */
private int getAggregateJobCount() {
    int count = 0;
    for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext();) {
        final JavaScriptJobManager jobManager;
        final WeakReference<JavaScriptJobManager> reference;
        try {
            reference = i.next();
            jobManager = reference.get();
            if (jobManager == null) {
                i.remove();
                continue;
            }
        }
        catch (final ConcurrentModificationException e) {
            i = jobManagers_.iterator();
            count = 0;
            continue;
        }
        final int jobCount = jobManager.getJobCount();
        count += jobCount;
    }
    return count;
}
 
Example #2
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the aggregate background JavaScript job count across all windows.
 * @return the aggregate background JavaScript job count across all windows
 */
private int getAggregateJobCount() {
    int count = 0;
    for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext();) {
        final JavaScriptJobManager jobManager;
        final WeakReference<JavaScriptJobManager> reference;
        try {
            reference = i.next();
            jobManager = reference.get();
            if (jobManager == null) {
                i.remove();
                continue;
            }
        }
        catch (final ConcurrentModificationException e) {
            i = jobManagers_.iterator();
            count = 0;
            continue;
        }
        final int jobCount = jobManager.getJobCount();
        count += jobCount;
    }
    return count;
}
 
Example #3
Source File: GSWorker.java    From slr-toolkit with Eclipse Public License 1.0 6 votes vote down vote up
protected IStatus waitForJs(HtmlPage page) {
	JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
	while (manager.getJobCount() > 0) {
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			return new Status(Status.ERROR, "de.tudresden.slr.googlescholar",
					"Failed to wait for Javascript processes", e);
		}

		if (this.monitor != null && monitor.isCanceled()) {
			return Status.CANCEL_STATUS;
		}
	}
	return Status.OK_STATUS;
}
 
Example #4
Source File: WebClient.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * When we deserialize, re-initializie transient fields.
 * @param in the object input stream
 * @throws IOException if an error occurs
 * @throws ClassNotFoundException if an error occurs
 */
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();

    webConnection_ = new HttpWebConnection(this);
    scriptEngine_ = new JavaScriptEngine(this);
    jobManagers_ = Collections.synchronizedList(new ArrayList<WeakReference<JavaScriptJobManager>>());
    loadQueue_ = new ArrayList<>();

    if (getBrowserVersion().hasFeature(JS_XML_SUPPORT_VIA_ACTIVEXOBJECT)) {
        initMSXMLActiveX();
    }
}
 
Example #5
Source File: WebClientWaitForBackgroundJobsTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void dontWaitWhenUnnecessary() throws Exception {
    final String content = "<html>\n"
        + "<head>\n"
        + "  <title>test</title>\n"
        + "  <script>\n"
        + "    var threadID;\n"
        + "    function test() {\n"
        + "      threadID = setTimeout(doAlert, 10000);\n"
        + "    }\n"
        + "    function doAlert() {\n"
        + "      alert('blah');\n"
        + "    }\n"
        + "  </script>\n"
        + "</head>\n"
        + "<body onload='test()'>\n"
        + "</body>\n"
        + "</html>";

    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    final HtmlPage page = loadPage(content, collectedAlerts);
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    assertNotNull(jobManager);
    assertEquals(1, jobManager.getJobCount());

    startTimedTest();
    assertEquals(1, page.getWebClient().waitForBackgroundJavaScriptStartingBefore(7000));
    assertMaxTestRunTime(100);
    assertEquals(1, jobManager.getJobCount());
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
 
Example #6
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * When we deserialize, re-initializie transient fields.
 * @param in the object input stream
 * @throws IOException if an error occurs
 * @throws ClassNotFoundException if an error occurs
 */
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();

    webConnection_ = createWebConnection();
    scriptEngine_ = new JavaScriptEngine(this);
    jobManagers_ = Collections.synchronizedList(new ArrayList<WeakReference<JavaScriptJobManager>>());
    loadQueue_ = new ArrayList<>();

    if (getBrowserVersion().hasFeature(JS_XML_SUPPORT_VIA_ACTIVEXOBJECT)) {
        initMSXMLActiveX();
    }
}
 
Example #7
Source File: WebWindowImpl.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public JavaScriptJobManager getJobManager() {
    return jobManager_;
}
 
Example #8
Source File: HtmlInlineFrameTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that frames added via element.innerHtml() are resolved.
 * @throws Exception if an error occurs
 */
@Test
public void frameSetInnerHtmlDoesLoadFrameContentTimeout() throws Exception {
    final String html1 =
          "<html><body>\n"
        + "<iframe id='myFrame' src='" + URL_THIRD + "'></iframe>';\n"
        + "<span id='A'></span>\n"
        + "<script>\n"
        + "  function createIframe() {\n"
        + "    var frame='<iframe id=\"f\" src=\"" + URL_SECOND + "\"></iframe>';\n"
        + "    document.getElementById('A').innerHTML = frame;\n"
        + "  }\n"
        + "  setTimeout('createIframe()', 100);\n"
        + "</script>\n"
        + "</body></html>";
    final String html2 = "<html><body>iframe content</body></html>";
    final String html3 = "<html><head></head><body>Third content</body></html>";

    final WebClient client = getWebClientWithMockWebConnection();

    final MockWebConnection conn = getMockWebConnection();
    conn.setResponse(URL_FIRST, html1);
    conn.setResponse(URL_SECOND, html2);
    conn.setResponse(URL_THIRD, html3);

    final HtmlPage page = client.getPage(URL_FIRST);

    final HtmlElement myFrame = page.getHtmlElementById("myFrame");
    assertEquals("iframe", myFrame.getTagName());

    HtmlPage enclosedPage = (HtmlPage) ((HtmlInlineFrame) myFrame).getEnclosedPage();
    assertEquals("Third content", enclosedPage.asText());

    // wait for the timer
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    jobManager.waitForJobs(1000);

    final HtmlElement iFrame = page.getHtmlElementById("f");
    assertEquals("iframe", iFrame.getTagName());

    enclosedPage = (HtmlPage) ((HtmlInlineFrame) iFrame).getEnclosedPage();
    assertEquals("iframe content", enclosedPage.asText());

    assertEquals(3, conn.getRequestCount());
}
 
Example #9
Source File: WebWindowImpl.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public JavaScriptJobManager getJobManager() {
    return jobManager_;
}
 
Example #10
Source File: WebWindow.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Returns the job manager for this window.
 *
 * @return the job manager for this window
 */
JavaScriptJobManager getJobManager();
 
Example #11
Source File: WebWindowImpl.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * <p><span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span></p>
 *
 * <p>Sets the JavaScript job manager for this window.</p>
 *
 * @param jobManager the JavaScript job manager to use
 */
public void setJobManager(final JavaScriptJobManager jobManager) {
    jobManager_ = jobManager;
}
 
Example #12
Source File: WebWindow.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Returns the job manager for this window.
 *
 * @return the job manager for this window
 */
JavaScriptJobManager getJobManager();
 
Example #13
Source File: WebWindowImpl.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * <p><span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span></p>
 *
 * <p>Sets the JavaScript job manager for this window.</p>
 *
 * @param jobManager the JavaScript job manager to use
 */
public void setJobManager(final JavaScriptJobManager jobManager) {
    jobManager_ = jobManager;
}