github.com/bazelbuild/rules_webtesting@v0.2.0/java/com/google/testing/web/WebTest.java (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 // ////////////////////////////////////////////////////////////////////////////// 16 // 17 package com.google.testing.web; 18 19 import java.net.MalformedURLException; 20 import java.net.URI; 21 import java.net.URISyntaxException; 22 import java.net.URL; 23 import java.util.Optional; 24 import org.openqa.selenium.Capabilities; 25 import org.openqa.selenium.WebDriver; 26 import org.openqa.selenium.remote.Augmenter; 27 import org.openqa.selenium.remote.DesiredCapabilities; 28 import org.openqa.selenium.remote.RemoteWebDriver; 29 30 /** 31 * Browser provisioning and information API. 32 * 33 * <p>Provision a browser: 34 * 35 * <pre class="code"> 36 * import com.google.testing.web.WebTest; 37 * 38 * WebDriver driver = new WebTest().newWebDriverSession(); 39 * </pre> 40 * 41 * <p>Provision a browser with capabilities (as an example, profiling): 42 * 43 * <pre class="code"> 44 * DesiredCapabilities capabilities = new DesiredCapabilities(); 45 * capabilities.setCapability(CapabilityType.ENABLE_PROFILING_CAPABILITY, true); 46 * 47 * WebDriver driver = new WebTest().newWebDriverSession(capabilities); 48 * </pre> 49 */ 50 public class WebTest { 51 52 private final URL wd; 53 private final URI http; 54 private final Optional<URI> https; 55 56 public WebTest() { 57 this( 58 System.getenv("WEB_TEST_WEBDRIVER_SERVER"), 59 System.getenv("WEB_TEST_HTTP_SERVER"), 60 System.getenv("WEB_TEST_HTTPS_SERVER")); 61 } 62 63 private WebTest(String wd, String http, String https) { 64 try { 65 this.wd = new URL(wd); 66 this.http = new URI(http); 67 if (https != null && !https.isEmpty()) { 68 this.https = Optional.of(new URI(https)); 69 } else { 70 this.https = Optional.empty(); 71 } 72 } catch (MalformedURLException | URISyntaxException e) { 73 throw new RuntimeException(e); 74 } 75 } 76 77 /** Provisions and returns a new default {@link WebDriver} session. */ 78 public WebDriver newWebDriverSession() { 79 return newWebDriverSession(new DesiredCapabilities()); 80 } 81 82 /** 83 * Provisions and returns a new {@link WebDriver} session. 84 * 85 * @param capabilities Configuration of the browser. 86 */ 87 public WebDriver newWebDriverSession(Capabilities capabilities) { 88 WebDriver driver = new Augmenter().augment(new RemoteWebDriver(wd, capabilities)); 89 90 return driver; 91 } 92 93 /** Returns the HTTP address of WTL. */ 94 public URI HTTPAddress() { 95 return http; 96 } 97 98 /** Returns the HTTPS address of WTL. */ 99 public Optional<URI> HTTPSAddress() { 100 return https; 101 } 102 }