Class JavaScriptUtils

java.lang.Object
uk.co.evoco.webdriver.utils.JavaScriptUtils

public final class JavaScriptUtils
extends java.lang.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, java.lang.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, java.lang.String javascript)
    Executes a given JavaScript script
    static void executeString​(org.openqa.selenium.WebDriver webDriver, org.openqa.selenium.WebElement webElement, java.lang.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
  • Constructor Details

  • Method Details

    • executeString

      public static void executeString​(org.openqa.selenium.WebDriver webDriver, java.lang.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, java.lang.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, java.lang.String filename) throws java.io.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:
      java.io.IOException - if the file cannot be found