[Webtest] AJAX Support

Michal Michal <uhmm@1234.sk>
Thu, 05 Apr 2007 18:09:50 +0200


Here it goes, the simplest as it may get.

test2.php

<html>
<head>
</head>
<body>
<script language="JavaScript" type="text/javaScript" 
src="xmlhttprequest.js"></script>
<script language="JavaScript" type="text/javaScript" 
src="dropdown.js"></script>
<script language="JavaScript" type="text/javaScript">
function populatePerson()
{
    requestXMLDocument(
        "populateperson.xml"
            + "?roleid=" + document.getElementById('role').value,
        processRoleXMLHttpResponse
    );
}

function processRoleXMLHttpResponse()
{
    var response = getResponseXMLDocument();
    if (response != null) {
        var personDropdown = document.getElementById("person");
        clearDropdown(personDropdown);
        var results = response.getElementsByTagName("result");
        for (var i = 0; i < results.length; i++) {
            var id = 
results[i].getElementsByTagName("id")[0].firstChild.data;
            var name = 
results[i].getElementsByTagName("name")[0].firstChild.data;
            addDropdownEntry(personDropdown, i, name, id, false);
        } // for
       
    }
}
</script>
<select name="role" id="role" onchange="populatePerson();">
    <option value="1">Role 1</option>
    <option value="2">Role 2</option>
</select>

<select name="person" id="person">
</select>
</body>
</html>


populateperson.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <result><id>10</id><name>Application Architect</name></result>
    <result><id>9</id><name>Application Developer</name></result>
    <result><id>52</id><name>Content Creator</name></result>
    <result><id>46</id><name>Copywriter</name></result>
    <result><id>22</id><name>Creative Director</name></result>
    <result><id>7</id><name>Graphic Designer</name></result>
    <result><id>49</id><name>IT Support</name></result>
    <result><id>1</id><name>Manager</name></result>
    <result><id>2</id><name>Project Manager</name></result>
    <result><id>8</id><name>QA Specialist</name></result>
    <result><id>35</id><name>Translation QA</name></result>
    <result><id>33</id><name>Translator</name></result>
    <result><id>6</id><name>Web Developer</name></result>
    <result><id>51</id><name>Web Marketing PM</name></result>
    <result><id>5</id><name>Web Producer</name></result>
    <result><id>39</id><name>Web Writer</name></result>
    <result><id>38</id><name>Webmaster</name></result>
</response>



xmlhttprequest.js

requestQueue = new Array();        // queue of requests
noRequests = true;                // binary semaphore

function requestXMLDocument(url, func)
{
    requestQueue.push({url: url, func: func});
   
    if (noRequests) {
        var request = requestQueue.pop();
        loadXMLDoc(request.url, request.func);
    } // if
} // requestXMLDocument


function loadXMLDoc(url, func)
{
    noRequests = false;
   
    var d = new Date();
    var timestamp = "&ts="
        + "." + d.getDate()
        + "." + d.getHours()
        + "." + d.getMinutes()
        + "." + d.getSeconds();
    var newurl = url + timestamp;
   
    if (window.XMLHttpRequest) {
        // branch for native XMLHttpRequest object
        req = new XMLHttpRequest();
        req.onreadystatechange = func;
        req.open("GET", newurl, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        // branch for IE/Windows ActiveX version
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = func;
            req.open("GET", newurl, true);
            req.send();
        } // if
    } // if
} // loadXMLDoc


function getResponseXMLDocument()
{
    if (req.readyState == 4) {
        var tempRequest = req;
        if (requestQueue.length > 0) {
            var request = requestQueue.pop();
            loadXMLDoc(request.url, request.func);
        } else {
            noRequests = true;
        } // if
        if (tempRequest.status && tempRequest.status == 200) {
            return tempRequest.responseXML.documentElement;
        }
        return null;
    } // if
    return null;
} // getResponseXMLDocument


dropdown.js
function addDropdownEntry(dropdown, position, text, value, selected)
{    selected = selected || false;    // defaults to false

    var opt = document.createElement('option');
    opt.text = text;
    opt.value = value;
    opt.defaultSelected = selected;
    opt.selected = opt.defaultSelected;
    try {
        // standards compliant; doesn't work in IE
        dropdown.add(opt, dropdown.options[position]);
    } catch(ex) {
        // IE only
        dropdown.add(opt, position);
    } // try
} // addDropdownEntry


function clearDropdown(dropdown)
{
    while (dropdown.options.length > 0) {
        dropdown.remove(0);
    } // while
} // clearDropdown


