Java Code Examples for com.gargoylesoftware.htmlunit.WebClient#setCurrentWindow()

The following examples show how to use com.gargoylesoftware.htmlunit.WebClient#setCurrentWindow() . 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: HTMLElement3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"onfocus text1", "onfocus text2", "onfocus text1", "onfocus text2"})
public void onFocusOnWindowFocusGain() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();

    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String firstHtml = "<html><head><title>First</title></head>\n"
        + "<body><form name='form1'>\n"
        + "<input id='text1' onfocus='alert(\"onfocus text1\")'>\n"
        + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
        + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><head><title>Second</title></head>\n"
        + "<body onLoad='doTest()'>\n"
        + "<input id='text2' onfocus='alert(\"onfocus text2\")'>\n"
        + "<script>\n"
        + "  function doTest() {\n"
        + "    opener.document.getElementById('text1').focus();\n"
        + "    document.getElementById('text2').focus();\n"
        + "  }\n"
        + "</script></body></html>";

    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 2
Source File: HTMLElement3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"onblur text2", "onblur text1"})
public void onBlurOnWindowFocusChange() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();

    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String firstHtml = "<html><head><title>First</title></head>\n"
        + "<body><form name='form1'>\n"
        + "<input id='text1' onblur='alert(\"onblur text1\")'>\n"
        + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
        + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><head><title>Second</title></head>\n"
        + "<body onLoad='doTest()'>\n"
        + "<input id='text2' onblur='alert(\"onblur text2\")'>\n"
        + "<script>\n"
        + "  function doTest() {\n"
        + "    opener.document.getElementById('text1').focus();\n"
        + "    document.getElementById('text2').focus();\n"
        + "  }\n"
        + "</script></body></html>";

    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}