cancel
Showing results for 
Search instead for 
Did you mean: 

importing function from javascript to keyword test

SOLVED
Highlighted
Valued Contributor

importing function from javascript to keyword test

Attempting to use 

require("MyCommonUnit").myFunction("someparameter")

within a keyword test statement such as Set Variable Value 

image.png

Seems like this should work, but returns an error:

Unable to evaluate the operation's "NewValue" parameter. Error: Error: Unable to find the specified module
 
In my script I have the following:
// [MyCommonUnit]
module.exports.myFunction = some_function; 

function some_function() {
//does stuff
return someValue; }
Anyone have an idea why this doesn't work or perhaps an alternate method to use?

''-Praise the sun and Give Kudos.''
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: importing function from javascript to keyword test

You can... if the function returns something, you can still use the Set Variable value after the script function call and set the "mode" of the set variable to "Last Operation Result".  This will take whatever the result is of the function call and assign it to the variable value.

SetVariableValue.png


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

5 REPLIES 5
Highlighted
Community Hero

Re: importing function from javascript to keyword test

So... you're trying to call a script function from a keyword test?

Have you looked at the following operation? https://support.smartbear.com/testcomplete/docs/keyword-testing/reference/test-actions/run-script-ro...

RunScriptRoutineOperation.png


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

Re: importing function from javascript to keyword test

More specifically, trying to return the result of that function.

I suppose I could just set a project level variable within that function, but I was hoping for a more dynamic solution.


''-Praise the sun and Give Kudos.''
Highlighted
Community Hero

Re: importing function from javascript to keyword test

You can... if the function returns something, you can still use the Set Variable value after the script function call and set the "mode" of the set variable to "Last Operation Result".  This will take whatever the result is of the function call and assign it to the variable value.

SetVariableValue.png


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

Re: importing function from javascript to keyword test

I definitely missed that option.

Exactly what I was looking for, thank you.


''-Praise the sun and Give Kudos.''
Highlighted
Community Hero

Re: importing function from javascript to keyword test

Hi,

 

Just an idea to check:

What if you wrap the Value with the eval/evaluate function?

I.e.:

evaluate(require(...).myFunction(...))

 

In case of success this will make it possible to use just one operation instead of two...

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
New Here?
Join us and watch the welcome video:
Top Kudoed Authors