cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Occasional Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

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

Ah... 

 

So, rather than a for loop to iterate through your driver, a better way is to do a "while" loop because it is an iteratiive collection that you need to move a point for.   

 

In fact, looking at your code, I'm not sure why you need the two functions.

 

See the example at https://support.smartbear.com/testcomplete/docs/reference/program-objects/ddt/exceldriver.html on how to use a driver.


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

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

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

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?


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
Highlighted
Occasional Contributor

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

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.

Highlighted
Community Hero

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

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.


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
Occasional Contributor

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

@tristaanogre ,

 

Found the issue, for (let l = 0;l < driver1.RowCount; l++) the loop is not working if i use RowCount, so i upated RowCount to Column Count, it started reading the file. My question is if i use only CoulmnCount (2) in the loop it's just using the first name(Kevin), last name(Gibson) in the first iteration and in the second iteration it's using Gibson as first name and the null as second name since there are only two columns. is there any keyword i can use to make it loop through all the rows?? The data file is attached.

 

Thanks,

Binnu.

Highlighted
Community Hero

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

Ah... 

 

So, rather than a for loop to iterate through your driver, a better way is to do a "while" loop because it is an iteratiive collection that you need to move a point for.   

 

In fact, looking at your code, I'm not sure why you need the two functions.

 

See the example at https://support.smartbear.com/testcomplete/docs/reference/program-objects/ddt/exceldriver.html on how to use a driver.


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

View solution in original post

Highlighted
Occasional Contributor

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

@tristaanogre ,

 

It worked, thanks alot!!

 

Binnu.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors