Revision: 5446
Author:   [email protected]
Date:     Wed Jun 12 12:50:57 2013
Log:      capture useful info after browser tests
https://codereview.appspot.com/9830047

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.

R=kpreid2


http://code.google.com/p/google-caja/source/detail?r=5446

Modified:
 /trunk/build.xml
 /trunk/tests/com/google/caja/plugin/BrowserTestCase.java
 /trunk/tests/com/google/caja/plugin/CatalogTestCase.java
 /trunk/tests/com/google/caja/plugin/ModulesBrowserTest.java
 /trunk/tests/com/google/caja/plugin/WebDriverHandle.java
 /trunk/tests/com/google/caja/util/TestFlag.java

=======================================
--- /trunk/build.xml    Fri Jun  7 17:22:43 2013
+++ /trunk/build.xml    Wed Jun 12 12:50:57 2013
@@ -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>
=======================================
--- /trunk/tests/com/google/caja/plugin/BrowserTestCase.java Fri May 17 17:46:31 2013 +++ /trunk/tests/com/google/caja/plugin/BrowserTestCase.java Wed Jun 12 12:50:57 2013
@@ -114,7 +114,8 @@
     return errStream;
   }

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

@@ -161,6 +162,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()) {
=======================================
--- /trunk/tests/com/google/caja/plugin/CatalogTestCase.java Fri May 17 17:46:31 2013 +++ /trunk/tests/com/google/caja/plugin/CatalogTestCase.java Wed Jun 12 12:50:57 2013
@@ -31,6 +31,6 @@
   }

   public void runTest() throws Exception {
-    runBrowserTest(entry.mayFail(), entry.getURL());
+    runBrowserTest(entry.getLabel(), entry.mayFail(), entry.getURL());
   }
 }
=======================================
--- /trunk/tests/com/google/caja/plugin/ModulesBrowserTest.java Tue May 14 14:13:11 2013 +++ /trunk/tests/com/google/caja/plugin/ModulesBrowserTest.java Wed Jun 12 12:50:57 2013
@@ -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
=======================================
--- /trunk/tests/com/google/caja/plugin/WebDriverHandle.java Wed May 29 22:56:37 2013 +++ /trunk/tests/com/google/caja/plugin/WebDriverHandle.java Wed Jun 12 12:50:57 2013
@@ -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,65 @@
           + 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;
+      try {
+        byte[] bytes = ss.getScreenshotAs(OutputType.BYTES);
+        saveToFile(dir + name + ".capture.png", bytes);
+      } catch (WebDriverException e) {
+        log("screenshot failed: " + e);
+      }
+    }
+
+    // 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);
+    }
+  }
 }
=======================================
--- /trunk/tests/com/google/caja/util/TestFlag.java     Wed May 29 22:56:37 2013
+++ /trunk/tests/com/google/caja/util/TestFlag.java     Wed Jun 12 12:50:57 2013
@@ -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