Reviewers: kpreid2,

Description:
html capture is an approximation of the dom, basically .innerHTML.
I'm unsure how inaccurate it can be.

screenshot capture is pretty reliable when available.

logs capture is pretty useless right now, because webdriver doesn't
give us what we want.

Please review this at https://codereview.appspot.com/9830047/

Affected files:
  M     build.xml
  M     tests/com/google/caja/plugin/BrowserTestCase.java
  M     tests/com/google/caja/plugin/CatalogTestCase.java
  M     tests/com/google/caja/plugin/ModulesBrowserTest.java
  M     tests/com/google/caja/plugin/WebDriverHandle.java
  M     tests/com/google/caja/util/TestFlag.java


Index: build.xml
===================================================================
--- build.xml   (revision 5443)
+++ build.xml   (working copy)
@@ -324,6 +324,7 @@
       <formatter type="xml"/>
       <jvmarg line="${jvmarg}"/>
       <syspropertyset refid="test.propertyset"/>
+ <sysproperty key="test.capture.to" value="${reports}/test.${test.type}"/>
       <batchtest todir="${reports}/test.${test.type}">
         <fileset dir="${tests}">
           <and>
Index: tests/com/google/caja/plugin/BrowserTestCase.java
===================================================================
--- tests/com/google/caja/plugin/BrowserTestCase.java   (revision 5443)
+++ tests/com/google/caja/plugin/BrowserTestCase.java   (working copy)
@@ -114,8 +114,10 @@
     return errStream;
   }

