github.com/bazelbuild/rules_webtesting@v0.2.0/go/webtest/webtest.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 provides WebDriver provisioning and information APIs.
    16  //
    17  // Provision a browser:
    18  //   import "github.com/bazelbuild/rules_webtesting/go/webtest"
    19  //
    20  //   driver, err := webtest.NewWebDriverSession(nil)
    21  //
    22  // Provision a browser with capabilities (as an example profiling):
    23  //   capabilities := map[string]interface{}{
    24  //     "webdriver.logging.profiler.enabled": true,
    25  //   }
    26  //   driver, err := webtest.NewWebDriverSession(capabilities)
    27  //
    28  // Get basic information about the browser defined by the web test environment:
    29  //   info, err := webtest.GetBrowserInfo()
    30  package webtest
    31  
    32  import (
    33  	"errors"
    34  	"net/url"
    35  	"os"
    36  	"strings"
    37  
    38  	"github.com/bazelbuild/rules_webtesting/go/bazel"
    39  	"github.com/bazelbuild/rules_webtesting/go/metadata"
    40  	"github.com/tebeka/selenium"
    41  )
    42  
    43  var info *BrowserInfo
    44  
    45  // BrowserInfo represents basic information about the browser defined by the web test environment.
    46  type BrowserInfo struct {
    47  	// The Bazel label for the browser.
    48  	BrowserLabel string
    49  	// The Environment that Web Test Launcher is using for the specified configuration.
    50  	Environment string
    51  }
    52  
    53  // GetBrowserInfo returns basic information about the browser defined by the web test environment.
    54  func GetBrowserInfo() (*BrowserInfo, error) {
    55  	if info == nil {
    56  		i, err := newInfo(os.Getenv("WEB_TEST_METADATA"))
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		info = i
    61  	}
    62  	return info, nil
    63  }
    64  
    65  func newInfo(mf string) (*BrowserInfo, error) {
    66  	f, err := bazel.Runfile(mf)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	m, err := metadata.FromFile(f, nil)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return &BrowserInfo{
    75  		BrowserLabel: m.BrowserLabel,
    76  		Environment:  m.Environment,
    77  	}, nil
    78  }
    79  
    80  // NewWebDriverSession provisions and returns a new WebDriver session.
    81  func NewWebDriverSession(capabilities selenium.Capabilities) (selenium.WebDriver, error) {
    82  	address, ok := os.LookupEnv("WEB_TEST_WEBDRIVER_SERVER")
    83  	if !ok {
    84  		return nil, errors.New(`environment variable "WEB_TEST_WEBDRIVER_SERVER" not set`)
    85  	}
    86  
    87  	return selenium.NewRemote(capabilities, strings.TrimSuffix(address, "/"))
    88  }
    89  
    90  // HTTPAddress returns the HTTP address of WTL.
    91  func HTTPAddress() (*url.URL, error) {
    92  	address := os.Getenv("WEB_TEST_HTTP_SERVER")
    93  
    94  	if address == "" {
    95  		return nil, errors.New(`environment variable "WEB_TEST_HTTP_SERVER" not set`)
    96  	}
    97  
    98  	return url.Parse(address)
    99  }
   100  
   101  // HTTPSAddress returns the HTTPS address of WTL.
   102  func HTTPSAddress() (*url.URL, error) {
   103  	address := os.Getenv("WEB_TEST_HTTPS_SERVER")
   104  
   105  	if address == "" {
   106  		return nil, errors.New(`environment variable "WEB_TEST_HTTPS_SERVER" not set`)
   107  	}
   108  
   109  	return url.Parse(address)
   110  }