com.gargoylesoftware.htmlunit.TopLevelWindow Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.TopLevelWindow. 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: HtmlAnchor2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Test for new openLinkInNewWindow() method.
 * @throws Exception on test failure
 */
@Test
public void openLinkInNewWindow() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n"
        + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(htmlContent);
    final HtmlAnchor anchor = page.getHtmlElementById("a1");

    assertEquals("size incorrect before test", 1, page.getWebClient().getWebWindows().size());

    final HtmlPage secondPage = (HtmlPage) anchor.openLinkInNewWindow();

    assertNotSame("new page not returned", page, secondPage);
    assertTrue("new page in wrong window type",
            TopLevelWindow.class.isInstance(secondPage.getEnclosingWindow()));
    assertEquals("new window not created", 2, page.getWebClient().getWebWindows().size());
    assertNotSame("new window not used", page.getEnclosingWindow(), secondPage
            .getEnclosingWindow());
}
 
Example #2
Source File: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Closes this window.
 */
@JsxFunction(functionName = "close")
public void close_js() {
    final WebWindow webWindow = getWebWindow();
    if (webWindow instanceof TopLevelWindow) {
        ((TopLevelWindow) webWindow).close();
    }
    else {
        webWindow.getWebClient().deregisterWebWindow(webWindow);
    }
}
 
Example #3
Source File: JavaScriptJobManagerTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Test for Bug #487 that makes sure closing a window prevents a
 * recursive setTimeout from continuing forever.
 *
 * @throws Exception if the test fails
 */
@Test
public void interruptAllWithRecursiveSetTimeout() throws Exception {
    final String content = "<html>\n"
        + "<head>\n"
        + "  <title>test</title>\n"
        + "  <script>\n"
        + "    var threadID;\n"
        + "    function test() {\n"
        + "      alert('ping');\n"
        + "      threadID = setTimeout(test, 5);\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);

    // Not perfect, but 100 chances to start should be enough for a loaded system
    Thread.sleep(500);

    assertFalse("At least one alert should have fired by now", collectedAlerts.isEmpty());
    ((TopLevelWindow) page.getEnclosingWindow()).close();

    // 100 chances to stop
    jobManager.waitForJobs(500);

    final int finalValue = collectedAlerts.size();

    // 100 chances to fail
    jobManager.waitForJobs(500);

    assertEquals("No new alerts should have happened", finalValue, collectedAlerts.size());
}
 
Example #4
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Closes this window.
 */
@JsxFunction(functionName = "close")
public void close_js() {
    final WebWindow webWindow = getWebWindow();
    if (webWindow instanceof TopLevelWindow) {
        ((TopLevelWindow) webWindow).close();
    }
    else {
        webWindow.getWebClient().deregisterWebWindow(webWindow);
    }
}
 
Example #5
Source File: UIServletTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testRender() throws Exception {
    ScheduledTime end = new ScheduledTime("2015-09-03T13:17Z");
    ScheduledTime start = new ScheduledTime("2015-09-03T13:11Z");
    NavigableSet<ScheduledTime> tileTimes = new TreeSet<>(ImmutableSet.of(new ScheduledTime("2015-09-03T13:10Z"), new ScheduledTime("2015-09-03T13:15Z")));
    WorkflowID id = new WorkflowID("foo");
    List<WorkflowGroup> groups = ImmutableList.of(new WorkflowGroup("All workflows", ImmutableList.of(id)));
    WorkflowInfo workflowInfo = new WorkflowInfo(new URL("http://example.com"), ImmutableList.of());
    SlotState state1 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:16Z")), SlotState.Status.FAILURE);
    SlotState state2 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:12Z")), SlotState.Status.WAITING);
    List<SlotState> slotStates = ImmutableList.of(state1, state2);
    Map<WorkflowID, WorkflowStatus> statuses = ImmutableMap.of(id, new WorkflowStatus(workflowInfo, slotStates, false));
    UIConfiguration conf = new UIConfiguration(start, end, tileTimes, groups, statuses, new URL("http://example.com"));
    
    StringWebResponse response = new StringWebResponse(UIServlet.render(conf), new URL("http://example.com"));
    WebClient webClient = new WebClient();
    webClient.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage page = HTMLParser.parse(response, new TopLevelWindow("top", webClient));
    
    // Some basic sanity checking
    
    List<HtmlTableDataCell> slotCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'slot')]");
    Assert.assertEquals("fail", slotCells.get(0).getTextContent());
    Assert.assertEquals("wait", slotCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> hourCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'hour')]");
    Assert.assertEquals("1315", hourCells.get(0).getTextContent());
    Assert.assertEquals("1310", hourCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> workflowCells = (List<HtmlTableDataCell>) page.getByXPath("//td[@class='workflow']");
    Assert.assertEquals("foo", workflowCells.get(0).getTextContent());
    
    System.out.println(response.getContentAsString());
}
 
