Forum Discussion

CSL's avatar
CSL
Contributor
7 years ago

Do we need to export all the constants declared in another unit in order to use it in another unit?

Hello,

 

I created several constants and functions in another unit in TestComplete using javascript as the scripting language. In order for me to use them in another unit, do I need to import & export them one by one? Is there a better method?

 

i.e (Constant Units)

/columns (RSR)

const colSelect_RSR = 0

const colSts_RSR = 10

const colAsset_RSR = 4

const colRsn_RSR = 11

const colUnits_RSR = 24

const colInv_RSR = 25

const colCap_RSR = 26

const colSvGrp_RSR = 28

 

Do I need to export them to be able t use in another unit? Help please!

//Export constants

module.exports.colSelect_RSR= colSelect_RSR;

module.exports.colSts_RSR= colSts_RSR;

module.exports.colAsset_RSR= colAsset_RSR;

module.exports.colRsn_RSR= colRsn_RSR;

module.exports.colUnits_RSR= colUnits_RSR;

module.exports.colInv_RSR= colInv_RSR;

module.exports.colInv_RSR= colSelect_RSR;

module.exports.colCap_RSR= colCap_RSR;

module.exports.colSvGrp_RSR= colSvGrp_RSR;

}

 

6 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    An alternative to the import/export functionality of JavaScript is, in your unit in which you want to use the constants,  add

     

    //USEUNIT <unitname>

     

    at the top of the unit.  This will bring over everything... otherwise, yes... you would need to declare all the constants.

     

    An additional alternative would be, rather than having the constants sitting out there in the open, put them in some sort of object.  Something like

     

    var myContants = {colSelect_RSR: 0, colSts_RSR: 10 ... etc...}

     

    Then, just export myConstants and reference the different properties.

     

    A slightly more advanced alternative would be to wrap all your constants in read-only properties of a Script Extension RunTime object.

    • CSL's avatar
      CSL
      Contributor

      Hi Robert,

       

      Thank you for your quick answer. I am very new in Test Complete so this definitely helps me. Anyway, so when I use //UseUnit, I don't need to export/import them? Can I use //USEUNIT in javascript?

       

      Thanks,

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Correct, with //USEUNIT you don't need to the export/import stuff and it is available in JavaScript in TestComplete