org.openqa.selenium.htmlunit.HtmlUnitDriver Java Examples

The following examples show how to use org.openqa.selenium.htmlunit.HtmlUnitDriver. 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: SvgMetadataTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGMetadataElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <metadata id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgMetadata.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #2
Source File: HtmlSpan2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Test that HTMLSpanElement is the default for other elements like 'address', 'code', 'strike', etc.
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "[object HTMLElement]",
        IE = "[object HTMLBlockElement]")
public void simpleScriptable_others() throws Exception {
    final String html = "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <address id='myId'>My Address</address>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(HtmlAddress.class.isInstance(page.getHtmlElementById("myId")));
    }
}
 
Example #3
Source File: HtmlHorizontalRuleTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object HTMLHRElement]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <hr id='myId'>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(HtmlHorizontalRule.class.isInstance(page.getHtmlElementById("myId")));
    }
}
 
Example #4
Source File: HtmlPasswordInputTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if an error occurs
 */
@Test
@Alerts("password")
public void upperCase() throws Exception {
    final String html =
          "<html><head><script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId').type);\n"
        + "  }\n"
        + "</script></head>\n"
        + "<body onload='test()'>\n"
        + "  <input TYPE='password' id='myId'>\n"
        + "</body></html>";
    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(HtmlPasswordInput.class.isInstance(page.getHtmlElementById("myId")));
    }
}
 
Example #5
Source File: HtmlEmbedTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object HTMLEmbedElement]")
public void saveAs() throws Exception {
    final String html = "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <embed id='myId' src='helloworld.bin'/>\n"
        + "</body></html>";

    getMockWebConnection().setDefaultResponse("something");
    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlEmbed element = (HtmlEmbed) toHtmlElement(driver.findElement(By.id("myId")));
        final File file = new File(System.getProperty("user.home"), "htmlunit-embed.bin");
        element.saveAs(file);
        final long length = file.length();
        assertTrue(file.delete());
        assertTrue(length > 0);
    }
}
 
Example #6
Source File: HtmlFieldSetTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"[object HTMLFieldSetElement]", "[object HTMLFormElement]"})
public void simpleScriptable() throws Exception {
    final String html
        = "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    var fs = document.getElementById('fs');\n"
        + "    alert(fs);\n"
        + "    alert(fs.form);\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <form>\n"
        + "    <fieldset id='fs'>\n"
        + "      <legend>Legend</legend>\n"
        + "    </fieldset>\n"
        + "  </form>\n"
        + "</body></html>";
    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(driver.findElement(By.id("fs")));
        assertTrue(element instanceof HtmlFieldSet);
    }
}
 
Example #7
Source File: SvgFeDiffuseLightingTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGFEDiffuseLightingElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <feDiffuseLighting id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgFeDiffuseLighting.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #8
Source File: HtmlLabelTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void getLabeledElementForButton() throws Exception {
    final String html = "<html>\n"
        + "<body>\n"
        + "  <label id='label1' for='button1'>Item</label>\n"
        + "  <button id='button1'></button>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        final HtmlLabel label = page.getHtmlElementById("label1");
        assertEquals("button1", label.getLabeledElement().getId());
    }
}
 
Example #9
Source File: SvgFeTurbulenceTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGFETurbulenceElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <feTurbulence id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgFeTurbulence.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #10
Source File: SvgAltGlyphItemTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <altGlyphItem id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgElement.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #11
Source File: SvgLinearGradientTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGLinearGradientElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <linearGradient id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgLinearGradient.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #12
Source File: SvgFePointLightTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGFEPointLightElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <fePointLight id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgFePointLight.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #13
Source File: HtmlSerializerVisibleText2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private void getVisibleTextWhiteSpaceTextArea(final String whiteSpace) throws Exception {
    final String htmlContent
        = "<html>\n"
        + "<head></head>\n"
        + "<body>\n"
        + "  <textarea id='tester' " + (whiteSpace == null ? "" : ("style='white-space: " + whiteSpace + "'")) + ">"
                + "  A B  C\t \t  D \r\nEF\nG \n H  <br> I  </textarea>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(htmlContent);
    final String text = driver.findElement(By.id("tester")).getText();
    assertEquals(getExpectedAlerts()[0], text);

    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertEquals(getExpectedAlerts()[0], page.getElementById("tester").getVisibleText());
    }
}
 
Example #14
Source File: SvgFeFloodTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGFEFloodElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <feFlood id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgFeFlood.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #15
Source File: HtmlLink2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object HTMLLinkElement]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n"
        + "<link id='myId' href='file1.css'>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(HtmlLink.class.isInstance(page.getHtmlElementById("myId")));
    }
}
 
Example #16
Source File: HtmlTextArea2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("")
public void getVisibleTextAndVisibility() throws Exception {
    final String html
        = "<html><head></head><body>\n"
        + "<form id='form1'>\n"
        + "<textarea id='tester' style='visibility:hidden'> foo \n bar "
        + "</textarea>\n"
        + "</form></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement textArea = driver.findElement(By.id("tester"));
    assertEquals(getExpectedAlerts()[0], textArea.getText());

    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertEquals(getExpectedAlerts()[0], page.getElementById("tester").getVisibleText());
    }
}
 
