I still don't find any appropriate name for this tag, but I name it HtmlUnitExtractElement but it is up to the project leader :)
So I need a tag to get any thing as a string for the given xpath. So it can be a attribute value, node value, or the html element or anything but it is a simple string.
But the current HtmlUnitHelper returns a html element based on xpath. HtmlUnitHelper uses HtmlUnitXpath which extends BaseXPath from jaxen. And it has stringValueOf() method to parse all the value as string.
I add this method inside HtmlUnitHelper.
[code]
/**
* Gets an HtmlElement matching the provided xpath expression
* @param container - The DOM to start at
* @param xpath - An XPath expression that matches the desired HtmlElement
*
* @return The matching HtmlElement
*/
public String getStringValueOfHtmlElementByXPath(String xpath) {
String valueOfString = null;
DomNode container = (HtmlPage) getCurrentPage();
if ( xpath != null) {
Navigator nav = HtmlUnitXPath.buildSubtreeNavigator(container);
try {
HtmlUnitXPath xp = new HtmlUnitXPath(xpath, nav);
valueOfString = (xp.stringValueOf(null));
} catch ( JaxenException je ) {
throw new RuntimeException("Problem processing XPath '"+xpath+"'", je);
}
} else {
throw new RuntimeException("Neither xpath nor container can be null! Supply an xpath expression and the container to execute it in.");
}
return valueOfString;
}
[/code]
and I create a new tag which uses this functionality which is HtmlUnitExtractElement (i send as attachment).
Logged In: YES
user_id=1455260
Originator: YES
sorry, i redesign the tag.
this one is easier and simpler. Btw i don't know where i put the code, so that I put it here first and up to admin to move it somewhere.
/*
* File: HtmlUnitExtractText.java
* System: Jamba! Plattform
* Module: net.sf.jameleon.plugin.htmlunit.tags
* Author: jzastrow
* Copyright: Jamba! GmbH
* Source: $Source: /var/lib/cvs/core-002/jameleon/jameleon-htmlunit-plugin/src/java/net/sf/jameleon/plugin/htmlunit/tags/HtmlUnitExtractElement.java,v $
* Last modified by: $Author: slawalata $
* Date: $Date: 2008-03-05 14:12:56 $
* Version: $Revision: 1.2 $
* Description:
* Preconditions:
*/
package net.sf.jameleon.plugin.htmlunit.tags;
import net.sf.jameleon.bean.Session;
import net.sf.jameleon.plugin.htmlunit.HtmlUnitFunctionTag;
import net.sf.jameleon.plugin.htmlunit.util.HtmlUnitDelegate;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
/**
* to get the attribute value or the node value of html element inside a htmlpage and stored inside resultVariable
* <pre><source>
* <html>
* <center>
* <p>
* <font size="-2"> Test Yuu hu hu hu </font>
* </p>
* <center>
* </html>
*
*
* <htmlunit-extract-element
* functionId="extract the html element based on xpath"
* xpath="//center/p/font"
* resultVariable="testHtml"/>
*
* <function-doc
* functionId="print the element as xml : ${testHtml.asXml()}"/>
*
* <function-doc
* functionId="print size attribute 'size' value ${testHtml.getAttributeValue('size')}"/>
*
* <function-doc
* functionId="print this element or node value as text: ${testHtml.asText()}"/>
* </source></pre>
*
* @author slawalata
* @jameleon.function name="htmlunit-extract-element" type="action"
*/
public class HtmlUnitExtractElement extends HtmlUnitFunctionTag {
/**
* xpath pattern used to extract the value of attribute or node of HTML element.
* Example: //center/p
* @jameleon.attribute required="true"
*/
String xpath;
/**
* The result should be stored to this variable.
* @jameleon.attribute required="true"
*/
String resultVariable;
@Override
public void testBlock() throws Exception {
HtmlElement result = helper.getHtmlElementByXPath(xpath);
if (result==null)
fail("No html element matching the xpath '"+xpath+"'");
else
setVariable(resultVariable, result);
log.info("Extracted element text is\n"+result.asXml()+"\n");
}
}
File Added: HtmlUnitExtractElement.java
Logged In: YES
user_id=1455260
Originator: YES
and of course HtmlUnitXPatch is leaved without any changes like it was which without getStringValueOfHtmlElementByXPath()