How to inject javascript function in katalon

Hello,

I will want to execute my little javascript function below, in order to call it KS.

The purpose of this function is to create a mousedown event on a DOM element that does not own it.

I read that @Russ_thomas asked us that it was possible to read create a javascript file and read it later, but I can not :frowning:
someone can help me please.

1- what import can i do, because my jsHelper variable is not recognized when not putting the type File

function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent (‘MouseEvents’);
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);

hello,

not sure but maybe this will help you

TESTCASE
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

def jsPath = System.getProperty("user.dir")+"/Include/JavaScript/Jsfunctions.js"

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
try {
  engine.eval(new FileReader(jsPath));
  Invocable invocable = (Invocable) engine;
  Object result;
  result = invocable.invokeFunction("display", 'helloWorld');
  System.out.println(result);
  System.out.println(result.getClass());
  } catch (FileNotFoundException | NoSuchMethodException | ScriptException e) {
	e.printStackTrace();
	}

JAVASCRIPT FILE
var display = function(name) {
	print("Hello, I am a Javascript display function",name);
	return ("display function return",name)
}
1 Like

Thank you Timo

But the function display that you share me is not a javascript function, it seems to me that it is Java.
the javascript print () should show me a popup box to print my page, but here I have a return of the string “Hello, I am a Javascipt display function”.

I finally succeeded by doing this:

// 1- Assigning the file path that contains my javascript function
def jsPath = System.getProperty(“user.dir”) + “\Include\scripts\javascript\All_javascript_function.js”

// 2- read file encode bytes
def fileBytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(jsPath))

// 3- Construction
String data = new String(fileBytes)

// 4- Declaration, we must to concatened the javaScript parameter, this parameter will reffer to my javaScript parmeter function in the file.
String codeToExecute = data + “triggerMouseEvent(document.getElementById(‘select-analyse’).getElementsByClassName(‘ng-input’)[0], ‘mousedown’);”

// 5- Executing the function of my file thanks to WebUI.executeJavaScript()
WebUI.executeJavaScript(codeToExecute, null)

As a reminder:

The purpose of my function is to create a JavaScript Mousdown event in an angular ng-select tag that does not have an event click, or the mousdown. So difficult to access this element, without this little funcion below:
which takes in parameters:
1 set the node of the DOM,
2 set the MouseDown event.

var triggerMouseEvent = function (node, eventType) {

   var clickEvent = document.createEvent ('MouseEvents');

   clickEvent.initEvent (eventType, true, true);

   node.dispatchEvent (clickEvent);
}
1 Like