Example #17
Source File: SvgCursorTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <cursor id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgElement.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #18
Source File: BrowserTabUtil.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private BrowserTabUtil(WebDriver driver) {
    this.driver = driver;

    if (driver instanceof JavascriptExecutor) {
        this.jsExecutor = (JavascriptExecutor) driver;
    } else {
        throw new RuntimeException("WebDriver must be instance of JavascriptExecutor");
    }

    // HtmlUnit doesn't work very well with JS and it's recommended to use this settings.
    // HtmlUnit validates all scripts and then fails. It turned off the validation.
    if (driver instanceof HtmlUnitDriver) {
        WebClient client = ((DroneHtmlUnitDriver) driver).getWebClient();
        client.getOptions().setThrowExceptionOnScriptError(false);
        client.getOptions().setThrowExceptionOnFailingStatusCode(false);
    }

    tabs = new ArrayList<>(driver.getWindowHandles());
}
 
Example #19
Source File: HtmlLabelTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void getLabeledElementNone() throws Exception {
    final String html = "<html>\n"
        + "<body>\n"
        + "  <label id='label1'>Item</label>\n"
        + "  <input type='text' id='text1'>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        final HtmlLabel label = page.getHtmlElementById("label1");
        assertNull(label.getLabeledElement());
    }
}
 
Example #20
Source File: SvgViewTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGViewElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <view id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgView.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #21
Source File: HtmlSerializerVisibleText2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private void getVisibleTextWhiteSpaceBreak(final String whiteSpace) throws Exception {
    final String htmlContent
        = "<html>\n"
        + "<head></head>\n"
        + "<body>\n"
        + "  <br id='tester' " + (whiteSpace == null ? "" : ("style='white-space: " + whiteSpace + "'")) + ">\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(htmlContent);
    final String text = driver.findElement(By.id("tester")).getText();
    assertEquals(getExpectedAlerts()[0], text);

    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertEquals(getExpectedAlerts()[0], page.getElementById("tester").getVisibleText());
    }
}
 
Example #22
Source File: SvgPolylineTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGPolylineElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <polyline id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgPolyline.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #23
Source File: SvgPathTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGPathElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <path id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgPath.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #24
Source File: HTMLOListElementTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object HTMLOListElement]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <ol id='myId'/>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final WebElement element = driver.findElement(By.id("myId"));
        assertTrue(toHtmlElement(element) instanceof HtmlOrderedList);
    }
}
 
Example #25
Source File: HiddenHtmlPrompt.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
public HiddenHtmlPrompt( UiDriver uiDriver ) {

        super(uiDriver);

        HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver;
        HtmlUnitDriver driver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name());
        Field webClientField = null;
        boolean fieldAccessibleState = false;
        try {

            TargetLocator targetLocator = driver.switchTo();
            webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
            fieldAccessibleState = webClientField.isAccessible();
            webClientField.setAccessible(true);
            webClient = (WebClient) webClientField.get(targetLocator.defaultContent());

        } catch (Exception e) {

            throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
        } finally {

            if (webClientField != null) {
                webClientField.setAccessible(fieldAccessibleState);
            }
        }
    }
 
Example #26
Source File: SvgFeSpotLightTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGFESpotLightElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <feSpotLight id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgFeSpotLight.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #27
Source File: HtmlSerializerVisibleText2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private void getVisibleTextWhiteSpaceInputRadio(final String whiteSpace) throws Exception {
    final String htmlContent
        = "<html>\n"
        + "<head></head>\n"
        + "<body>\n"
        + "<form id='form1'>\n"
        + "  <input type='radio' name='tester' id='tester' "
                    + (whiteSpace == null ? "" : ("style='white-space: " + whiteSpace + "'"))
                    + " value='  A B  C\t \t  D \r\nEF\nG \n H  <br> I  '>\n"
        + "</form>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(htmlContent);
    final String text = driver.findElement(By.id("tester")).getText();
    assertEquals(getExpectedAlerts()[0], text);

    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertEquals(getExpectedAlerts()[0], page.getElementById("tester").getVisibleText());
    }
}
 
Example #28
Source File: HtmlHeadTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object HTMLHeadElement]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head id='myId'><script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(driver.findElement(By.id("myId")));
        assertTrue(element instanceof HtmlHead);
    }
}
 
Example #29
Source File: SvgSwitchTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("[object SVGSwitchElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    alert(document.getElementById('myId'));\n"
        + "  }\n"
        + "</script>\n"
        + "</head><body onload='test()'>\n"
        + "  <svg xmlns='http://www.w3.org/2000/svg' version='1.1'>\n"
        + "    <switch id='myId'/>\n"
        + "  </svg>\n"
        + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlPage page = (HtmlPage) getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertTrue(SvgSwitch.class.isInstance(page.getElementById("myId")));
    }
}
 
Example #30
Source File: DomElementTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"2", "2"})
public void getElementsByTagName() throws Exception {
    final String html = "<html><head><script>\n"
            + "function test() {\n"
            + "  alert(document.f1.getElementsByTagName('input').length);\n"
            + "  alert(document.f1.getElementsByTagName('INPUT').length);\n"
            + "}\n"
            + "</script></head>\n"
            + "<body onload='test()'>\n"
            + "  <form name='f1'>\n"
            + "    <input>\n"
            + "    <INPUT>\n"
            + "  </form>\n"
            + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final WebWindow webWindow = getWebWindowOf((HtmlUnitDriver) driver);
        final HtmlPage page = (HtmlPage) webWindow.getEnclosedPage();
        assertEquals(2, page.getForms().get(0).getElementsByTagName("input").size());
        assertEquals(2, page.getForms().get(0).getElementsByTagName("INPUT").size());
    }
}