use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_onSubmitHandler_returnFalse.
/**
* @throws Exception if the test fails
*/
@Test
public void submit_onSubmitHandler_returnFalse() throws Exception {
final String firstHtml = "<html><head><title>First</title></head><body>\n" + "<form method='get' action='" + URL_SECOND + "' " + "onSubmit='alert(\"clicked\");return false;'>\n" + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n" + "</body></html>";
final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, firstHtml);
webConnection.setResponse(URL_SECOND, secondHtml);
final HtmlPage firstPage = client.getPage(URL_FIRST);
final HtmlSubmitInput button = firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = button.click();
assertEquals(firstPage.getTitleText(), secondPage.getTitleText());
assertEquals(new String[] { "clicked" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_onSubmitHandler_javascriptDisabled.
/**
* @throws Exception if the test fails
*/
@Test
public void submit_onSubmitHandler_javascriptDisabled() throws Exception {
final String firstHtml = "<html><head><title>First</title></head><body>\n" + "<form method='get' action='" + URL_SECOND + "' onSubmit='alert(\"clicked\")'>\n" + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n" + "</body></html>";
final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";
final WebClient client = getWebClientWithMockWebConnection();
client.getOptions().setJavaScriptEnabled(false);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, firstHtml);
webConnection.setDefaultResponse(secondHtml);
final HtmlPage firstPage = client.getPage(URL_FIRST);
final HtmlSubmitInput button = firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = button.click();
assertEquals("First", firstPage.getTitleText());
assertEquals("Second", secondPage.getTitleText());
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_javascriptAction.
/**
* @throws Exception if the test fails
*/
@Test
public void submit_javascriptAction() throws Exception {
final String firstHtml = "<html><head><title>First</title></head><body>\n" + "<form method='get' action='javascript:alert(\"clicked\")'>\n" + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n" + "</body></html>";
final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, firstHtml);
webConnection.setResponse(URL_SECOND, secondHtml);
final HtmlPage firstPage = client.getPage(URL_FIRST);
final HtmlSubmitInput button = firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = button.click();
assertEquals(firstPage.getTitleText(), secondPage.getTitleText());
assertEquals(new String[] { "clicked" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFrameSetTest method frameOnloadAccessOtherFrame.
/**
* Forward referencing issue in FrameSet.
* Test for bug #291
* @throws Exception if the test fails
*/
@Test
public void frameOnloadAccessOtherFrame() throws Exception {
final String framesContent = "<html><head><title>Main</title>\n" + "</head>\n" + " <frameset cols='18%,*'>\n" + " <frame name='menu' src='" + URL_SECOND + "'>\n" + " <frame name='button_pallete' src='about:blank'>\n" + " </frameset>\n" + "</html>";
final String menuContent = "<html><head><title>Menu</title>\n" + " <script>\n" + " function init() {\n" + " var oFrame = top.button_pallete;\n" + " alert((oFrame == null) ? 'Failure' : 'Success');\n" + " }\n" + " </script>\n" + "</head>\n" + "<body onload='init()'></body></html>";
final WebClient webClient = getWebClientWithMockWebConnection();
final MockWebConnection webConnection = getMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
final String[] expectedAlerts = { "Success" };
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
webConnection.setResponse(URL_FIRST, framesContent);
webConnection.setResponse(URL_SECOND, menuContent);
final HtmlPage framesPage = webClient.getPage(URL_FIRST);
assertEquals("Main", framesPage.getTitleText());
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFrameTest method onLoadHandler.
/**
* @throws Exception if the test fails
*/
@Test
public void onLoadHandler() throws Exception {
final WebClient webClient = getWebClientWithMockWebConnection();
final MockWebConnection webConnection = getMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final String html = "<html><head><title>first</title></head>\n" + "<frameset cols='20%,80%'>\n" + " <frame id='frame1'>\n" + " <frame onload='alert(this.tagName)' id='frame2'>\n" + "</frameset></html>";
final String[] expectedAlerts = { "FRAME" };
webConnection.setResponse(URL_FIRST, html);
final HtmlPage page = webClient.getPage(URL_FIRST);
assertEquals("first", page.getTitleText());
final HtmlFrame frame1 = page.getHtmlElementById("frame1");
assertEquals("frame1", "", ((HtmlPage) frame1.getEnclosedPage()).getTitleText());
final HtmlFrame frame2 = page.getHtmlElementById("frame2");
assertEquals("frame2", "", ((HtmlPage) frame2.getEnclosedPage()).getTitleText());
assertEquals(expectedAlerts, collectedAlerts);
}
Aggregations