Search in sources :

Example 1 with HtmlScript

use of com.gargoylesoftware.htmlunit.html.HtmlScript in project maven-doxia-sitetools by apache.

the class JavascriptVerifier method verify.

/**
 * Verifies a HtmlPage.
 *
 * @param file the file to verify.
 *
 * @throws Exception if something goes wrong.
 */
public void verify(String file) throws Exception {
    File jsTest = getTestFile("target/output/javascript.html");
    assertNotNull(jsTest);
    assertTrue(jsTest.exists());
    // HtmlUnit
    try (WebClient webClient = new WebClient()) {
        webClient.getOptions().setCssEnabled(false);
        final List<String> collectedAlerts = new ArrayList<String>(4);
        webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
        HtmlPage page = (HtmlPage) webClient.getPage(jsTest.toURI().toURL());
        assertNotNull(page);
        HtmlElement element = page.getHtmlElementById("contentBox");
        assertNotNull(element);
        HtmlDivision division = (HtmlDivision) element;
        assertNotNull(division);
        Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();
        // ----------------------------------------------------------------------
        // 
        // ----------------------------------------------------------------------
        HtmlSection section = (HtmlSection) elementIterator.next();
        assertNotNull(section);
        HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
        assertNotNull(h2);
        assertEquals("Test", h2.asText().trim());
        HtmlAnchor a = (HtmlAnchor) elementIterator.next();
        assertNotNull(a);
        assertEquals("Test", a.getAttribute("name"));
        HtmlParagraph p = (HtmlParagraph) elementIterator.next();
        assertNotNull(p);
        assertEquals("You should see a JavaScript alert...", p.asText().trim());
        HtmlScript script = (HtmlScript) elementIterator.next();
        assertNotNull(script);
        assertEquals("text/javascript", script.getAttribute("type"));
        assertEquals("", script.asText().trim());
        List<String> expectedAlerts = Collections.singletonList("Hello!");
        assertEquals(expectedAlerts, collectedAlerts);
    }
}
Also used : HtmlScript(com.gargoylesoftware.htmlunit.html.HtmlScript) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlHeading2(com.gargoylesoftware.htmlunit.html.HtmlHeading2) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) HtmlDivision(com.gargoylesoftware.htmlunit.html.HtmlDivision) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlAnchor(com.gargoylesoftware.htmlunit.html.HtmlAnchor) HtmlParagraph(com.gargoylesoftware.htmlunit.html.HtmlParagraph) HtmlSection(com.gargoylesoftware.htmlunit.html.HtmlSection) PlexusExtension.getTestFile(org.codehaus.plexus.testing.PlexusExtension.getTestFile) File(java.io.File)

Example 2 with HtmlScript

use of com.gargoylesoftware.htmlunit.html.HtmlScript in project faces by jakartaee.

the class URLClient method validateScript.

// End ajaxPDLResourceTest
// ------------------------------------------------------------------------------------------------------------
// private methods
private void validateScript(HtmlPage page) throws Fault {
    StringBuilder messages = new StringBuilder(128);
    Formatter formatter = new Formatter(messages);
    String script = "script";
    // Test by Resource name.
    HtmlScript resn = (HtmlScript) getElementOfTypeIncludingSrc(page, script, RES_NAME);
    if (resn == null) {
        formatter.format("Unexpected Test Result For %s Tag! %n" + "Expected Src Attribute to contain: %s %n", script, RES_NAME);
    }
    // Test by Resource Library name.
    HtmlScript resl = (HtmlScript) getElementOfTypeIncludingSrc(page, script, LIB_NAME);
    if (resl == null) {
        formatter.format("Unexpected Test Result For %s Tag! %n" + "Expected Src Attribute to contain: %s %n", script, LIB_NAME);
    }
    handleTestStatus(messages);
    formatter.close();
}
Also used : HtmlScript(com.gargoylesoftware.htmlunit.html.HtmlScript) Formatter(java.util.Formatter)

Example 3 with HtmlScript

use of com.gargoylesoftware.htmlunit.html.HtmlScript in project faces by jakartaee.

the class URLClient method outputScriptEncodeTest.

/*
   * @class.setup_props: webServerHost; webServerPort; ts_home;
   */
