-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIndependentNodeJs.java
29 lines (20 loc) · 1.01 KB
/
IndependentNodeJs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package examples;
import com.osiris.headlessbrowser.js.contexts.NodeContext;
import org.junit.jupiter.api.Test;
public class IndependentNodeJs {
@Test
void test() throws Exception {
// Installs Node.js into current working directory if needed
try (NodeContext ctx = new NodeContext()) { // Use another constructor for customization
// Easily install/update needed modules
ctx.npmInstall("async"); // name of Node module
// To be able to see the JavaScript code results.
// Otherwise you can also init NodeContext with debugOutput=System.out to achieve this.
ctx.onPrintLine(line -> System.out.println(line));
ctx.executeJavaScript("console.log('hello world!');");
// You can return JavaScript results too.
// Note that you must have a result variable in the provided JS Code for this to work!
String result = ctx.executeJSAndGetResult("var result = 'my JavaScript result!';");
}
}
}