Ask a Question

ScriptExtension replacement for ODT - Need some help

tristaanogre
Community Hero

ScriptExtension replacement for ODT - Need some help

 

Folks using DelphiScript for TestComplete automation have a disadvantage over the other script languages.  While JScript and JavaScript and Python allows you to declare classes and instantiate objects, DelphiScript doesn't have that ability.

 

To this end, in the past, folks have used the ODT object in TestComplete to be able to build classes and objects.  However, that object will be deprecated at some point.

 

To that end, I've started working at a simplistic replacement for the ODT object to be able to allow similar functionality but via a Script Extension.  Everything is working pretty well... except for one thing: adding methods to classes.  It works fine if the project in which you're calling the object is a JavaScript or JScript project.  But once I try using the object in a DelphiScript project, it doesn't work.  I don't get any errors, just that the code never actually executes.

 

Below is the code in the script extension:

 

var classes = {
    Declare: function (className) {
        classes[className] = new function newClass(){
        this.AddProperty = addProperty;
        this.AddMethod = addMethod;
        };
    },
    New: function (className) {return new classes[className].constructor()}};
    
function getclasses(){
    return classes;
}

function addProperty(propertyName, defaultValue) {
    if (defaultValue === undefined) {
        this.constructor.prototype[propertyName] = undefined;
    }
    else {
        this.constructor.prototype[propertyName] = defaultValue;
    }
}


function addMethod(methodName, stringProc){
    function newFunction() {
        Runner.CallMethod(stringProc)
    };
    this.constructor.prototype[methodName] = newFunction;
}   

To use this, once the extension is built, here's the way the code would look in a DelphiScript project:

 

//The unit name is Unit1
procedure test;
var myObject1;
begin
    OgreODT.Classes.Declare('test');
    OgreODT.Classes.test.AddProperty('prop1');
    OgreODT.Classes.test.AddProperty('prop2');
    OgreODT.Classes.test.AddMethod('logBlah', 'Unit1.testODT');
    myObject1 := OgreODT.Classes.New('test');
    myObject1.logBlah();
end;

procedure testODT;
begin
    Log.Message('it worked');
end;

Everything executes without error... just that the message never actually gets logged.  As mentioned, if I run similar code via JScript or JavaScript, everything works fine.

 

So... any assistance would be helpful in debugging what's going on here.  @HKosova.... you've been helpful in the past with these kinds of things.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
1 REPLY 1
tristaanogre
Community Hero

Re: ScriptExtension replacement for ODT - Need some help

Quick update: 

 

I tried a number of things before trying this.  The below code works with my object in DelphiScript

 

procedure test;
var myObject1;
begin
    OgreODT.Classes.Declare('test');
    OgreODT.Classes.test.AddProperty('prop1');
    OgreODT.Classes.test.AddProperty('prop2');
    OgreODT.Classes.test.AddMethod('logBlah', 'Unit1.testODT');
    myObject1 := OgreODT.Classes.New('test');
    aqObject.CallMethod(myObject1, 'logBlah');
end;

I'm really not sure why this makes a difference but, for whatever reason, this works.  It's a good short term work around while I try and figure other stuff out but, effectively, I have a way now, in DelphiScript, to declare and instantiate objects in code.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
cancel
Showing results for 
Search instead for 
Did you mean: