So, here it is, the minimal test script.
<?xml version="1.0" ?>

<!DOCTYPE project SYSTEM "../config/MyWebTest.dtd" [
        <!ENTITY makeDate "
// make a Date out of a String dd.mm.yyyy
def makeDate(event) {
    def dmy = event.split(/\./).collect { num -> Integer.parseInt(num.trim()) }
    def eventDate = Calendar.getInstance()
    eventDate.set(dmy[2], dmy[1] - 1, dmy[0], 0, 0, 0)
    eventDate.set(Calendar.MILLISECOND, 0)
    return eventDate
}

def isDateInRange(event) {
    // this statement breaks if eventTo is not a Date!
    eventTo.after(event)
}
">
        ] >

<project name="testGroovyDefNoDef" default="full" basedir=".">

    <target name="full">
        <antcall target="testScriptStepNoDef"/>
        <antcall target="testScriptStepDef"/>
        <antcall target="testGroovyNoDef"/>
        <antcall target="testGroovyDef"/>
    </target>

    <property name="eventTo" value="30.04.2006"/>

    <target name="testScriptStepNoDef">
        <webtest name="testScriptStepNoDef">
            &sharedConfiguration;
            <steps>
<!-- works as expected -->
                <scriptStep language="groovy">
                    &makeDate;
                    eventTo = makeDate(step.project.properties.eventTo)
                    isDateInRange(makeDate('1.5.2001'))
                </scriptStep>
            </steps>
        </webtest>
    </target>

    <target name="testScriptStepDef">
        <webtest name="testScriptStepDef">
            &sharedConfiguration;
            <steps>
<!--
Test failed.
Exception raised:
com.canoo.webtest.engine.StepExecutionException: Error invoking script:
groovy.lang.MissingMethodException:
No signature of method java.lang.String.after() is applicable for argument types:
(java.util.GregorianCalendar) values: {java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=&quot;Europe/Zurich&quot;,offset=3600000,dstSavings=3600000,useDaylight=true,transitions=121,lastRule=java.util.SimpleTimeZone[id=Europe/Zurich,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2001,MONTH=4,WEEK_OF_YEAR=12,WEEK_OF_MONTH=4,DAY_OF_MONTH=1,DAY_OF_YEAR=82,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=9,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=0]}
 -->

                <scriptStep language="groovy">
                    &makeDate;
                    def eventTo = makeDate(step.project.properties.eventTo)
                    isDateInRange(makeDate('1.5.2001'))
                </scriptStep>
            </steps>
        </webtest>
    </target>

    <target name="testGroovyNoDef">
        <webtest name="testGroovyNoDef">
            &sharedConfiguration;
            <steps>
                <!-- works as expected -->
                <groovy>
                    &makeDate;
                    eventTo = makeDate(step.project.properties.eventTo)
                    isDateInRange(makeDate('1.5.2001'))
                </groovy>
            </steps>
        </webtest>
    </target>

    <target name="testGroovyDef">
        <webtest name="testGroovyDef">
            &sharedConfiguration;
            <steps>
<!--
Test failed.
Exception raised:
com.canoo.webtest.engine.StepExecutionException: Error invoking groovy:
No such property: eventTo for class: Script1
 -->
                <groovy>
                    &makeDate;
                    def eventTo = makeDate(step.project.properties.eventTo)
                    isDateInRange(makeDate('1.5.2001'))
                </groovy>
            </steps>
        </webtest>
    </target>

</project>

If placed in the selftests/tests/ directory of webtest, it can be called with:
bin/webtest.sh functionalTest -DtestScript groovyDefNoDef



The script shows that
1) scriptStep and groovy succeed without the def,
2) and both fail with a def, but with different exceptions.

Why can't both steps behave in the same way?

I think I could understand why it fails in the groovy case:
a def name would only be known in the scope it is defined,
and the scope would be the statements in the script.
But then I would expect this rule to hold for method too:

def m2(text) {
    m1(text)
}

def m1(text) {
    println(text)
}

m2("hello")

Perplexed, and waiting for the groovy book ;-)

        dna


On 22 mars 06, at 13:49, Dierk Koenig wrote:

If at all, that's a bug in Groovy, not in ScriptStep.
Anyway I don't think, that is the case either.

I'm a bit puzzled by the sequence of steps.
What exact sequence leads to the error?
(please include all declarations)

cheers
Mittie


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Denis N. Antonioli
Sent: Mittwoch, 22. Marz 2006 13:17
To: Webtest
Subject: [Webtest] Bug in ScriptStep?


Hi

I've just found a very un-intuitive behavior in ScriptStep: at least
in groovy code, an ant property name hides a groovy variable name!

I have a webtest that defines the tthe ant property ${eventFrom}.
The test then calls this groovy code through scriptStep:

def makeDate(event) {
        // convert the string to a GregorianCalendar and returns it
        // return new GregorianCalendar(event)
}

eventFrom = makeDate(step.project.properties.eventFrom)

// in real text, extract the event date form the current response
thisEvent = makeDate('22.3.2006')

// compares two Calendar objects
assert eventFrom.before(thisEvent)



This groovy code works,  but if I change to

def eventFrom = makeDate(step.project.properties.eventFrom)

The test breaks with
"No signature of method java.lang.String.before() is applicable for
argument types (java.util.GregorianCalendar ...)"

Groovy used the ant property instead of its own variable. I would
expect the groovy code to take precedence over the enclosing webtest
stuff, as the groovy code is the nearer scope.

Is this really the desired behavior?

How is it with other scripting language?

Best
        dna
--
Jakob's Law of the Web User Experience: Users spend most of their
time on other sites.
   -- Jakob Nielsen's Alertbox, August 22, 1999

_______________________________________________
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

--
Two ways to build software:
 (1) make it so simple that there are obviously no bugs,
 (2) make it so complicated that there are no obvious bugs.
  -- C.A.R. Hoare (1980 Turing Award Lecture)


Reply via email to