github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/environment/external/external.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 external works with an externally started WebDriver server
    16  // located at EXTERNAL_WEBDRIVER_SERVER_ADDRESS.
    17  package external
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  
    24  	"github.com/bazelbuild/rules_webtesting/go/errors"
    25  	"github.com/bazelbuild/rules_webtesting/go/metadata"
    26  	"github.com/bazelbuild/rules_webtesting/go/wtl/diagnostics"
    27  	"github.com/bazelbuild/rules_webtesting/go/wtl/environment"
    28  )
    29  
    30  const (
    31  	name          = "External WebDriver Environment"
    32  	addressEnvVar = "EXTERNAL_WEBDRIVER_SERVER_ADDRESS"
    33  )
    34  
    35  type external struct {
    36  	*environment.Base
    37  	address string
    38  }
    39  
    40  // NewEnv creates a new environment that uses an externally started Selenium Server.
    41  func NewEnv(m *metadata.Metadata, d diagnostics.Diagnostics) (environment.Env, error) {
    42  	address, ok := os.LookupEnv(addressEnvVar)
    43  	if !ok {
    44  		return nil, errors.New(name, fmt.Errorf("environment variable %q not set", addressEnvVar))
    45  	}
    46  
    47  	base, err := environment.NewBase(name, m, d)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	return &external{
    53  		Base:    base,
    54  		address: address,
    55  	}, nil
    56  }
    57  
    58  // WDAddress returns the user-provided selenium address.
    59  func (e *external) WDAddress(context.Context) string {
    60  	return e.address
    61  }