Example #6
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes this window.
 * @param webWindow the web window corresponding to this window
 */
public void initialize(final WebWindow webWindow) {
    webWindow_ = webWindow;
    webWindow_.setScriptableObject(this);

    windowProxy_ = new WindowProxy(webWindow_);

    final Page enclosedPage = webWindow.getEnclosedPage();
    if (enclosedPage instanceof XmlPage) {
        document_ = new XMLDocument();
    }
    else {
        document_ = new HTMLDocument();
    }
    document_.setParentScope(this);
    document_.setPrototype(getPrototype(document_.getClass()));
    document_.setWindow(this);

    if (enclosedPage instanceof SgmlPage) {
        final SgmlPage page = (SgmlPage) enclosedPage;
        document_.setDomNode(page);

        final DomHtmlAttributeChangeListenerImpl listener = new DomHtmlAttributeChangeListenerImpl();
        page.addDomChangeListener(listener);

        if (page.isHtmlPage()) {
            ((HtmlPage) page).addHtmlAttributeChangeListener(listener);
            ((HtmlPage) page).addAutoCloseable(this);
        }
    }

    documentProxy_ = new DocumentProxy(webWindow_);

    navigator_ = new Navigator();
    navigator_.setParentScope(this);
    navigator_.setPrototype(getPrototype(navigator_.getClass()));

    screen_ = new Screen();
    screen_.setParentScope(this);
    screen_.setPrototype(getPrototype(screen_.getClass()));

    history_ = new History();
    history_.setParentScope(this);
    history_.setPrototype(getPrototype(history_.getClass()));

    location_ = new Location();
    location_.setParentScope(this);
    location_.setPrototype(getPrototype(location_.getClass()));
    location_.initialize(this);

    console_ = new Console();
    ((Console) console_).setWebWindow(webWindow_);
    console_.setParentScope(this);
    ((Console) console_).setPrototype(getPrototype(((SimpleScriptable) console_).getClass()));

    applicationCache_ = new ApplicationCache();
    applicationCache_.setParentScope(this);
    applicationCache_.setPrototype(getPrototype(applicationCache_.getClass()));

    // like a JS new Object()
    final Context ctx = Context.getCurrentContext();
    controllers_ = ctx.newObject(this);

    if (webWindow_ instanceof TopLevelWindow) {
        final WebWindow opener = ((TopLevelWindow) webWindow_).getOpener();
        if (opener != null) {
            opener_ = opener.getScriptableObject();
        }
    }
}
 
