Class 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!
    • Constructor Summary

      Constructors 
      Constructor Description
      JavaScriptUtils()  
    • 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
    • Constructor Detail

      • JavaScriptUtils

        public JavaScriptUtils()
    • Method Detail

      • 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