function removeDropdownEntry(dropdown, toRemove)
{
    for (var i = 0; i < dropdown.options.length; i++) {
        if (dropdown.options[i].value == toRemove) {
            dropdown.options[i] = null;
            return;
        }
    }
}


function removeDropdownEntryByName(dropdown, toRemove)
{
    for (var i = 0; i < dropdown.options.length; i++) {
        if (dropdown.options[i].text == toRemove) {
            dropdown.options[i] = null;
            return;
        }
    }
}






Marc Guillemot wrote:
> really strange.
>
> Can you provide the smallest html+js file allowing to reproduce your error?
>
> Marc.
>
> uhmm wrote:
>   
>> I searched using find / -iname "*htmlunit*" and I found only one on
>> the whole disk. Version 1.11
>>
>>
>> Thursday, April 5, 2007, 9:01:13 AM, you wrote:
>>
>> MG> Have you searched on your whole disk for htmlunit*.jar?
>>
>> MG> Marc.
>>
>> MG> uhmm@1234.sk wrote:
>>     
>>>> How can I find out if it is a classpath problem? There so no
>>>> other htmlunit code on the machine.
>>>>
>>>>
>>>> ----- Original Message -----
>>>> From: Marc Guillemot <mguillemot@yahoo.fr>
>>>> To: webtest@lists.canoo.com
>>>> Subject: Re: [Webtest] AJAX Support
>>>> Date: Wed, 4 Apr 2007 06:05:56 -0700 (PDT)
>>>>
>>>>         
>>>>> Hi,
>>>>>
>>>>> I think that you have some classpath problem and that an
>>>>> older version of htmlunit is used because in htmlunit 1.11
>>>>> (the one now provided with WebTest) no
>>>>> NullPointerException can occur at line 102 of
>>>>> OptionsArray.
>>>>>
>>>>> Marc.
>>>>>
>>>>>
>>>>> uhmm@1234.sk wrote:
>>>>>           
>>>>>> Hey there,
>>>>>> I've been looking around internet for ajax support in
>>>>>> webtest, but
>>>>>> haven't found anything what would help me. Hope people
>>>>>> here can...
>>>>>>
>>>>>> I have school example of ajax usage. 2 dropdowns, first
>>>>>> one is
>>>>>> changed, second one is updated. How shall I create test
>>>>>> steps?
>>>>>>
>>>>>> Easy it may seem, but it's not. Here is my try:
>>>>>>
>>>>>> <setSelectField
>>>>>>        description="Select offering type"
>>>>>>        htmlid="fOfferingType"
>>>>>>        text="Application Support"
>>>>>> />
>>>>>> <sleep description="12s pause" seconds="12" />
>>>>>> <setSelectField
>>>>>>        description="Select offering subtype"
>>>>>>        htmlid="fOfferingSubtype"
>>>>>>        value="Other"
>>>>>> />
>>>>>>
>>>>>> Errors I'm getting:
>>>>>> [setSelectField]  INFO (com.canoo.webtest.steps.Step) -
>>>>>>             
>>>>>>>>>> Start
>>>>>>>>>>                     
>>>>>> Step: setSelectField "Select offering type" (13/15)
>>>>>>    [sleep] java.lang.NullPointerException
>>>>>>    [sleep]     at
>>>>>>
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.javascript.OptionsArray.get(
>>>>>           
>>>>>>    OptionsArray.java:102) [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.ScriptableObject.getProperty(Script
>>>>>           
>>>>>>    ableObject.java:1343) [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.ScriptRuntime.getObjectElem(ScriptR
>>>>>           
>>>>>>    untime.java:1304) [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.ScriptRuntime.getObjectElem(ScriptR
>>>>>           
>>>>>>    untime.java:1288) [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.Interpreter.interpretLoop(Interpret
>>>>>           
>>>>>> er.java(Compiled Code))
>>>>>>    [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.Interpreter.interpret(Interpreter.j
>>>>>           
>>>>>>    ava:2251) [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.InterpretedFunction.call(Interprete
>>>>>           
>>>>>>    dFunction.java:161) [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.ContextFactory.doTopCall(ContextFac
>>>>>           
>>>>>>    tory.java:340) [sleep]     at
>>>>>>
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFa
>>>>>           
>>>>>>    ctory.doTopCall(HtmlUnitContextFactory.java:151)
>>>>>> [sleep]     at
>>>>>>             
>>>>> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRunti
>>>>>           
>>>>>>    me.java:2758) [sleep]     at
>>>>>>
>>>>>>             
>>>>> org.mozilla.javascript.InterpretedFunction.call(Interprete
>>>>>           
>>>>>>    dFunction.java:159) [sleep]     at
>>>>>>
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.
>>>>>           
>>>>>>    callFunction(JavaScriptEngine.java:363) [sleep]    
>>>>>> at
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.javascript.host.XMLHttpReque
>>>>>           
>>>>>>    st.setState(XMLHttpRequest.java:145) [sleep]     at
>>>>>>
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.javascript.host.XMLHttpReque
>>>>>           
>>>>>>    st.doSend(XMLHttpRequest.java:375) [sleep]     at
>>>>>>
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.javascript.host.XMLHttpReque
>>>>>           
>>>>>>    st.access$000(XMLHttpRequest.java:72) [sleep]     at
>>>>>>
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.javascript.host.XMLHttpReque
>>>>>           
>>>>>>    st$1.run(XMLHttpRequest.java:328) [sleep]     at
>>>>>>    java.lang.Thread.run(Thread.java:570) [sleep]     at
>>>>>>
>>>>>>             
>>>>> com.gargoylesoftware.htmlunit.ThreadManager$1.run(ThreadMa
>>>>>           
>>>>>> nager.java:118) [setSelectField]  INFO
>>>>>> (com.canoo.webtest.steps.Step) - >>>> Start
>>>>>> Step: setSelectField "Select offering subtype" (15/15)
>>>>>> [setSelectField]  INFO (com.canoo.webtest.steps.Step) -
>>>>>> Running with:
>>>>>> Canoo Webtest: R_1537.
>>>>>> [setSelectField]  INFO (com.canoo.webtest.steps.Step) -
>>>>>> Exception
>>>>>> thrown from this class:
>>>>>> com.canoo.webtest.engine.StepFailedException
>>>>>> [setSelectField]  INFO (com.canoo.webtest.steps.Step) -
>>>>>> Message was:
>>>>>> No option found matching criteria in select
>>>>>> HtmlSelect[<select
>>>>>> name="fOfferingSubtype" id="fOfferingSubtype"
>>>>>> class="body-text-small "
>>>>>> onchange="showRelevantCountries();">]
>>>>>>  INFO (com.canoo.webtest.steps.Step) - Running with:
>>>>>> Canoo Webtest: R_1537.
>>>>>>  INFO (com.canoo.webtest.steps.Step) - Exception thrown
>>>>>> from this
>>>>>> class: com.canoo.webtest.engine.StepFailedException
>>>>>>  INFO (com.canoo.webtest.steps.Step) - Message was: No
>>>>>> option found
>>>>>> matching criteria in select HtmlSelect[<select
>>>>>> name="fOfferingSubtype"
>>>>>> id="fOfferingSubtype" class="body-text-small "
>>>>>> onchange="showRelevantCountries();">]
>>>>>>
>>>>>>
>>>>>> What is wrong? As you can see I have the latest Webtest
>>>>>> revision with
>>>>>> latest HtmlUnit...
>>>>>> Exception coming out of sleep?
>>>>>>
>>>>>> Your help is very appreciated!
>>>>>> _______________________________________________
>>>>>> WebTest mailing list
>>>>>> WebTest@lists.canoo.com
>>>>>> http://lists.canoo.com/mailman/listinfo/webtest
>>>>>>
>>>>>>
>>>>>>             
>>>>> -- 
>>>>> View this message in context:
>>>>> http://www.nabble.com/AJAX-Support-tf3525274.html#a9836506
>>>>> Sent from the WebTest mailing list archive at Nabble.com.
>>>>>
>>>>> _______________________________________________
>>>>> WebTest mailing list
>>>>> WebTest@lists.canoo.com
>>>>> http://lists.canoo.com/mailman/listinfo/webtest
>>>>>           
>>>> _______________________________________________
>>>> WebTest mailing list
>>>> WebTest@lists.canoo.com
>>>> http://lists.canoo.com/mailman/listinfo/webtest
>>>>
>>>>         
>> MG> _______________________________________________
>> MG> WebTest mailing list
>> MG> WebTest@lists.canoo.com
>> MG> http://lists.canoo.com/mailman/listinfo/webtest
>>
>>
>> _______________________________________________
>> WebTest mailing list
>> WebTest@lists.canoo.com
>> http://lists.canoo.com/mailman/listinfo/webtest
>>
>>     
>
> _______________________________________________
> WebTest mailing list
> WebTest@lists.canoo.com
> http://lists.canoo.com/mailman/listinfo/webtest
>
>
>
>