/**
 * @testName: outputScriptEncodeTest
 * @assertion_ids: PENDING
 * @test_Strategy: Validate the following test cases.
 *
 *                 case 1: Return the resource and render it when just the
 *                 'name' attribute is specified.
 *
 *                 case 2: Return the resource and render it when 'name' &
 *                 'library' attributes are specified.
 *
 * @since 2.0
 */
public void outputScriptEncodeTest() throws Fault {
    StringBuilder messages = new StringBuilder(128);
    Formatter formatter = new Formatter(messages);
    HtmlPage page = getPage(CONTEXT_ROOT + "/faces/encodetest_facelet.xhtml");
    // -------------------------------------------------------------- case 1
    String[] expected = { "hello.js", "goodbye.js" };
    List<HtmlScript> scripts = page.getDocumentElement().getHtmlElementsByTagName("script");
    boolean hjs = false, gjs = false;
    for (HtmlScript hs : scripts) {
        if (!hjs) {
            hjs = hs.getSrcAttribute().contains(expected[0]);
        }
        if (!gjs) {
            gjs = hs.getSrcAttribute().contains(expected[1]);
        }
    }
    if (!hjs) {
        formatter.format("Did not find script hello.js resource!");
    }
    if (!gjs) {
        formatter.format("Did not find script goodbye.js resource!");
    }
    handleTestStatus(messages);
}
Also used : HtmlScript(com.gargoylesoftware.htmlunit.html.HtmlScript) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Formatter(java.util.Formatter)

Example 4 with HtmlScript

use of com.gargoylesoftware.htmlunit.html.HtmlScript in project faces by jakartaee.

the class URLClient method testRelocatableElement.

// ---------------------------------------------------------- private
// methods
private void testRelocatableElement(HtmlPage page, String expected) throws Fault {
    StringBuilder messages = new StringBuilder(128);
    Formatter formatter = new Formatter(messages);
    HtmlScript htmlscript = (HtmlScript) getElementOfTypeIncludingSrc(page, SCRIPT, SCRIPT_NAME);
    String result = htmlscript.getParentNode().getNodeName();
    if (!expected.equals(result)) {
        formatter.format("Test FAILED.  Unexpected test result!" + NL + "Expected Parent Tag: '%s'" + NL + "Received Parent Tag: '%s", expected, result);
    }
    handleTestStatus(messages);
}
Also used : HtmlScript(com.gargoylesoftware.htmlunit.html.HtmlScript) Formatter(java.util.Formatter)

Example 5 with HtmlScript

use of com.gargoylesoftware.htmlunit.html.HtmlScript in project htmlunit by HtmlUnit.

the class HTMLScriptElement method getSrc.

/**
 * Returns the {@code src} property.
 * @return the {@code src} property
 */
@JsxGetter
public String getSrc() {
    final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
    String src = tmpScript.getSrcAttribute();
    if (ATTRIBUTE_NOT_DEFINED == src) {
        return src;
    }
    try {
        final URL expandedSrc = ((HtmlPage) tmpScript.getPage()).getFullyQualifiedUrl(src);
        src = expandedSrc.toString();
    } catch (final MalformedURLException e) {
    // ignore
    }
    return src;
}
Also used : MalformedURLException(java.net.MalformedURLException) HtmlScript(com.gargoylesoftware.htmlunit.html.HtmlScript) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) URL(java.net.URL) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Aggregations

HtmlScript (com.gargoylesoftware.htmlunit.html.HtmlScript)6 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)4 Formatter (java.util.Formatter)3 URL (java.net.URL)2 CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 HtmlAnchor (com.gargoylesoftware.htmlunit.html.HtmlAnchor)1 HtmlDivision (com.gargoylesoftware.htmlunit.html.HtmlDivision)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlHeading2 (com.gargoylesoftware.htmlunit.html.HtmlHeading2)1 HtmlParagraph (com.gargoylesoftware.htmlunit.html.HtmlParagraph)1 HtmlSection (com.gargoylesoftware.htmlunit.html.HtmlSection)1 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)1 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 PlexusExtension.getTestFile (org.codehaus.plexus.testing.PlexusExtension.getTestFile)1 Test (org.junit.Test)1