github.com/bazelbuild/rules_webtesting@v0.2.0/testing/web/webtest.py (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  """Browser provisioning and information API.
    15  
    16  Provision a browser:
    17    from testing.web import webtest
    18  
    19    driver = webtest.new_webdriver_session()
    20  
    21  Provision a browser with capabilities:
    22    capabilities = {"webdriver.logging.profiler.enabled": true}
    23    driver = webtest.new_webdriver_session(capabilities)
    24  """
    25  import os
    26  
    27  from selenium.webdriver.remote import remote_connection
    28  from selenium.webdriver.remote import webdriver
    29  
    30  
    31  def new_webdriver_session(capabilities=None):
    32    """Provisions a new WebDriver session.
    33  
    34    Args:
    35      capabilities: a dict with the json capabilities desired for this browser
    36        session.
    37  
    38    Returns:
    39      A new WebDriver connected to a browser defined by the web test
    40      environment.
    41    """
    42    capabilities = capabilities or {}
    43    address = os.environ['WEB_TEST_WEBDRIVER_SERVER'].rstrip('/')
    44  
    45    # Set the timeout for WebDriver http requests so that the socket default
    46    # timeout is not used.
    47    remote_connection.RemoteConnection.set_timeout(450)
    48  
    49    return webdriver.WebDriver(address, desired_capabilities=capabilities)
    50  
    51  
    52  def http_address():
    53    """Return the HTTP address of WTL."""
    54    return os.environ['WEB_TEST_HTTP_SERVER']
    55  
    56  
    57  def https_address():
    58    """Return the HTTPS address of WTL."""
    59    return os.environ['WEB_TEST_HTTPS_SERVER']