Example #7
Source File: HistoryTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void backAndForward() throws Exception {
    final WebClient client = getWebClient();
    final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
    final History history = window.getHistory();

    final String urlA = URL_FIRST + "HistoryTest_a.html";
    final String urlB = URL_FIRST + "HistoryTest_b.html";
    final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
    final String urlC = URL_FIRST + "HistoryTest_c.html";

    HtmlPage page = client.getPage(urlA);
    assertEquals(1, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());

    page = page.getAnchorByName("b").click();
    assertEquals(2, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());

    page = page.getAnchorByName("x").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlBX, page.getUrl());

    page = page.getAnchorByName("back").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());

    page = page.getAnchorByName("back").click();
    assertEquals(3, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());

    page = page.getAnchorByName("forward").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());

    page = page.getAnchorByName("c").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlC, page.getUrl());

    page = page.getAnchorByName("back").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());

    page = page.getAnchorByName("forward").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlC, page.getUrl());
}
 
Example #8
Source File: HistoryTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
public void go() throws Exception {
    final WebClient client = getWebClient();
    final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
    final History history = window.getHistory();

    final String urlA = URL_FIRST + "HistoryTest_a.html";
    final String urlB = URL_FIRST + "HistoryTest_b.html";
    final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
    final String urlC = URL_FIRST + "HistoryTest_c.html";

    HtmlPage page = client.getPage(urlA);
    assertEquals(1, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());

    page = page.getAnchorByName("b").click();
    assertEquals(2, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());

    page = page.getAnchorByName("x").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlBX, page.getUrl());

    page = page.getAnchorByName("minusTwo").click();
    assertEquals(3, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());

    page = page.getAnchorByName("plusOne").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());

    page = page.getAnchorByName("c").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlC, page.getUrl());

    page = page.getAnchorByName("minusOne").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());

    page = page.getAnchorByName("plusTwo").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
}
 
Example #9
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes this window.
 * @param webWindow the web window corresponding to this window
 */
public void initialize(final WebWindow webWindow) {
    webWindow_ = webWindow;
    webWindow_.setScriptableObject(this);

    windowProxy_ = new WindowProxy(webWindow_);

    final Page enclosedPage = webWindow.getEnclosedPage();
    if (enclosedPage instanceof XmlPage) {
        document_ = new XMLDocument();
    }
    else {
        document_ = new HTMLDocument();
    }
    document_.setParentScope(this);
    document_.setPrototype(getPrototype(document_.getClass()));
    document_.setWindow(this);

    if (enclosedPage instanceof SgmlPage) {
        final SgmlPage page = (SgmlPage) enclosedPage;
        document_.setDomNode(page);

        final DomHtmlAttributeChangeListenerImpl listener = new DomHtmlAttributeChangeListenerImpl();
        page.addDomChangeListener(listener);

        if (page.isHtmlPage()) {
            ((HtmlPage) page).addHtmlAttributeChangeListener(listener);
            ((HtmlPage) page).addAutoCloseable(this);
        }
    }

    documentProxy_ = new DocumentProxy(webWindow_);

    navigator_ = new Navigator();
    navigator_.setParentScope(this);
    navigator_.setPrototype(getPrototype(navigator_.getClass()));

    screen_ = new Screen();
    screen_.setParentScope(this);
    screen_.setPrototype(getPrototype(screen_.getClass()));

    history_ = new History();
    history_.setParentScope(this);
    history_.setPrototype(getPrototype(history_.getClass()));

    location_ = new Location();
    location_.setParentScope(this);
    location_.setPrototype(getPrototype(location_.getClass()));
    location_.initialize(this);

    console_ = new Console();
    ((Console) console_).setWebWindow(webWindow_);
    console_.setParentScope(this);
    ((Console) console_).setPrototype(getPrototype(((SimpleScriptable) console_).getClass()));

    applicationCache_ = new ApplicationCache();
    applicationCache_.setParentScope(this);
    applicationCache_.setPrototype(getPrototype(applicationCache_.getClass()));

    // like a JS new Object()
    final Context ctx = Context.getCurrentContext();
    controllers_ = ctx.newObject(this);

    if (webWindow_ instanceof TopLevelWindow) {
        final WebWindow opener = ((TopLevelWindow) webWindow_).getOpener();
        if (opener != null) {
            opener_ = opener.getScriptableObject();
        }
    }
}