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);
}
}
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();
}
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);
}
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);
}
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;
}
Aggregations