I've encountered an issue while running a JavaScript file using Java with Selenium in my application. When I execute the JavaScript file with JavascriptExecutor after logging in, I'm only getting a null return instead of a valid one.
Below is a snippet from my code:
public static void runJavascriptFile(ChromeDriver driver) throws Exception {
System.out.println("Start");
Object js = driver.executeScript(TestHelper.getTextFromFile("demo-client/scripts/get-test.js"));
System.out.println(js);
System.out.println("End");
}
The content of my JavaScript file is as follows:
async function getPeople() {
let response = await fetch("https://swapi.dev/api/people/1/", { method: "GET" })
console.log("This is from my Javascript File from Java.")
return response.status;
}
let result = await getPeople();
console.log("result", result);
In the Chrome browser console, I can see the return value from the JavaScript file, but not in my Java IDE: BrowserConsoleXJavaConsole
If anyone has a different approach that could resolve this issue, your help would be greatly appreciated!