github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/proxy/driverhub/scripttimeout/script_timeout_test.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
    16  
    17  import (
    18  	"fmt"
    19  	"log"
    20  	"net/http"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/bazelbuild/rules_webtesting/go/bazel"
    26  	"github.com/bazelbuild/rules_webtesting/go/portpicker"
    27  	"github.com/bazelbuild/rules_webtesting/go/webtest"
    28  	"github.com/tebeka/selenium"
    29  )
    30  
    31  var testpage = ""
    32  
    33  func TestMain(m *testing.M) {
    34  	port, err := portpicker.PickUnusedPort()
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  
    39  	dir, err := bazel.Runfile("testdata/")
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  
    44  	go func() {
    45  		log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), http.FileServer(http.Dir(dir))))
    46  	}()
    47  
    48  	testpage = fmt.Sprintf("http://localhost:%d/testpage.html", port)
    49  
    50  	os.Exit(m.Run())
    51  }
    52  
    53  func TestSetScriptTimeout(t *testing.T) {
    54  	// This test only superficially tests script timeout functionality (e.g. that the timeout still gets set).
    55  	driver, err := webtest.NewWebDriverSession(selenium.Capabilities{})
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	defer driver.Quit()
    61  
    62  	if err := driver.Get(testpage); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	if err := driver.SetAsyncScriptTimeout(1 * time.Second); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	start := time.Now()
    71  	if _, err := driver.ExecuteScriptAsync("return;", []interface{}{}); err == nil {
    72  		t.Fatal("got nil err, expected timeout err")
    73  	}
    74  	if run := time.Now().Sub(start); run < 1*time.Second {
    75  		t.Fatalf("got runtime %v, expected to be at least 1 seconds", run)
    76  	}
    77  
    78  	if err := driver.SetAsyncScriptTimeout(5 * time.Second); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	start = time.Now()
    83  	if _, err := driver.ExecuteScriptAsync("return;", []interface{}{}); err == nil {
    84  		t.Fatal("got nil err, expected timeout err")
    85  	}
    86  	if run := time.Now().Sub(start); run < 5*time.Second {
    87  		t.Fatalf("got runtime %v, expected to be at least 1 seconds", run)
    88  	}
    89  
    90  }