private ProxyDriverIntegrator getProxyDriverIntegrator(RequestFilter recordRequestFilter, WebDriverSupplier webDriverSupplier, DriverServiceSupplier driverServiceSupplier, @Named(PATH_TO_DRIVER) String pathToDriverExecutable, @Named(SCREEN) String screen, @Named(TIMEOUT) int timeout, ResponseFilter responseFilter) throws IOException { BrowserMobProxy proxy = createBrowserMobProxy(timeout, recordRequestFilter, responseFilter); proxy.start(0); logger.info("Proxy running on port " + proxy.getPort()); Proxy seleniumProxy = createSeleniumProxy(proxy); DesiredCapabilities desiredCapabilities = createDesiredCapabilities(seleniumProxy); DriverService driverService = driverServiceSupplier.getDriverService(pathToDriverExecutable, screen); WebDriver driver = webDriverSupplier.get(driverService, desiredCapabilities); return new ProxyDriverIntegrator(driver, proxy, driverService); }
public WebDriver start(DriverServiceFactory factory, DriverService driverService) { WebDriver driver = null; for (int i = 0; i < retries && driver == null; i++) { log.info("trying to start {}/{} (attempt/max attempts)", i, retries); driver = startServiceAndCreateWebdriver(driverService, factory); log.info("is started: {}", driver == null); if (driver == null) { try { timeUnit.sleep(2); } catch (InterruptedException e) { log.error(e.getMessage(), e); } } } return driver; }
public final DriverService getDriverService() { try { if (driverService == null) { driverService = createDriverService(); wasRunning = driverService.isRunning(); if (wasRunning) { LOGGER.info("Driver service {} is already running", driverService.getUrl()); } else { driverService.start(); LOGGER.info("Driver service {} started", driverService.getUrl()); } } return driverService; } catch (IOException e) { throw Throwables.propagate(e); } }
public WebDriverEx createWebDriver(DriverService driverService, DesiredCapabilities desiredCapabilities, SiteConfig siteConfig, DriverConfig driverConfig) throws IOException { driverService.start(); //自定义HttpClientFactory用于设置命令超时时间 ApacheHttpClient.Factory httpClientFactory = createHttpClientFactory(siteConfig, driverConfig); HttpCommandExecutor httpCommandExecutor = new HttpCommandExecutor( ImmutableMap.<String, CommandInfo>of(), driverService.getUrl(), httpClientFactory); WebDriverEx webDriver = new WebDriverEx(httpCommandExecutor, desiredCapabilities); webDriver.setDriverService(driverService); webDriver.setCreatedTime(new Date()); webDriver.manage().timeouts().implicitlyWait(driverConfig.getImplicitlyWait(), TimeUnit.MILLISECONDS); webDriver.manage().timeouts().pageLoadTimeout(driverConfig.getPageLoadTimeout(), TimeUnit.MILLISECONDS); webDriver.manage().timeouts().setScriptTimeout(driverConfig.getScriptTimeout(), TimeUnit.MILLISECONDS); return webDriver; }
@Override public DriverService getDriverService(String pathToDriverExecutable, String screenToUse) throws IOException { return getBuilder() .usingDriverExecutable(new File(pathToDriverExecutable)) .usingAnyFreePort() .withEnvironment(ImmutableMap.of("DISPLAY", screenToUse)) .build(); }
@Test public void invokesTheCorrectMethodsOfTheBuilder() throws IOException { DriverService.Builder builder = mock(DriverService.Builder.class, RETURNS_DEEP_STUBS); DriverServiceSupplierBase driverServiceSupplierBase = new DriverServiceSupplierBase() { @Override protected DriverService.Builder getBuilder() { return builder; } }; DriverService driverService = driverServiceSupplierBase.getDriverService(driverExecutableFileName, screenToUse); verify(builder).usingDriverExecutable(eq(driverExecutableFile)); }
@Test public void providesAccessToConnectedDriverProxyAndDriverService() { WebDriver driver = mock(WebDriver.class); BrowserMobProxy proxy = mock(BrowserMobProxy.class); DriverService driverService = mock(DriverService.class); ProxyDriverIntegrator proxyDriverIntegrator = new ProxyDriverIntegrator(driver, proxy, driverService); assertEquals(driver, proxyDriverIntegrator.getWebDriver()); assertEquals(proxy, proxyDriverIntegrator.getProxy()); assertEquals(driverService, proxyDriverIntegrator.getDriverService()); }
private WebDriver startServiceAndCreateWebdriver(DriverService driverService, DriverServiceFactory factory) { ExecutorService service = Executors.newFixedThreadPool(1); @SuppressWarnings("unchecked") Callable<WebDriver> startJob = () -> { if (!driverService.isRunning()) { log.info("starting"); driverService.start(); } log.info("try to create webdriver"); return factory.createWebDriver(driverService); }; Future<WebDriver> startFuture = service.submit(startJob); try { log.info("waiting for webdriver"); return startFuture.get(timeout, timeUnit); } catch (ExecutionException | InterruptedException | TimeoutException e) { log.warn(e.getMessage(), e); if (driverService.isRunning()) { driverService.stop(); } } finally { service.shutdownNow(); } return null; }
/** * Gets a web driver for a given browser * @param browser The browser to get a driver for * @return * @throws IOException */ public static synchronized RemoteWebDriver getDriver(Browser browser) throws IOException { if (global == null) { DriverService service = start(browser); global = new RemoteWebDriver(service.getUrl(), getDesiredBrowserCapabilities(browser)); drivers.put(global, service); } return global; }
/** * Starts a selenium service for a given browser * @param browser The browser to start the service for * @return * @throws IOException */ public static DriverService start(Browser browser) throws IOException { BrowserDriverProvider provider = getProvider(browser); DriverService service = new ChromeDriverService.Builder().usingDriverExecutable(provider.getDriverFile()).usingAnyFreePort().build(); service.start(); return service; }
@Override protected DriverService createDriverService() { Builder builder = new ChromeDriverService.Builder(); if (port != null) builder.usingPort(port); if (driverExecutable != null) builder.usingDriverExecutable(driverExecutable); if (environment != null) builder.withEnvironment(environment); if (logFile != null) builder.withLogFile(logFile); if (verbose != null) builder.withVerbose(verbose); if (silent != null) builder.withSilent(silent); return builder.build(); }
@Override protected DriverService createDriverService() { Builder builder = new GeckoDriverService.Builder(); if (port != null) builder.usingPort(port); if (driverExecutable != null) builder.usingDriverExecutable(driverExecutable); if (environment != null) builder.withEnvironment(environment); if (logFile != null) builder.withLogFile(logFile); return builder.build(); }
@Override public DriverService createDriverService() { Builder builder = new PhantomJSDriverService.Builder(); if (driverExecutable != null) builder.usingPhantomJSExecutable(driverExecutable); if (ghostDriver != null) builder.usingGhostDriver(ghostDriver); if (port != null) builder.usingPort(port); if (environment != null) builder.withEnvironment(environment); if (logFile != null) builder.withLogFile(logFile); if (proxy != null) builder.withProxy(new Proxy(proxy)); if (commandLineArguments != null) builder.usingCommandLineArguments(commandLineArguments); if (ghostDriverCommandLineArguments != null) builder.usingGhostDriverCommandLineArguments(ghostDriverCommandLineArguments); return builder.build(); }
@Override public DriverService createDriverService() { Builder builder = new InternetExplorerDriverService.Builder(); if (port != null) builder.usingPort(port); if (driverExecutable != null) builder.usingDriverExecutable(driverExecutable); if (environment != null) builder.withEnvironment(environment); if (logFile != null) builder.withLogFile(logFile); if (logLevel != null) builder.withLogLevel(InternetExplorerDriverLogLevel.valueOf(logLevel.toUpperCase())); if (engineImplementation != null) builder.withEngineImplementation(InternetExplorerDriverEngine.valueOf(engineImplementation.toUpperCase())); if (host != null) builder.withHost(host); if (extractPath != null) builder.withExtractPath(extractPath); if (silent != null) builder.withSilent(silent); return builder.build(); }
@Override public WebDriver create(WebDriverFactory webDriverFactory, DesiredCapabilities desiredCapabilities) { ChromeDriverServiceProperties serviceProperties = webDriverFactory.driverServices == null ? null : webDriverFactory.driverServices.getChrome(); DriverService driverService = serviceProperties == null ? null : serviceProperties.getDriverService(); return driverService == null ? new ChromeDriver(new ChromeOptions().merge(desiredCapabilities)) : new RemoteWebDriver(driverService.getUrl(), desiredCapabilities); }
@Override public WebDriver create(WebDriverFactory webDriverFactory, DesiredCapabilities desiredCapabilities) { FirefoxDriverServiceProperties serviceProperties = webDriverFactory.driverServices == null ? null : webDriverFactory.driverServices.getFirefox(); DriverService driverService = serviceProperties == null ? null : serviceProperties.getDriverService(); FirefoxOptions firefoxOptions = new FirefoxOptions().merge(desiredCapabilities); return driverService == null ? new FirefoxDriver(firefoxOptions) : new RemoteWebDriver(driverService.getUrl(), firefoxOptions); }
@Override public WebDriver create(WebDriverFactory webDriverFactory, DesiredCapabilities desiredCapabilities) { InternetExplorerDriverServiceProperties serviceProperties = webDriverFactory.driverServices == null ? null : webDriverFactory.driverServices.getInternetExplorer(); DriverService driverService = serviceProperties == null ? null : serviceProperties.getDriverService(); InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions().merge(desiredCapabilities); return driverService == null ? new InternetExplorerDriver(internetExplorerOptions) : new RemoteWebDriver(driverService.getUrl(), internetExplorerOptions); }
public DriverService getDriverService() { return driverService; }
public void setDriverService(DriverService driverService) { this.driverService = driverService; }
@Override protected DriverService.Builder getBuilder() { return new ChromeDriverService.Builder(); }
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service, HttpClient.Factory httpClientFactory) { super(additionalCommands, service.getUrl(), httpClientFactory); this.service = service; }
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service) { this(additionalCommands, service, new ApacheHttpClient.Factory()); }
public MyChromeDriverCommandExecutor(DriverService service) { super(service, CHROME_COMMAND_NAME_TO_URL); }
@SuppressWarnings("unchecked") private Map<String, String> getEnvFromDriverService(ChromeDriverService service) throws NoSuchFieldException, IllegalAccessException { Field field = DriverService.class.getDeclaredField("environment"); field.setAccessible(true); return (Map<String, String>) field.get(service); }
@Override public WebDriver create(WebDriverFactory webDriverFactory, DesiredCapabilities desiredCapabilities) { PhantomJsDriverServiceProperties serviceProperties = webDriverFactory.driverServices == null ? null : webDriverFactory.driverServices.getPhantomJs(); DriverService driverService = serviceProperties == null ? null : serviceProperties.getDriverService(); return driverService == null ? new PhantomJSDriver(desiredCapabilities) : new RemoteWebDriver(driverService.getUrl(), desiredCapabilities); }
/** * Creates a new object that holds a driver, a proxy and a driver service that are connected together * @param driver the driver * @param proxy the proxy that the driver uses * @param driverService the driver service that is used for connecting to the virtual screen */ public ProxyDriverIntegrator(WebDriver driver, BrowserMobProxy proxy, DriverService driverService) { this.driver = driver; this.proxy = proxy; this.driverService = driverService; }
DriverService getDriverService(String pathToDriverExecutable, String screenToUse) throws IOException;
protected abstract DriverService.Builder getBuilder();
protected abstract DriverService createDriverService();