Class JavaScriptUtils

java.lang.Object
com.version1.webdriver.utils.JavaScriptUtils

public final class JavaScriptUtils extends Object
Utilities class providing support methods for executing JavaScript While this is straight forward, its less than obvious to newcomers. Feel free not to use this at all and just do it with WebDriver!
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    executeFile(org.openqa.selenium.WebDriver webDriver, String filename)
    Reads a file from the classpath (./src/test/resources/javascript/myfile.js) and executes it via the JavaScript executor.
    static void
    executeString(org.openqa.selenium.WebDriver webDriver, String javascript)
    Executes a given JavaScript script
    static void
    executeString(org.openqa.selenium.WebDriver webDriver, org.openqa.selenium.WebElement webElement, String javascript)
    Executes a given JavaScript script against a given WebElement

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • executeString

      public static void executeString(org.openqa.selenium.WebDriver webDriver, String javascript)
      Executes a given JavaScript script
      Parameters:
      webDriver - active WebDriver instance
      javascript - String representing javascript expression
    • executeString

      public static void executeString(org.openqa.selenium.WebDriver webDriver, org.openqa.selenium.WebElement webElement, String javascript)
      Executes a given JavaScript script against a given WebElement
      Parameters:
      webDriver - active WebDriver instance
      webElement - active WebElement, already located
      javascript - String representing javascript expression
    • executeFile

      public static void executeFile(org.openqa.selenium.WebDriver webDriver, String filename) throws IOException
      Reads a file from the classpath (./src/test/resources/javascript/myfile.js) and executes it via the JavaScript executor. This can make tests a little more readable as there's no inline JavaScript and horrible escape characters to content with.
      Parameters:
      webDriver - active WebDriver instance
      filename - File representing javascript expression
      Throws:
      IOException - if the file cannot be found