// Render the current page as a PNG or JPEG image, writing it to the given filename or writable
// destination object.
//
// If the filename is the string `"temporary"`, a file will be created in the system's
// temporary area (e.g.: `/tmp`) and the screenshot will be written there.  It is the caller's
// responsibility to remove the temporary file if desired.  The temporary file path is available in
// the return object's `path` parameter.
Render the current page as a PNG or JPEG image, writing it to the given filename or writable
destination object.

If the filename is the string `"temporary"`, a file will be created in the system's
temporary area  and the screenshot will be written there.  It is the caller's
responsibility to remove the temporary file if desired.  The temporary file path is available in
the return object's `path` parameter.
// Render the current page as a PNG or JPEG image writing it to the given filename or writable // destination object . // // If the filename is the string ` " temporary " ` a file will be created in the system ' s // temporary area ( e . g . : ` / tmp ` ) and the screenshot will be written there . It is the caller ' s // responsibility to remove the temporary file if desired . The temporary file path is available in // the return object ' s ` path ` parameter .
func (self *Commands) Screenshot(destination interface{}, args *ScreenshotArgs) (*ScreenshotResponse, error) {
	if args == nil {
		args = &ScreenshotArgs{}
	}
	defaults.SetDefaults(args)
	response := &ScreenshotResponse{}
	var writer io.Writer
	if destination != nil {
		if filename, ok := destination.(string); ok {
			if newPath, w, ok := self.browser.GetWriterForPath(filename); ok {
				writer = w
				response.Path = newPath
			} else if filename == `temporary` {
				if temp, err := ioutil.TempFile(``, ``); err == nil {
					writer = temp
					response.Path = temp.Name()
				} else {
					return nil, err
				}
			} else if file, err := os.Create(filename); err == nil {
				writer = file
				response.Path = filename
			} else {
				return nil, err
			}
		} else if w, ok := destination.(io.Writer); ok {
			writer = w
		} else {
			return nil, fmt.Errorf("Unsupported destination %T; expected string or io.Writer", destination)
		}
	}
	if writer == nil {
		return response, fmt.Errorf("A destination for the screenshot must be specified")
	}
	if args.Width == 0 || args.Height == 0 {
		if pageWidth, pageHeight, err := self.browser.Tab().PageSize(); err == nil {
			if args.Width == 0 {
				args.Width = int(pageWidth)
			}
			if args.Height == 0 {
				args.Height = int(pageHeight)
			}
		}
	}
	if err := self.screenshotPopulateArgsFromElement(args, response); err != nil {
		return response, err
	}
	screenshot := map[string]interface{}{
		`format`:      args.Format,
		`fromSurface`: false,
	}
	if args.Format == `jpeg` && args.Quality > 0 {
		screenshot[`quality`] = args.Quality
	}
	if args.X > 0 || args.Y > 0 {
		screenshot[`clip`] = map[string]interface{}{
			`width`:  args.Width,
			`height`: args.Height,
			`x`:      args.X,
			`y`:      args.Y,
			`scale`:  1.0,
		}
	} else if args.Autoresize && args.Width > 0 && args.Height > 0 {
		if _, err := self.browser.Tab().RPC(`Emulation`, `setDeviceMetricsOverride`, map[string]interface{}{
			`width`:             args.Width,
			`height`:            args.Height,
			`deviceScaleFactor`: 1.0,
			`mobile`:            false,
		}); err != nil {
			return response, fmt.Errorf("Failed to resize screen: %v", err)
		}
	}
	if reply, err := self.browser.Tab().RPC(`Page`, `captureScreenshot`, screenshot); err == nil {
		defer func() {
			self.browser.Tab().RPC(`Emulation`, `clearDeviceMetricsOverride`, nil)
			self.browser.Tab().RPC(`Emulation`, `resetPageScaleFactor`, nil)
		}()
		response.Width = args.Width
		response.Height = args.Height
		response.X = args.X
		response.Y = args.Y
		if data := reply.R().String(`data`); data != `` {
			decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(data))
			if n, err := io.Copy(writer, decoder); err == nil {
				response.Size = n
				if args.Autoclose {
					if closer, ok := writer.(io.Closer); ok {
						if err := closer.Close(); err == nil {
							log.Debugf("Destination file closed.")
						} else {
							return response, err
						}
					}
				}
				return response, nil
			} else {
				return response, fmt.Errorf("Error decoding screenshot response: %v", err)
			}
		} else {
			return response, fmt.Errorf("Empty response received while capturing screenshot")
		}
	} else {
		return response, err
	}
}
func ( self * Commands ) Screenshot ( destination interface { } , args * ScreenshotArgs ) ( * ScreenshotResponse , error ) { if args == nil { args = & ScreenshotArgs { } } defaults . SetDefaults ( args ) response := & ScreenshotResponse { } var writer io . Writer if destination != nil { if filename , ok := destination . ( string ) ; ok { if newPath , w , ok := self . browser . GetWriterForPath ( filename ) ; ok { writer = w response . Path = newPath } else if filename == `temporary` { if temp , err := ioutil . TempFile ( `` , `` ) ; err == nil { writer = temp response . Path = temp . Name ( ) } else { return nil , err } } else if file , err := os . Create ( filename ) ; err == nil { writer = file response . Path = filename } else { return nil , err } } else if w , ok := destination . ( io . Writer ) ; ok { writer = w } else { return nil , fmt . Errorf ( " " , destination ) } } if writer == nil { return response , fmt . Errorf ( " " ) } if args . Width == 0 || args . Height == 0 { if pageWidth , pageHeight , err := self . browser . Tab ( ) . PageSize ( ) ; err == nil { if args . Width == 0 { args . Width = int ( pageWidth ) } if args . Height == 0 { args . Height = int ( pageHeight ) } } } if err := self . screenshotPopulateArgsFromElement ( args , response ) ; err != nil { return response , err } screenshot := map [ string ] interface { } { `format` : args . Format , `fromSurface` : false , } if args . Format == `jpeg` && args . Quality > 0 { screenshot [ `quality` ] = args . Quality } if args . X > 0 || args . Y > 0 { screenshot [ `clip` ] = map [ string ] interface { } { `width` : args . Width , `height` : args . Height , `x` : args . X , `y` : args . Y , `scale` : 1.0 , } } else if args . Autoresize && args . Width > 0 && args . Height > 0 { if _ , err := self . browser . Tab ( ) . RPC ( `Emulation` , `setDeviceMetricsOverride` , map [ string ] interface { } { `width` : args . Width , `height` : args . Height , `deviceScaleFactor` : 1.0 , `mobile` : false , } ) ; err != nil { return response , fmt . Errorf ( " " , err ) } } if reply , err := self . browser . Tab ( ) . RPC ( `Page` , `captureScreenshot` , screenshot ) ; err == nil { defer func ( ) { self . browser . Tab ( ) . RPC ( `Emulation` , `clearDeviceMetricsOverride` , nil ) self . browser . Tab ( ) . RPC ( `Emulation` , `resetPageScaleFactor` , nil ) } ( ) response . Width = args . Width response . Height = args . Height response . X = args . X response . Y = args . Y if data := reply . R ( ) . String ( `data` ) ; data != `` { decoder := base64 . NewDecoder ( base64 . StdEncoding , bytes . NewBufferString ( data ) ) if n , err := io . Copy ( writer , decoder ) ; err == nil { response . Size = n if args . Autoclose { if closer , ok := writer . ( io . Closer ) ; ok { if err := closer . Close ( ) ; err == nil { log . Debugf ( " " ) } else { return response , err } } } return response , nil } else { return response , fmt . Errorf ( " " , err ) } } else { return response , fmt . Errorf ( " " ) } } else { return response , err } }
Render the current page as a PNG or JPEG image, writing it to the given filename or writable
destination object.
Render the current page as a PNG or JPEG image writing it to the given filename or writable destination object .
// if one or both of the dimensions are not explicitly given, fill them in from the current // page dimensions (scroll width/height) // start building screenshot RPC request // set quality for format="jpeg" // setup clipping region to the given X/Y if specified // resize viewport to given width and height // take the screenshot // decode the response from base64, write the data to destination