github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/proxy/driverhub/googlescreenshot/google_screenshot.go (about)

     1  // Copyright 2017 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package googlescreenshot includes a handler for an advanced screenshot endpoint at POST google/screenshot.
    16  package googlescreenshot
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"encoding/base64"
    22  	"encoding/json"
    23  	"fmt"
    24  	"image"
    25  	"image/png"
    26  	"log"
    27  	"net/http"
    28  
    29  	"github.com/bazelbuild/rules_webtesting/go/cropper"
    30  	"github.com/bazelbuild/rules_webtesting/go/metadata/capabilities"
    31  	"github.com/bazelbuild/rules_webtesting/go/webdriver"
    32  	"github.com/bazelbuild/rules_webtesting/go/wtl/proxy/driverhub"
    33  )
    34  
    35  type request struct {
    36  	Element map[string]interface{}
    37  	Exclude []map[string]interface{}
    38  }
    39  
    40  // ProviderFunc provides a handler for an advanced screenshot endpoint at POST google/screenshot.
    41  func ProviderFunc(session *driverhub.WebDriverSession, _ *capabilities.Capabilities, base driverhub.HandlerFunc) (driverhub.HandlerFunc, bool) {
    42  	return func(ctx context.Context, rq driverhub.Request) (driverhub.Response, error) {
    43  		if rq.Method != http.MethodPost || len(rq.Path) != 2 || rq.Path[0] != "google" || rq.Path[1] != "screenshot" {
    44  			return base(ctx, rq)
    45  		}
    46  
    47  		r := request{}
    48  
    49  		if err := json.Unmarshal(rq.Body, &r); err != nil {
    50  			// TODO(DrMarcII): better error message?
    51  			return driverhub.ResponseFromError(err)
    52  		}
    53  
    54  		img, err := captureScreenshot(ctx, session.WebDriver, r)
    55  		if err != nil {
    56  			// TODO(DrMarcII): better error message?
    57  			return driverhub.ResponseFromError(err)
    58  		}
    59  
    60  		return createResponse(img)
    61  	}, true
    62  }
    63  
    64  func captureScreenshot(ctx context.Context, driver webdriver.WebDriver, r request) (image.Image, error) {
    65  	el, _ := driver.ElementFromMap(r.Element)
    66  
    67  	if el != nil {
    68  		if err := el.ScrollIntoView(ctx); err != nil {
    69  			return nil, err
    70  		}
    71  	}
    72  
    73  	// TODO(DrMarcII): stabilization to ensure that we have finished scrolling?
    74  	img, err := driver.Screenshot(ctx)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	for _, e := range r.Exclude {
    80  		exclude, err := driver.ElementFromMap(e)
    81  		if err != nil {
    82  			log.Print(err)
    83  			continue
    84  		}
    85  
    86  		bounds, err := exclude.Bounds(ctx)
    87  		if err != nil {
    88  			log.Print(err)
    89  			continue
    90  		}
    91  
    92  		i, err := cropper.Blackout(img, bounds)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		img = i
    97  	}
    98  
    99  	if el != nil {
   100  		bounds, err := el.Bounds(ctx)
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  
   105  		return cropper.Crop(img, bounds)
   106  	}
   107  	return img, nil
   108  }
   109  
   110  func createResponse(img image.Image) (driverhub.Response, error) {
   111  	buffer := &bytes.Buffer{}
   112  	b64 := base64.NewEncoder(base64.StdEncoding, buffer)
   113  	defer b64.Close()
   114  
   115  	if err := png.Encode(b64, img); err != nil {
   116  		return driverhub.ResponseFromError(err)
   117  	}
   118  
   119  	return driverhub.Response{
   120  		Status: http.StatusOK,
   121  		Body:   []byte(fmt.Sprintf(`{"status": 0, "value": %q}`, buffer.String())),
   122  	}, nil
   123  }