I'm starting with test automation in Selenium. I'm writing tests in JavaScript and I want to execute 'pure' JavaScript code in them, for example
alert('Hello World');
But I'm getting an error: "(node:12988) UnhandledPromiseRejectionWarning: ReferenceError: alert is not defined".
There's many tutorials how to execute JS code in Selenium using Python, Java, C# etc. but is it possible to execute JavaScript code in Selnium using JavaScript?
alert() function is only available for browser windows. use console.log('Hello World'); instead.
else if you explicitly want to use the alert and make your browser to show the alert popup you can run any javascript code in the current browser instance using JavaScriptExecutor below are sample codes if at all you want to do so.(not recommended as it doesn't add any value)
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("alert('Hello World');");
refer to the below answer too. Node.js Alert Causes Crash