Did you know you can execute JavaScript code inside a Selenium test? Here is how you can accomplish this using the WebDriver
object:
display current date on browser
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class displayDate { public static void main(String[] args) throws InterruptedException { WebDriver driver; driver = new FirefoxDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.write(Date());"); } }
display current date on Alert box
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class displayDate { public static void main(String[] args) throws InterruptedException { WebDriver driver; driver = new FirefoxDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("alert(Date());"); } }