[Webtest] Reading values from an XML File

Matt Raible webtest@lists.canoo.com
Wed, 8 Oct 2003 13:28:25 -0600


The #{count} value is 0, I'm not setting it.  I tried removing the 
<repeat> wrappers and putting in "1" and this worked.  Is it possible 
to increment the "repeat" variable by 1?

Thanks,

Matt

On Wednesday, October 8, 2003, at 01:11 PM, Daniel Potter wrote:

> I believe Xpath indeces begin at 1 (instead of 0).  Try
> //Menus/Menu[1]/@page instead.  That should match the xml you listed
> below.
>
> Daniel
>
> On Wed, Oct 08, 2003 at 12:38:19PM -0600, Matt Raible wrote:
>> I'm trying to implement this - exactly as you have below - and I'm
>> getting a StackOverFlow on super.execute();
>>
>> public void doExecute(final Context context) throws Exception {
>>         super.execute(context);
>>
>> I tried removing it, and it seems to run, but now I get:
>>
>> No match for xpath expression <//Menus/Menu[0]/@page>
>>
>> My menu-config.xml looks like this:
>>
>> <MenuConfig><Menus><Menu name="" page=""/></Menus></MenuConfig>
>>
>> Thanks,
>>
>> Matt
>>
>> On Tuesday, October 7, 2003, at 09:34 PM, Daniel Potter wrote:
>>
>>> Matt,
>>> Here is the code for a custom WebTest step that would do what I
>>> mentioned in
>>> my previous email (I didn't try to compile it, but it should work).
>>> Just drop
>>> it in the src/com/canoo/webtest/extension directory, run
>>> WebTest's "clean_compile" target, and add it to the
>>> webtestTaskdefs.properties
>>> file.
>>>
>>> Then use it in your script like this:
>>>
>>>    <target name="displayMenus">
>>>    <testSpec name="">
>>>    <config ... />
>>>    <steps>
>>>      <loadXml file="${basedir}/defaultroot/WEB-INF/menu-config.xml"/>
>>>      <repeat count="<-- insert number of menus -->">
>>>        <storexpath
>>>            xpath="//Item[#{count}]/@page"
>>>            property="page" />
>>>        <invoke
>>>            url="#{page}"/>
>>>      </repeat>
>>>      ...
>>>    </steps>
>>>    </testSpec>
>>>    </target>
>>>
>>> Unfortunately, this requires you to know the number of menu items in
>>> your xml
>>> file and hardcode it in the script.  You could modify the code below
>>> to use an
>>> xml api to determine the number of items in the file and set it as a
>>> WebTest
>>> dynamic property (using the Step class's setDynamicProperty() method)
>>> to get
>>> around this.
>>>
>>> Hope that helps.  Good luck!
>>>
>>> Daniel
>>>
>>> =========================
>>> package com.canoo.webtest.extension;
>>>
>>> import java.net.HttpURLConnection;
>>> import java.net.URL;
>>> import java.io.*;
>>> import java.util.HashMap;
>>> import java.util.Map;
>>>
>>> import com.canoo.webtest.engine.Context;
>>> import com.canoo.webtest.engine.StepExecutionException;
>>> import com.canoo.webtest.self.WebResponseStub;
>>> import com.canoo.webtest.steps.Step;
>>> import com.meterware.httpunit.WebResponse;
>>>
>>> /**
>>> * Custom WebTest (webtest.canoo.com) step that reads an XML file and
>>> uses
>>> * the result to set WebTest's last response so that the standard
>>> WebTest
>>> * tasks can be used to validate and/or utilize the contents of the
>>> message.
>>> */
>>> public class LoadXmlStep extends Step {
>>>
>>>    private static String DUMMY_URL = "http://dummyurl";
>>>    private static String FILE = "file";
>>>
>>>    private String file;
>>>
>>>    public LoadXmlStep() {
>>>        super();
>>>    }
>>>
>>>    public void doExecute(final Context context) throws Exception {
>>>        super.doExecute(context);
>>>        verifyParameters();
>>>
>>>        String line = null;
>>>        StringBuffer xml = new StringBuffer();
>>>        BufferedReader reader =
>>>            new BufferedReader(
>>>                new FileReader(file));
>>>        while ((line = reader.readLine()) != null) {
>>>            sb.append(line);
>>>        }
>>>        reader.close();
>>>
>>>        WebResponse response =
>>>            new XmlWebResponseStub(
>>>                xml.toString(),
>>>                new URL(DUMMY_URL),
>>>                HttpURLConnection.HTTP_OK);
>>>        context.setLastResponse(response);
>>>    }
>>>
>>>    protected void verifyParameters() {
>>>        if (file == null) {
>>>            throw new StepExecutionException(
>>>                "Required parameter " + FILE + " not set!");
>>>        }
>>>    }
>>>
>>>    public void expandProperties() {
>>>        super.expandProperties();
>>>        file = expandDynamicProperties(file);
>>>    }
>>>
>>>    public Map getParameterDictionary() {
>>>        Map map = new HashMap();
>>>        map.put(FILE, file);
>>>        return map;
>>>    }
>>>
>>>    public void setFile(String file) {
>>>        this.file = file;
>>>    }
>>>
>>> }
>>>
>>> /**
>>> * Stub for web responses containing XML.  Overrides default HTML
>>> content-type
>>> * that's hardcoded in WebResponseStub superclass and sets it to
>>> indicate XML
>>> * content.
>>> */
>>> class XmlWebResponseStub extends WebResponseStub {
>>>
>>>    public static final String CONTENT_TYPE_HEADER = "Content-type";
>>>    public static final String XML_CONTENT_TYPE =
>>>        "application/xml; charset=utf-8";
>>>
>>>    /**
>>>     * Constructor for XmlWebResponseStub.
>>>     * @param content String containing the XML content of the web
>>> response
>>>     * @param url URL associated with the original request (can be a
>>> dummy URL
>>>     * since this is just a stub and not the result of a real web
>>> request)
>>>     * @param returnCode HTTP status/error code
>>>     */
>>>    public XmlWebResponseStub(String content, URL url, int returnCode)
>>> {
>>>        super(content, url, returnCode);
>>>        Map headers = getHeaders();
>>>        headers.put(CONTENT_TYPE_HEADER, XML_CONTENT_TYPE);
>>>    }
>>> }
>>> ============================
>>>
>>>
>>> -------------------------------------------------
>>> This mail sent through IMP: http://horde.org/imp/
>>