-  protected String runBrowserTest(boolean isKnownFailure, String pageName,
-      String... params) throws Exception {
+  protected String runBrowserTest(
+      String label, boolean isKnownFailure, String pageName,
+      String... params) throws Exception
+  {
     int serverPort = TestFlag.SERVER_PORT.getInt(0);

if (TestFlag.DEBUG_BROWSER.truthy() || TestFlag.DEBUG_SERVER.truthy()) {
@@ -161,6 +163,7 @@
       result = driveBrowser(driver);
       passed = true;
     } finally {
+      wdh.captureResults(label);
       localServer.stop();
       // It's helpful for debugging to keep failed windows open.
       if (!passed && !isKnownFailure && !TestFlag.BROWSER_CLOSE.truthy()) {
Index: tests/com/google/caja/plugin/CatalogTestCase.java
===================================================================
--- tests/com/google/caja/plugin/CatalogTestCase.java   (revision 5443)
+++ tests/com/google/caja/plugin/CatalogTestCase.java   (working copy)
@@ -31,6 +31,6 @@
   }

   public void runTest() throws Exception {
-    runBrowserTest(entry.mayFail(), entry.getURL());
+    runBrowserTest(entry.getLabel(), entry.mayFail(), entry.getURL());
   }
 }
Index: tests/com/google/caja/plugin/ModulesBrowserTest.java
===================================================================
--- tests/com/google/caja/plugin/ModulesBrowserTest.java        (revision 5443)
+++ tests/com/google/caja/plugin/ModulesBrowserTest.java        (working copy)
@@ -25,7 +25,7 @@
   // BrowserTestCase is now JUnit 4-ish, so we use annotations.
   @Test
   public final void testModules() throws Exception {
-    runBrowserTest(false, "modules-test.html");
+    runBrowserTest("testModules", false, "modules-test.html");
   }

   @Override
Index: tests/com/google/caja/plugin/WebDriverHandle.java
===================================================================
--- tests/com/google/caja/plugin/WebDriverHandle.java   (revision 5443)
+++ tests/com/google/caja/plugin/WebDriverHandle.java   (working copy)
@@ -16,22 +16,31 @@

 import java.io.FileDescriptor;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.concurrent.TimeUnit;

 import org.openqa.selenium.Capabilities;
 import org.openqa.selenium.NoSuchWindowException;
+import org.openqa.selenium.OutputType;
+import org.openqa.selenium.TakesScreenshot;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebDriverException;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.chrome.ChromeOptions;
 import org.openqa.selenium.firefox.FirefoxDriver;
+import org.openqa.selenium.logging.LogEntries;
+import org.openqa.selenium.logging.LogEntry;
+import org.openqa.selenium.logging.LogType;
+import org.openqa.selenium.logging.Logs;
 import org.openqa.selenium.remote.DesiredCapabilities;
 import org.openqa.selenium.remote.RemoteWebDriver;
 import org.openqa.selenium.safari.SafariDriver;

+import com.google.caja.SomethingWidgyHappenedError;
 import com.google.caja.util.TestFlag;

 /**
@@ -170,20 +179,20 @@
         chromeOpts.setBinary(chromeBin);
       }
       String chromeArgs = TestFlag.CHROME_ARGS.getString(null);
-      if (chromeArgs!= null) {
+      if (chromeArgs != null) {
         String[] args = chromeArgs.split(";");
         chromeOpts.addArguments(args);
       }
       dc.setCapability(ChromeOptions.CAPABILITY, chromeOpts);
     }

-    String webdriver = TestFlag.WEBDRIVER_URL.getString("");
+    String url = TestFlag.WEBDRIVER_URL.getString("");

-    if (!"".equals(webdriver)) {
+    if (!"".equals(url)) {
       dc.setBrowserName(browserType);
       dc.setJavascriptEnabled(true);
       try {
-        return new RemoteWebDriver(new URL(webdriver), dc);
+        return new RemoteWebDriver(new URL(url), dc);
       } catch (MalformedURLException e) {
         throw new RuntimeException(e);
       }
@@ -199,4 +208,61 @@
           + browserType + "'");
     }
   }
+
+  public void captureResults(String name) {
+    if (driver == null) { return; }
+    String dir = TestFlag.CAPTURE_TO.getString("");
+    if ("".equals(dir)) { return; }
+    if (!dir.endsWith("/")) { dir = dir + "/"; }
+
+    // Try to capture the final html
+    String source = driver.getPageSource();
+    if (source != null) {
+      saveToFile(dir + name + ".capture.html", source);
+    }
+
+    // Try to capture a screenshot
+    if (driver instanceof TakesScreenshot) {
+      TakesScreenshot ss = (TakesScreenshot) driver;
+      byte[] bytes = ss.getScreenshotAs(OutputType.BYTES);
+      saveToFile(dir + name + ".capture.png", bytes);
+    }
+
+    // Try to capture logs
+    // This is currently not really useful.
+    // - ChromeDriver doesn't support log capture at all
+    // - FirefoxDriver gives you Error Console messages not Web Console
+    Logs logs = driver.manage().logs();
+    if (logs != null) {
+      if (logs.getAvailableLogTypes().contains(LogType.BROWSER)) {
+        LogEntries entries = logs.get(LogType.BROWSER);
+        if (entries != null) {
+          StringBuilder sb = new StringBuilder();
+          for (LogEntry e : entries) {
+            sb.append(e.toString() + "\n");
+          }
+          saveToFile(dir + name + ".capture.log", sb.toString());
+        }
+      }
+    }
+  }
+
+  private void saveToFile(String fileName, String str) {
+    try {
+      saveToFile(fileName, str.getBytes("UTF-8"));
+    } catch (UnsupportedEncodingException e) {
+      throw new SomethingWidgyHappenedError(e);
+    }
+  }
+
+  private void saveToFile(String fileName, byte[] bytes) {
+    FileOutputStream out = null;
+    try {
+      out = new FileOutputStream(fileName);
+      out.write(bytes);
+      out.close();
+    } catch (IOException e) {
+      throw new SomethingWidgyHappenedError(e);
+    }
+  }
 }
Index: tests/com/google/caja/util/TestFlag.java
===================================================================
--- tests/com/google/caja/util/TestFlag.java    (revision 5443)
+++ tests/com/google/caja/util/TestFlag.java    (working copy)
@@ -34,6 +34,8 @@
       "test.browser"),
   BROWSER_CLOSE(
       "test.browser.close"),
+  CAPTURE_TO(
+      "test.capture.to"),
   CHROME_ARGS(
       "test.chrome.args"),
   CHROME_BINARY(


--

--- You received this message because you are subscribed to the Google Groups "Google Caja Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to