Search in sources :

Example 1 with MockWebConnection

use of com.gargoylesoftware.htmlunit.MockWebConnection in project arquillian-extension-drone by arquillian.

the class HtmlUnitDriverTest method shouldOverrideWebClientOptions.

@Test
public void shouldOverrideWebClientOptions() throws IOException {
    final HtmlUnitDriverFactory htmlUnitDriverFactory = new HtmlUnitDriverFactory();
    final WebDriverConfiguration configuration = getMockedConfiguration("ThrowExceptionOnFailingStatusCode=true");
    final WebDriver driver = htmlUnitDriverFactory.createInstance(configuration);
    final DroneHtmlUnitDriver htmlUnitDriver = (DroneHtmlUnitDriver) driver;
    MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse("http://foo.com", 503, "Service Unavailable", "text/plain");
    WebClient webClient = htmlUnitDriver.getWebClient();
    webClient.setWebConnection(webConnection);
    thrown.expect(WebDriverException.class);
    thrown.expectMessage("com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 503 Service Unavailable for http://foo.com/");
    driver.get("http://foo.com");
    driver.quit();
}
Also used : WebDriver(org.openqa.selenium.WebDriver) DroneHtmlUnitDriver(org.jboss.arquillian.drone.webdriver.htmlunit.DroneHtmlUnitDriver) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebDriverConfiguration(org.jboss.arquillian.drone.webdriver.configuration.WebDriverConfiguration) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 2 with MockWebConnection

use of com.gargoylesoftware.htmlunit.MockWebConnection in project htmlunit by HtmlUnit.

the class HtmlAnchorTest method click.

/**
 * @throws Exception if the test fails
 */
@Test
public void click() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n" + "<a href='" + URL_SECOND + "' id='a2'>link to foo2</a>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setDefaultResponse(secondContent);
    final WebDriver driver = loadPage2(html);
    assertEquals(1, webConnection.getRequestCount());
    // Test that the correct value is being passed back up to the server
    driver.findElement(By.id("a2")).click();
    assertEquals(URL_SECOND.toExternalForm(), driver.getCurrentUrl());
    assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
    assertTrue(webConnection.getLastParameters().isEmpty());
    assertEquals(2, webConnection.getRequestCount());
}
Also used : WebDriver(org.openqa.selenium.WebDriver) BuggyWebDriver(com.gargoylesoftware.htmlunit.junit.BrowserRunner.BuggyWebDriver) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) Test(org.junit.Test)

Example 3 with MockWebConnection

use of com.gargoylesoftware.htmlunit.MockWebConnection in project htmlunit by HtmlUnit.

the class HtmlAnchor2Test method click_onClickHandler.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_onClickHandler() throws Exception {
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n" + "<a href='" + URL_SECOND + "' id='a2' " + "onClick='alert(\"clicked\")'>link to foo2</a>\n" + "<a href='http://www.foo3.com' id='a3'>link to foo3</a>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    client.setWebConnection(webConnection);
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = anchor.click();
    assertEquals(new String[] { "clicked" }, collectedAlerts);
    assertEquals("Second", secondPage.getTitleText());
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 4 with MockWebConnection

use of com.gargoylesoftware.htmlunit.MockWebConnection in project htmlunit by HtmlUnit.

the class HtmlAnchor2Test method clickShiftCtrlJavascript.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickShiftCtrlJavascript() throws Exception {
    final String first = "<html><head><title>First</title></head><body>\n" + "  <a href='javascript: window.location=\"" + URL_SECOND + "\"' id='a2'>link to foo2</a>\n" + "</body></html>";
    final String second = "<html><head><title>Second</title></head><body></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, first);
    webConnection.setResponse(URL_SECOND, second);
    client.setWebConnection(webConnection);
    assertEquals(1, getWebClient().getTopLevelWindows().size());
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    final HtmlPage secondPage = anchor.click(true, true, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("First", secondPage.getTitleText());
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 5 with MockWebConnection

use of com.gargoylesoftware.htmlunit.MockWebConnection in project htmlunit by HtmlUnit.

the class HtmlAnchor2Test method clickCtrlShift.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickCtrlShift() throws Exception {
    final String first = "<html><head><title>First</title></head><body>\n" + "  <a href='" + URL_SECOND + "' id='a2'>link to foo2</a>\n" + "</body></html>";
    final String second = "<html><head><title>Second</title></head><body></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, first);
    webConnection.setResponse(URL_SECOND, second);
    client.setWebConnection(webConnection);
    assertEquals(1, getWebClient().getTopLevelWindows().size());
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    final HtmlPage secondPage = anchor.click(true, true, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("First", secondPage.getTitleText());
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Aggregations

MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)274 Test (org.junit.Test)252 WebClient (com.gargoylesoftware.htmlunit.WebClient)209 ArrayList (java.util.ArrayList)98 CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)82 URL (java.net.URL)70 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)62 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)48 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)35 Page (com.gargoylesoftware.htmlunit.Page)21 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)21 WebDriver (org.openqa.selenium.WebDriver)21 File (java.io.File)20 HtmlAnchor (com.gargoylesoftware.htmlunit.html.HtmlAnchor)18 HtmlPageTest (com.gargoylesoftware.htmlunit.html.HtmlPageTest)11 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)10 BuggyWebDriver (com.gargoylesoftware.htmlunit.junit.BrowserRunner.BuggyWebDriver)10 WebWindowEvent (com.gargoylesoftware.htmlunit.WebWindowEvent)8 WebWindowListener (com.gargoylesoftware.htmlunit.WebWindowListener)8 LinkedList (java.util.LinkedList)8