Forum Discussion

binnu's avatar
binnu
Occasional Contributor
5 years ago
Solved

How to export a function from one unit to another unit in JavaScript.

Hi Everyone,

 

I'm trying to export a function from one unit to another unit, i used "module.exports.Test1 = Test1;" command in the unit1 to export Test1 function and used "var objin = require("Data1");" command to make it availble in unit2. i want to assign the Test1() funtion to a variable in the unit2. "let arrValues = objin.Test1();" i used this command to make it happen. but it's not working. can somebody help me export a funtion from external unit and how to use the function in another unit/funtion.

Data1 (unit1):

function Test1()
{
var driver = DDT.CSVDriver("C:\\Users\\75\\Desktop\\Data.csv");
var firstname_col = [];
// var lastname_col = [];



//let value1 = Project.Variables.Data.Value('FIRST NAME');
//let value2 = Project.Variables.Data.Value('LAST NAME');

for (i=0;i < driver.ColumnCount; i++)
{
firstname_col.push(driver.Value(i));

}
DDT.CloseDriver(driver.Name);

return firstname_col;
}

module.exports.Test1 = Test1;

 

Data2(Unit2):

var objin = require("Data1");
//USEUNIT Data1
function test()
{
var driver1 = DDT.CSVDriver("C:\\Users\\13175\\Desktop\\Data.csv");
let arrValues = objin.Test1();
//objin.Test1();

for (let l = 0;l < driver1.RowCount; l++)
{
let url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

Browsers.Item(btIExplorer).Run(url);
let page = Sys.Browser("*").Page("*");
aqUtils.Delay(1000);
let first_name = page.EvaluateXPath("//*[@id='fname']");
first_name[0].Click();

Sys.Keys(arrValues[l]);
let last_name = page.EvaluateXPath("//*[@id='lname']");
last_name[0].Click();

Sys.Keys(arrValues[l+1]);

// Call the function

var arr = page.EvaluateXPath("//*[@type='submit']");
arr[0].Click();

aqObject.CheckProperty(Aliases.browser.pageTryitEditorV36.frameIframeresult.textnode, "contentText", cmpEqual, "The server has processed your input and returned this answer.");

// DDT.CloseDriver(driver.Name);


page.Close();
}
}

 

Thanks,

Binnu

6 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Here's what I created:

    //Data1 Unit
    function Test1(){
        var testvalues = [];
        testvalues.push("value1")
        testvalues.push("value2")
        return testvalues
    }
    
    module.exports.Test1 = Test1
    
    //Data2 Unit
    var objin = require("Data1");
    
    
    function blah(){
        var arrayValues = objin.Test1()
        Log.Message(arrayValues[0]);
        Log.Message(arrayValues[1]);
    }

    This works just fine.  And looking through your code, I don't see anything that would keep this from working.  So, perhaps you could tell us, EXACTLY what's "not working".  Are you getting an error message? Is there something you want to show up that's not showing up?  Can you confirm that your project is, actually, using JavaScript and not JScript?

    • binnu's avatar
      binnu
      Occasional Contributor

      Hi tristaanogre ,

       

      Thanks for the reply, i don't get the error message or anything, testcomplete is passing the two tests and doing nothing. the log file doesn't show anything, it's just blank. Please see the screenshots for more details.

       

      Thanks,

      Binnu.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Well, first of all, you don't need to run "Data1.Test()" at all as a test item.  The way you have it written, it simply does data processing and returns an array.  That array is consumed in Data2 and called within Data2.

        As for Data2.... have you tried dropping in a breakpoint at the beginning of the code and seeing what happens?  Step through the code and see if there's a point where it's trying to do something and skpping over or erroring out.  You don't have any exception handling so it might be that you're running into a problem and it's just not logging it.