github.com/bazelbuild/rules_webtesting@v0.2.0/go/webtest/webtest_test.go (about)

     1  // Copyright 2016 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 webtest
    16  
    17  import (
    18  	"net/url"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/tebeka/selenium"
    23  )
    24  
    25  func TestProvisionBrowser_NoCaps(t *testing.T) {
    26  	wd, err := NewWebDriverSession(selenium.Capabilities{})
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	address, err := HTTPAddress()
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	address = address.ResolveReference(&url.URL{Path: "/healthz"})
    37  
    38  	if err := wd.Get(address.String()); err != nil {
    39  		t.Error(err)
    40  	}
    41  
    42  	url, err := wd.CurrentURL()
    43  	if err != nil {
    44  		t.Error(err)
    45  	}
    46  	if url == "" {
    47  		t.Error("Got empty url")
    48  	}
    49  
    50  	if err := wd.Quit(); err != nil {
    51  		t.Error(err)
    52  	}
    53  }
    54  
    55  func TestProvisionBrowser_WithCaps(t *testing.T) {
    56  	wd, err := NewWebDriverSession(selenium.Capabilities{
    57  		"acceptInsecureCerts": false,
    58  		"pageLoadStrategy":    "normal",
    59  	})
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	address, err := HTTPAddress()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	address = address.ResolveReference(&url.URL{Path: "/healthz"})
    70  
    71  	if err := wd.Get(address.String()); err != nil {
    72  		t.Error(err)
    73  	}
    74  
    75  	url, err := wd.CurrentURL()
    76  	if err != nil {
    77  		t.Error(err)
    78  	}
    79  	if url == "" {
    80  		t.Error("Got empty url")
    81  	}
    82  
    83  	if err := wd.Quit(); err != nil {
    84  		t.Error(err)
    85  	}
    86  }
    87  
    88  func TestGetInfo(t *testing.T) {
    89  	i, err := GetBrowserInfo()
    90  
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	switch {
    96  	case strings.Contains(i.BrowserLabel, "sauce"):
    97  		if i.Environment != "sauce" {
    98  			t.Errorf(`got Environment = %q, expected "sauce"`, i.Environment)
    99  		}
   100  	case strings.Contains(i.BrowserLabel, "chromium"):
   101  		if i.Environment != "local" {
   102  			t.Errorf(`got Environment = %q, expected "local"`, i.Environment)
   103  		}
   104  	case strings.Contains(i.BrowserLabel, "firefox"):
   105  		if i.Environment != "local" {
   106  			t.Errorf(`got Environment = %q, expected "local"`, i.Environment)
   107  		}
   108  	}
   109  }
   110  
   111  func TestReusableBrowser(t *testing.T) {
   112  	address, err := HTTPAddress()
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	address = address.ResolveReference(&url.URL{Path: "/healthz"})
   118  
   119  	wd1, err := NewWebDriverSession(selenium.Capabilities{
   120  		"google:canReuseSession": true,
   121  	})
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  
   126  	id1 := wd1.SessionID()
   127  	if err := wd1.Quit(); err != nil {
   128  		t.Error(err)
   129  	}
   130  
   131  	wd2, err := NewWebDriverSession(selenium.Capabilities{
   132  		"google:canReuseSession": true,
   133  	})
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	if id1 != wd2.SessionID() {
   139  		t.Errorf("got different ids %q, %q, expected them to be the same.", id1, wd2.SessionID())
   140  	}
   141  
   142  	if err := wd2.Get(address.String()); err != nil {
   143  		t.Error(err)
   144  	}
   145  
   146  	url, err := wd2.CurrentURL()
   147  	if err != nil {
   148  		t.Error(err)
   149  	}
   150  	if url == "" {
   151  		t.Error("Got empty url")
   152  	}
   153  
   154  	if err := wd2.Quit(); err != nil {
   155  		t.Error(err)
   156  	}
   157  }