1. Kostya Marchenko
  2. JavaScript Test Rig

Commits

Kostya Marchenko  committed 7709336

Implemented test rig for JS testing in page context.
Unit tests are also implemented.

  • Participants
  • Parent commits 61b40ae
  • Branches default

Comments (0)

Files changed (7)

File src/main/java/sandbox/JsTestHelper.java

View file
  • Ignore whitespace
      */
     public static void injectJavaScripts(WebDriver driver, ArrayList<String> javaScripts) throws Exception {
         //Html Unit not supported yet for multiple injections
+        //TODO: Implement support for multiple injection of JS for HtmlUnitDriver
         if (driver instanceof HtmlUnitDriver) {
             throw new UnsupportedOperationException("Can't inject multiple JavaScripts when using HtmlUnit driver");
         }

File src/main/java/sandbox/JsTestRig.java

View file
  • Ignore whitespace
      * @throws Exception if test rig can not be created
      */
     public static JavascriptExecutor getIsolatedRig(String scriptUnderTest, JsTestHelper.BrowserType browserType) throws Exception {
-        driver = JsTestHelper.getDriver(browserType);
-        driver.get("about:blank");
-
-        try {
-            JsTestHelper.injectJavaScript(driver, scriptUnderTest);
-        } catch (Exception e) {
-            throw new Exception("Can't create test rig: " + e.toString());
-        }
-        return (JavascriptExecutor) driver;
+        return getRig("about:blank", scriptUnderTest, browserType);
     }
 
     /**
      * @throws Exception if test rig can not be created
      */
     public static JavascriptExecutor getIsolatedRig(ArrayList<String> scriptsUnderTest, JsTestHelper.BrowserType browserType) throws Exception {
+        return getRig("about:blank", scriptsUnderTest, browserType);
+    }
 
+    /**
+     * Creates test rig for JavaScript testing in page context. Can be used to test JS which manipulate DOM
+     *
+     * @param pageUrl         String which represents URL to the page in which context JS should be tested
+     * @param scriptUnderTest String which represents JavaScript which should be tested
+     * @param browserType     Type of the browser in which to test JavaScript
+     * @return JavascriptExecutor object which provides ability to execute JavaScript in the same context as script under test
+     * @throws Exception if test rig can not be created
+     */
+    public static JavascriptExecutor getRig(String pageUrl, String scriptUnderTest, JsTestHelper.BrowserType browserType) throws Exception {
         driver = JsTestHelper.getDriver(browserType);
-        driver.get("about:blank");
+        driver.get(pageUrl);
+
+        try {
+            JsTestHelper.injectJavaScript(driver, scriptUnderTest);
+
+        } catch (Exception e) {
+            throw new Exception("Can't create test rig: " + e.toString());
+        }
+        return (JavascriptExecutor) driver;
+
+    }
+
+    /**
+     * Creates test rig for JavaScript testing in page context. Can be used to test multiple JavaScripts which manipulate DOM
+     *
+     * @param pageUrl          String which represents URL to the page in which context JS should be tested
+     * @param scriptsUnderTest ArrayList of JavaScripts to be tested. Scripts will be loaded in the same order as they are in array.
+     * @param browserType      Type of the browser in which to test JavaScript
+     * @return JavascriptExecutor object which provides ability to execute JavaScript in the same context as script under test
+     * @throws Exception if test rig can not be created
+     */
+    public static JavascriptExecutor getRig(String pageUrl, ArrayList<String> scriptsUnderTest, JsTestHelper.BrowserType browserType) throws Exception {
+        driver = JsTestHelper.getDriver(browserType);
+        driver.get(pageUrl);
 
         try {
             JsTestHelper.injectJavaScripts(driver, scriptsUnderTest);
             throw new Exception("Can't create test rig: " + e.toString());
         }
         return (JavascriptExecutor) driver;
+
     }
 
-
     /**
      * Shutdown test rig
      */

File src/test/java/sandbox/JsTestRigTest.java

View file
  • Ignore whitespace
 package sandbox;
 
+import org.apache.commons.io.IOUtils;
 import org.junit.After;
 import org.junit.Test;
+import org.openqa.selenium.By;
 import org.openqa.selenium.JavascriptExecutor;
+import org.openqa.selenium.WebDriver;
 
 import java.util.ArrayList;
 
         assertThat(resultTwo, equalTo("Hello, Kostya!"));
     }
 
+
+    @Test
+    public void testRigWithSingleScriptInPageContext() throws Exception {
+        String scriptUnderTest = IOUtils.toString(getClass().getResourceAsStream("ScriptUnderTestOne.js"));
+        String pageUrl = getClass().getResource("SamplePage.html").toString();
+        JavascriptExecutor js = JsTestRig.getRig(pageUrl, scriptUnderTest, JsTestHelper.BrowserType.FIREFOX);
+
+        js.executeScript("createDiv();");    //driver.findElement(By.id("clickMeButton")).click();
+
+        WebDriver driver = (WebDriver) js;
+        String divText = driver.findElement(By.id("sampleDiv")).getText();
+        assertThat(divText, equalTo("I'm generated by JavaScript"));
+    }
+
+    @Test
+    public void testRigWithMultipleScriptsInPageContext() throws Exception {
+        String scriptUnderTestOne = IOUtils.toString(getClass().getResourceAsStream("ScriptUnderTestOne.js"));
+        String scriptUnderTestTwo = IOUtils.toString(getClass().getResourceAsStream("ScriptUnderTestTwo.js"));
+        ArrayList<String> scriptsUnderTest = newArrayList(scriptUnderTestOne, scriptUnderTestTwo);
+
+        String pageUrl = getClass().getResource("SamplePage.html").toString();
+        JavascriptExecutor js = JsTestRig.getRig(pageUrl, scriptsUnderTest, JsTestHelper.BrowserType.FIREFOX);
+
+        js.executeScript("createDiv();");    //driver.findElement(By.id("clickMeButton")).click();
+        js.executeScript("createAnotherDiv();");
+
+        WebDriver driver = (WebDriver) js;
+        String divText = driver.findElement(By.id("sampleDiv")).getText();
+        assertThat(divText, equalTo("I'm generated by JavaScript"));
+
+        divText = driver.findElement(By.id("sampleDivTwo")).getText();
+        assertThat(divText, equalTo("I'm also generated by JavaScript"));
+    }
+
     @After
     public void tearDown() {
         JsTestRig.tearDown();

File src/test/resources/sandbox/SamplePage.html

View file
  • Ignore whitespace
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+        "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+    <title>Sample Page</title>
+</head>
+<body>
+    <h1>Sample Page</h1>
+
+    <p>Sample page, used as context to test JavaScript which modifies DOM.</p>
+
+    <input id="clickMeButton" type="button" value="Click Me!" onclick="createDiv()">
+
+</body>
+</html>

File src/test/resources/sandbox/ScriptUnderTest.js

  • Ignore whitespace
-function HelloWorld() {
-    return "Hello, World";
-}

File src/test/resources/sandbox/ScriptUnderTestOne.js

View file
  • Ignore whitespace
+function createDiv() {
+    var divTag = document.createElement("div");
+    divTag.id = "sampleDiv";
+    divTag.innerHTML = "I'm generated by JavaScript";
+    document.body.appendChild(divTag);
+}

File src/test/resources/sandbox/ScriptUnderTestTwo.js

View file
  • Ignore whitespace
+function createAnotherDiv() {
+    var divTag = document.createElement("div");
+    divTag.id = "sampleDivTwo";
+    divTag.innerHTML = "I'm also generated by JavaScript";
+    document.body.appendChild(divTag);
+}