/** * Creates a new PhantomJSDriver instance. The instance will have a * default set of desired capabilities. * * @see org.openqa.selenium.phantomjs.PhantomJSDriverService#createDefaultService() for configuration details. */ public PhantomJSDriver() { this(DesiredCapabilities.phantomjs()); }
Capabilities caps = new DesiredCapabilities(); ((DesiredCapabilities) caps).setJavascriptEnabled(true); ((DesiredCapabilities) caps).setCapability("takesScreenshot", true); ((DesiredCapabilities) caps).setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "your custom path\\phantomjs.exe" ); WebDriver driver = new PhantomJSDriver(caps);
protected static Map<String, CommandInfo> getCustomCommands() { Map<String, CommandInfo> customCommands = new HashMap<String, CommandInfo>(); customCommands.put(COMMAND_EXECUTE_PHANTOM_SCRIPT, new CommandInfo("/session/:sessionId/phantom/execute", POST)); return customCommands; } }
DesiredCapabilities createCommonCapabilities(Config config, Proxy proxy) { DesiredCapabilities browserCapabilities = new DesiredCapabilities(); if (proxy != null) { browserCapabilities.setCapability(PROXY, proxy); } if (config.browserVersion() != null && !config.browserVersion().isEmpty()) { browserCapabilities.setVersion(config.browserVersion()); } browserCapabilities.setCapability(PAGE_LOAD_STRATEGY, config.pageLoadStrategy()); browserCapabilities.setCapability(ACCEPT_SSL_CERTS, true); transferCapabilitiesFromSystemProperties(browserCapabilities); browserCapabilities = mergeCapabilitiesFromConfiguration(config, browserCapabilities); return browserCapabilities; }
File profileDirectory = new File("c://mach//lib//prof"); FirefoxProfile profile = new FirefoxProfile(profileDirectory); DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability(FirefoxDriver.PROFILE, profile); driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
@Override public Browser openBrowser() { try { WebDriver driver = new RemoteWebDriver(new URL(gridUrl), this.createCapabilities()); WebDriver augmentedDriver = new Augmenter().augment(driver); return new SeleniumBrowser(augmentedDriver); } catch (Exception ex) { throw new RuntimeException(ex); } }
private WebDriver createRemoteDriver(Config config, Proxy proxy) { try { DesiredCapabilities capabilities = getDriverCapabilities(config, new Browser(config.browser(), false), proxy); RemoteWebDriver webDriver = new RemoteWebDriver(new URL(config.remote()), capabilities); webDriver.setFileDetector(new LocalFileDetector()); return webDriver; } catch (MalformedURLException e) { throw new IllegalArgumentException("Invalid 'remote' parameter: " + config.remote(), e); } }
@Override protected void containerIsStarted(InspectContainerResponse containerInfo) { driver = Unreliables.retryUntilSuccess(30, TimeUnit.SECONDS, Timeouts.getWithTimeout(10, TimeUnit.SECONDS, () -> () -> new RemoteWebDriver(getSeleniumAddress(), capabilities))); if (vncRecordingContainer != null) { LOGGER.debug("Starting VNC recording"); vncRecordingContainer.start(); } }
DesiredCapabilities getDriverCapabilities(Config config, Browser browser, Proxy proxy) { DesiredCapabilities capabilities = createCommonCapabilities(config, proxy); capabilities.setBrowserName(getBrowserNameForGrid(config, browser)); if (config.headless()) { capabilities.merge(getHeadlessCapabilities(config, browser)); } if (!config.browserBinary().isEmpty()) { capabilities.merge(getBrowserBinaryCapabilities(config, browser)); } return capabilities; }
@Test public void testWebDriverToNginxContainerAccessViaContainerLink() { RemoteWebDriver driver = chrome.getWebDriver(); driver.get("http://nginx/"); assertEquals("Using selenium, an HTTP GET from the nginx server returns the index.html from the custom content directory", "This worked", driver.findElement(By.tagName("body")).getText()); } }
@Test public void testContainerRunningAgainstExposedHostPort() { // useHostExposedPort { final String rootUrl = String.format("http://host.testcontainers.internal:%d/", localServerPort); final RemoteWebDriver webDriver = browser.getWebDriver(); webDriver.get(rootUrl); // } final String pageSource = webDriver.getPageSource(); assertTrue(pageSource.contains("Hello World!")); } }
protected void assertBrowserNameIs(BrowserWebDriverContainer rule, String expectedName) { RemoteWebDriver driver = setupDriverFromRule(rule); String actual = driver.getCapabilities().getBrowserName(); assertTrue(format("actual browser name is %s", actual), actual.equals(expectedName)); }
protected static void doSimpleExplore(BrowserWebDriverContainer rule) { RemoteWebDriver driver = setupDriverFromRule(rule); driver.get("http://en.wikipedia.org/wiki/Randomness"); // Oh! The irony! assertTrue("Randomness' description has the word 'pattern'", driver.findElementByPartialLinkText("pattern").isDisplayed()); }
/** * Take screenshot of the current window. * * @param target The target type/format of the Screenshot * @return Screenshot of current window, in the requested format */ @Override public <X> X getScreenshotAs(OutputType<X> target) { // Get the screenshot as base64 and convert it to the requested type (i.e. OutputType<T>) String base64 = (String) execute(DriverCommand.SCREENSHOT).getValue(); return target.convertFromBase64Png(base64); }
private String getConsoleIconPath(DesiredCapabilities cap) { String name = cap.getBrowserName().toLowerCase(); String path = "org/openqa/grid/images/"; InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path + name + ".png"); return in == null ? null : contextPath + "/grid/resources/" + path + name + ".png"; }
@Override public void stop() { if (driver != null) { try { driver.quit(); } catch (Exception e) { LOGGER.debug("Failed to quit the driver", e); } } if (vncRecordingContainer != null) { try { vncRecordingContainer.stop(); } catch (Exception e) { LOGGER.debug("Failed to stop vncRecordingContainer", e); } } super.stop(); }
DesiredCapabilities mergeCapabilitiesFromConfiguration(Config config, DesiredCapabilities currentCapabilities) { return currentCapabilities.merge(config.browserCapabilities()); }
@NotNull private static RemoteWebDriver setupDriverFromRule(BrowserWebDriverContainer rule) { RemoteWebDriver driver = rule.getWebDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); return driver; }
FirefoxProfile firefoxProfile = new FirefoxProfile(); File extension = new File("extension.xpi"); firefoxProfile.addExtension(extension); DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile); driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
LoggingPreferences logs = new LoggingPreferences(); logs.enable(LogType.BROWSER, Level.ALL); logs.enable(LogType.CLIENT, Level.ALL); logs.enable(LogType.DRIVER, Level.ALL); logs.enable(LogType.PERFORMANCE, Level.ALL); logs.enable(LogType.PROFILER, Level.ALL); logs.enable(LogType.SERVER, Level.ALL); DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox(); desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs); WebDriver driver = new FirefoxDriver(desiredCapabilities);