github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/proxy/driverhub/scripttimeout/script_timeout.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 scripttimeout translates calls to set script timeout into calls
    16  // on the WebDriver object so it can record the last set script timeout.
    17  package scripttimeout
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"net/http"
    23  	"time"
    24  
    25  	"github.com/bazelbuild/rules_webtesting/go/metadata/capabilities"
    26  	"github.com/bazelbuild/rules_webtesting/go/wtl/proxy/driverhub"
    27  )
    28  
    29  // ProviderFunc provides a handler for set script timeout commands.
    30  func ProviderFunc(session *driverhub.WebDriverSession, _ *capabilities.Capabilities, base driverhub.HandlerFunc) (driverhub.HandlerFunc, bool) {
    31  	return func(ctx context.Context, rq driverhub.Request) (driverhub.Response, error) {
    32  
    33  		if rq.Method == http.MethodPost && len(rq.Path) == 1 && rq.Path[0] == "timeouts" {
    34  			var request map[string]interface{}
    35  
    36  			if err := json.Unmarshal(rq.Body, &request); err != nil {
    37  				return base(ctx, rq)
    38  			}
    39  
    40  			if timeout, ok := request["script"].(int); ok {
    41  				if err := session.WebDriver.SetScriptTimeout(ctx, time.Duration(timeout)*time.Millisecond); err == nil {
    42  					delete(request, "script")
    43  				}
    44  			}
    45  
    46  			if timeout, ok := request["script"].(float64); ok {
    47  				if err := session.WebDriver.SetScriptTimeout(ctx, time.Duration(timeout)*time.Millisecond); err == nil {
    48  					delete(request, "script")
    49  				}
    50  			}
    51  
    52  			if t, ok := request["type"].(string); ok && t == "script" {
    53  				if timeout, ok := request["ms"].(int); ok {
    54  					if err := session.WebDriver.SetScriptTimeout(ctx, time.Duration(timeout)*time.Millisecond); err == nil {
    55  						delete(request, "ms")
    56  						delete(request, "type")
    57  					}
    58  				}
    59  
    60  				if timeout, ok := request["ms"].(float64); ok {
    61  					if err := session.WebDriver.SetScriptTimeout(ctx, time.Duration(timeout)*time.Millisecond); err == nil {
    62  						delete(request, "ms")
    63  						delete(request, "type")
    64  					}
    65  				}
    66  			}
    67  
    68  			if len(request) == 0 {
    69  				return driverhub.Response{
    70  					Status: http.StatusOK,
    71  					Body:   []byte(`{"status": 0}`),
    72  				}, nil
    73  			}
    74  
    75  			body, err := json.Marshal(request)
    76  			if err == nil {
    77  				rq.Body = body
    78  			}
    79  		}
    80  
    81  		if rq.Method == http.MethodPost && len(rq.Path) == 2 && rq.Path[0] == "timeouts" && rq.Path[1] == "async_script" {
    82  			var request map[string]interface{}
    83  
    84  			if err := json.Unmarshal(rq.Body, &request); err != nil {
    85  				return base(ctx, rq)
    86  			}
    87  
    88  			if timeout, ok := request["ms"].(int); ok {
    89  				if err := session.WebDriver.SetScriptTimeout(ctx, time.Duration(timeout)*time.Millisecond); err == nil {
    90  					return driverhub.Response{
    91  						Status: http.StatusOK,
    92  						Body:   []byte(`{"status": 0}`),
    93  					}, nil
    94  				}
    95  			}
    96  
    97  			if timeout, ok := request["ms"].(float64); ok {
    98  				if err := session.WebDriver.SetScriptTimeout(ctx, time.Duration(timeout)*time.Millisecond); err == nil {
    99  					return driverhub.Response{
   100  						Status: http.StatusOK,
   101  						Body:   []byte(`{"status": 0}`),
   102  					}, nil
   103  				}
   104  			}
   105  		}
   106  
   107  		return base(ctx, rq)
   108  	}, true
   109  }