// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote.server.handler;

import static org.openqa.selenium.OutputType.BASE64;

import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.server.Session;

public class CaptureScreenshot extends WebDriverHandler<String> {

  public CaptureScreenshot(Session session) {
    super(session);
  }

  @Override
  public String call() throws Exception {
    WebDriver driver = getUnwrappedDriver();

    return ((TakesScreenshot) driver).getScreenshotAs(BASE64);
  }

  @Override
  public String toString() {
    return "[take screenshot]";
  }
}

@@ -895,21 +895,56 @@
 utType.B
-ASE64
+YTES;%0A%0Aimport com.google.common.io.Files
 ;%0A%0Aimpor
@@ -1020,16 +1020,114 @@
 Driver;%0A
+import org.openqa.selenium.WebDriverException;%0Aimport org.openqa.selenium.internal.Base64Encoder;%0A
 import o
@@ -1169,16 +1169,66 @@
 ssion;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A%0A
 public c
@@ -1286,16 +1286,41 @@
 ing%3E %7B%0A%0A
+  private File dumpDir;%0A%0A
   public
@@ -1376,16 +1376,189 @@
 ession);
+%0A%0A    String path = System.getProperty(%22webdriver.saveScreenshots%22);%0A    if (path != null && !path.isEmpty()) %7B%0A      dumpDir = new File(path);%0A      dumpDir.mkdirs();%0A    %7D
 %0A  %7D%0A%0A  
@@ -1659,22 +1659,28 @@
 );%0A%0A    
-return
+byte%5B%5D png =
  ((Takes
@@ -1720,93 +1720,504 @@
 As(B
-ASE64);%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return %22%5Btake screenshot%5D%22;
+YTES);%0A%0A    if (dumpDir != null) %7B%0A      save(dumpDir, png);%0A    %7D%0A    return new Base64Encoder().encode(png);%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return %22%5Btake screenshot%5D%22;%0A  %7D%0A%0A  private File save(File folder, byte%5B%5D data) %7B%0A    try %7B%0A      String filename = System.currentTimeMillis() + %22_screenshot.png%22;%0A%0A      File file = new File(folder, filename);%0A      Files.write(data, file);%0A%0A      return file;%0A    %7D catch (IOException e) %7B%0A      throw new WebDriverException(e);%0A    %7D
 %0A  %7D

Add an option to log all screenshots server side