Java Code Examples for org.openqa.selenium.remote.DesiredCapabilities#chrome()

The following examples show how to use org.openqa.selenium.remote.DesiredCapabilities#chrome() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteDesiredCapabilitiesFactory.java    From jmeter-plugins-webdriver with Apache License 2.0 6 votes vote down vote up
public static DesiredCapabilities build(RemoteCapability capability){
 DesiredCapabilities desiredCapabilities;
 if(RemoteCapability.CHROME.equals(capability)){
  ChromeOptions options = new ChromeOptions();
     desiredCapabilities = DesiredCapabilities.chrome();
     desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
  return desiredCapabilities;
 } else if (RemoteCapability.FIREFOX.equals(capability)){
  FirefoxProfile profile = new FirefoxProfile();
  desiredCapabilities = DesiredCapabilities.firefox();
  desiredCapabilities.setCapability(FirefoxDriver.PROFILE, profile);
  return desiredCapabilities;
 } else if (RemoteCapability.INTERNET_EXPLORER.equals(capability)){
  desiredCapabilities = DesiredCapabilities.internetExplorer();
  return desiredCapabilities;
 } else if (RemoteCapability.PHANTOMJS.equals(capability)){
  desiredCapabilities = DesiredCapabilities.phantomjs();
  return desiredCapabilities;
 }
 throw new IllegalArgumentException("No such capability");
}
 
Example 2
Source File: SeleniumTest.java    From webmagic with Apache License 2.0 6 votes vote down vote up
@Ignore("need chrome driver")
@Test
public void testSelenium() {
    System.getProperties().setProperty("webdriver.chrome.driver", "/Users/yihua/Downloads/chromedriver");
    Map<String, Object> contentSettings = new HashMap<String, Object>();
    contentSettings.put("images", 2);

    Map<String, Object> preferences = new HashMap<String, Object>();
    preferences.put("profile.default_content_settings", contentSettings);

    DesiredCapabilities caps = DesiredCapabilities.chrome();
    caps.setCapability("chrome.prefs", preferences);
    caps.setCapability("chrome.switches", Arrays.asList("--user-data-dir=/Users/yihua/temp/chrome"));
    WebDriver webDriver = new ChromeDriver(caps);
    webDriver.get("http://huaban.com/");
    WebElement webElement = webDriver.findElement(By.xpath("/html"));
    System.out.println(webElement.getAttribute("outerHTML"));
    webDriver.close();
}
 
Example 3
Source File: ChromeDriverClient.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
public void createDriver(String pkg_name, String sn) {
    	if(this.driver == null) {
	        ChromeOptions chromeOptions = new ChromeOptions();
	        chromeOptions.setExperimentalOption("androidPackage", pkg_name);
	//        chromeOptions.setExperimentalOption("androidActivity", "com.eg.android.AlipayGphone.AlipayLogin");
	//        chromeOptions.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
	        chromeOptions.setExperimentalOption("androidUseRunningApp", true);
	        chromeOptions.setExperimentalOption("androidDeviceSerial", sn);
	//        Map<String, Object> chromeOptions = new HashMap<String, Object>();
	//        chromeOptions.put("androidPackage", "com.eg.android.AlipayGphoneRC");
	//        chromeOptions.put("androidActivity", "com.eg.android.AlipayGphone.AlipayLogin");
	        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
	        LoggingPreferences logPrefs = new LoggingPreferences();
	        logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
	        capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
	        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
//	        capabilities.setCapability(CapabilityType., value);
	        if(ChromeService.getService() != null)
	        	driver = new RobotRemoteWebDriver(ChromeService.getService().getUrl(), capabilities);
    	}
    }
 
Example 4
Source File: DebateFetcher.java    From argument-reasoning-comprehension-task with Apache License 2.0 6 votes vote down vote up
public DebateFetcher(String chromeDriverFile)
        throws IOException
{
    service = new ChromeDriverService.Builder()
            .usingDriverExecutable(
                    new File(chromeDriverFile))
            .usingAnyFreePort()
            .withEnvironment(ImmutableMap.of("DISPLAY", ":20")).build();
    service.start();

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    driver = new RemoteWebDriver(service.getUrl(), capabilities);
}
 
Example 5
Source File: SeleniumTestUtilities.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
public static WebDriver getChromeDriver()
{
       String path = "src/test/resources/chromedriver";
	System.setProperty("webdriver.chrome.driver", path);

	DesiredCapabilities capabilities = DesiredCapabilities.chrome();
       capabilities.setCapability("networkConnectionEnabled", true);
       capabilities.setCapability("browserConnectionEnabled", true);

       return new ChromeDriver(capabilities);
}
 
Example 6
Source File: ChromeAndroidUser.java    From openvidu with Apache License 2.0 5 votes vote down vote up
public ChromeAndroidUser(String userName, int timeOfWaitInSeconds) {
	super(userName, timeOfWaitInSeconds);

	Map<String, String> mobileEmulation = new HashMap<>();
	mobileEmulation.put("deviceName", "Pixel 2");

	ChromeOptions chromeOptions = new ChromeOptions();
	chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

	DesiredCapabilities capabilities = DesiredCapabilities.chrome();
	capabilities.setAcceptInsecureCerts(true);
	capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

	// This flag avoids to grant the user media
	chromeOptions.addArguments("--use-fake-ui-for-media-stream");
	// This flag fakes user media with synthetic video
	chromeOptions.addArguments("--use-fake-device-for-media-stream");

	String REMOTE_URL = System.getProperty("REMOTE_URL_CHROME");
	if (REMOTE_URL != null) {
		log.info("Using URL {} to connect to remote web driver", REMOTE_URL);
		try {
			this.driver = new RemoteWebDriver(new URL(REMOTE_URL), capabilities);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	} else {
		log.info("Using local web driver");
		this.driver = new ChromeDriver(capabilities);
	}

	this.driver.manage().timeouts().setScriptTimeout(this.timeOfWaitInSeconds, TimeUnit.SECONDS);
	this.configureDriver();
}
 
Example 7
Source File: ChromeBrowser.java    From SeleniumCucumber with GNU General Public License v3.0 5 votes vote down vote up
public Capabilities getChromeCapabilities() {
	ChromeOptions option = new ChromeOptions();
	option.addArguments("start-maximized");
	DesiredCapabilities chrome = DesiredCapabilities.chrome();
	chrome.setJavascriptEnabled(true);
	chrome.setCapability(ChromeOptions.CAPABILITY, option);
	return chrome;
}
 
Example 8
Source File: FetcherChrome.java    From sparkler with Apache License 2.0 5 votes vote down vote up
@Override
public void init(JobContext context, String pluginId) throws SparklerException {
    super.init(context, pluginId);

    SparklerConfiguration config = jobContext.getConfiguration();
    //TODO should change everywhere 
    pluginConfig = config.getPluginConfiguration(pluginId);
    String loc = (String) pluginConfig.getOrDefault("chrome.dns", "");
    if(loc.equals("")){
        driver = new ChromeDriver();
    }
    else{
        try {
            final DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
            final ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--no-sandbox");
            chromeOptions.addArguments("--headless");
            desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

            driver = new RemoteWebDriver(new URL(loc), desiredCapabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }

}
 
Example 9
Source File: ChromeRemote.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
@BeforeMethod
public void setUpProxy() throws Exception {
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.PROXY, getProxy());
    //or
    //capabilities.setCapability(CapabilityType.PROXY, server.seleniumProxy());
    driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
}
 
Example 10
Source File: SeleniumTestUtilities.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
public static WebDriver getChromeDriver()
{
       String path = "src/test/resources/chromedriver";
	System.setProperty("webdriver.chrome.driver", path);

	DesiredCapabilities capabilities = DesiredCapabilities.chrome();
       capabilities.setCapability("networkConnectionEnabled", true);
       capabilities.setCapability("browserConnectionEnabled", true);

       return new ChromeDriver(capabilities);
}
 
Example 11
Source File: SeleniumTestUtilities.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
public static WebDriver getChromeDriver(String pathToChromeExecutable)
{
	String path = System.getProperty("user.dir") + "\\Drivers\\chromedriver.exe";
	System.setProperty("webdriver.chrome.driver",path);

	Map<String, Object> chromeOptions = new HashMap<String, Object>();
	chromeOptions.put("binary", pathToChromeExecutable);
	DesiredCapabilities capabilities = DesiredCapabilities.chrome();
	capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

	return new ChromeDriver(capabilities);
}
 
Example 12
Source File: SeleniumTestUtilities.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
public static WebDriver getChromeDriver(String pathToChromeExecutable)
{
	String path = System.getProperty("user.dir") + "\\Drivers\\chromedriver.exe";
	System.setProperty("webdriver.chrome.driver",path);

	Map<String, Object> chromeOptions = new HashMap<String, Object>();
	chromeOptions.put("binary", pathToChromeExecutable);
	DesiredCapabilities capabilities = DesiredCapabilities.chrome();
	capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

	return new ChromeDriver(capabilities);
}
 
Example 13
Source File: HtmlExporterUtils.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
/**
     * 初始化配置 PhantomJS Driver.
     *
     * @param url         目标 URL
     * @param addedCookie 添加 cookie
     * @return 初始化过的 PhantomJS Driver
     */
    public static PhantomJSDriver prepare(String url, Cookie addedCookie, Integer width, Integer height) {
        // chrome driver maybe not necessary
        // download from https://sites.google.com/a/chromium.org/chromedriver/downloads
//        System.setProperty("webdriver.chrome.driver",
//                DirUtils.RESOURCES_PATH.concat(
//                        PropUtils.getInstance().getProperty("html.exporter.webdriver.chrome.driver")));

        DesiredCapabilities phantomCaps = DesiredCapabilities.chrome();
        phantomCaps.setJavascriptEnabled(true);
        PropUtils p = PropUtils.getInstance();
        phantomCaps.setCapability("phantomjs.page.settings.userAgent",
                p.getProperty("html.exporter.user.agent"));

        PhantomJSDriver driver = new PhantomJSDriver(phantomCaps);
        driver.manage().timeouts().implicitlyWait(Integer.parseInt(
                p.getProperty("html.exporter.driver.timeouts.implicitly.seconds")), TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(Integer.parseInt(
                p.getProperty("html.exporter.driver.timeouts.page.load.seconds")), TimeUnit.SECONDS);
        driver.manage().timeouts().setScriptTimeout(Integer.parseInt(
                p.getProperty("html.exporter.driver.timeouts.script.seconds")), TimeUnit.SECONDS);

        if (width == null || height == null) driver.manage().window().maximize();
        else driver.manage().window().setSize(new Dimension(width, height));

        if (addedCookie != null) driver.manage().addCookie(addedCookie);

        driver.get(url);
//        try {
//            // timeout is not work, so fix it by sleeping thread
//            Thread.sleep(Integer.valueOf(PropUtils.getInstance()
//                    .getProperty("html.exporter.driver.timeouts.implicitly.seconds")) * 1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        return driver;
    }
 
Example 14
Source File: WebDriverUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * <p>
 * remote.public.driver set to chrome or firefox (null assumes firefox).
 * </p><p>
 * if remote.public.hub is set a RemoteWebDriver is created (Selenium Grid)
 * if proxy.host is set, the web driver is setup to use a proxy
 * </p>
 *
 * @return WebDriver or null if unable to create
 */
public static WebDriver getWebDriver() {
    String driverParam = System.getProperty(HUB_DRIVER_PROPERTY);
    String hubParam = System.getProperty(HUB_PROPERTY);
    String proxyParam = System.getProperty(PROXY_HOST_PROPERTY);

    // setup proxy if specified as VM Arg
    DesiredCapabilities capabilities = new DesiredCapabilities();
    WebDriver webDriver = null;
    if (StringUtils.isNotEmpty(proxyParam)) {
        capabilities.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyParam));
    }

    if (hubParam == null) {
        if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) {
            FirefoxProfile profile = new FirefoxProfile();
            profile.setEnableNativeEvents(false);
            capabilities.setCapability(FirefoxDriver.PROFILE, profile);
            return new FirefoxDriver(capabilities);
        } else if ("chrome".equalsIgnoreCase(driverParam)) {
            return new ChromeDriver(capabilities);
        } else if ("safari".equals(driverParam)) {
            System.out.println("SafariDriver probably won't work, if it does please contact Erik M.");
            return new SafariDriver(capabilities);
        }
    } else {
        try {
            if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) {
                return new RemoteWebDriver(new URL(getHubUrlString()), DesiredCapabilities.firefox());
            } else if ("chrome".equalsIgnoreCase(driverParam)) {
                return new RemoteWebDriver(new URL(getHubUrlString()), DesiredCapabilities.chrome());
            }
        } catch (MalformedURLException mue) {
            System.out.println(getHubUrlString() + " " + mue.getMessage());
            mue.printStackTrace();
        }
    }
    return null;
}
 
Example 15
Source File: BrowserDriver.java    From SWET with MIT License 5 votes vote down vote up
private static DesiredCapabilities capabilitiesiPhone() {
	DesiredCapabilities capabilities = DesiredCapabilities.chrome();

	Map<String, String> mobileEmulation = new HashMap<>();
	mobileEmulation.put("deviceName", "Apple iPhone 6");

	Map<String, Object> chromeOptions = new HashMap<>();
	chromeOptions.put("mobileEmulation", mobileEmulation);
	capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

	return capabilities;
}
 
Example 16
Source File: LocalDriverFactory.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
public static DesiredCapabilities getChromeMobileCapabilities(Map<String, Object> profile) {
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    if (profile != null) {
        capabilities.setCapability(ChromeOptions.CAPABILITY, profile);
    }
    return capabilities;
}
 
Example 17
Source File: ExamplesTest.java    From carnotzet with Apache License 2.0 5 votes vote down vote up
private static WebDriver createBrowserSession() throws MalformedURLException {
	DesiredCapabilities capabilities = DesiredCapabilities.chrome();
	capabilities.setJavascriptEnabled(true);
	LoggingPreferences logPreferences = new LoggingPreferences();
	logPreferences.enable(LogType.BROWSER, Level.ALL);
	capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPreferences);
	return new RemoteWebDriver(
			new URL("http://" + runtime.getContainer("selenium-chrome").getIp() + ":4444/wd/hub"),
			capabilities
	);
}
 
Example 18
Source File: SauceLabsWebDriverHelper.java    From rice with Educational Community License v2.0 4 votes vote down vote up
/**
 * <p>
 * Saucelabs setUp.
 * </p><p>
 * Creates a {@link org.openqa.selenium.remote.RemoteWebDriver} instance with the DesiredCapabilities as configured
 * using the JVM arguments described as SAUCE_ Constants in this class.  After setUp the WebDriver can be accessed via
 * {@see #getDriver}.  You'll also need {@see #getSessionId} for when you call {@see #tearDown}
 * </p>
 *
 * @param className class name of the test being setup as a String
 * @param testName test name of the test being setup as a String
 * @throws Exception
 */
public void setUp(String className, String testName) throws Exception {
    if (System.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY) == null) { // dup guard so WebDriverUtils doesn't have to be used.
        return;
    }

    if (System.getProperty(SAUCE_USER_PROPERTY) == null || System.getProperty(SAUCE_KEY_PROPERTY) == null) {
        Assert.fail("-D" + SAUCE_USER_PROPERTY + " and -D" + SAUCE_KEY_PROPERTY + " must be set to saucelabs user and access key.");
    }

    DesiredCapabilities capabilities = null;
    if ("ff".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
        capabilities = DesiredCapabilities.firefox();
    } else if ("ie".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY)))  {
        capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
            System.getProperty(SAUCE_IE_INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS_PROPERTY, "true"));
    } else if ("chrome".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY)))  {
        capabilities = DesiredCapabilities.chrome();
    } else if ("opera".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY)))  {
        capabilities = DesiredCapabilities.opera();
    } else if ("android".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY)))  {
        capabilities = DesiredCapabilities.android();
    } else if ("safari".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY)))  {
        capabilities = DesiredCapabilities.safari();
    } else if ("ipad".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY)))  {
        capabilities = DesiredCapabilities.ipad();
    } else if ("iphone".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY)))  {
        capabilities = DesiredCapabilities.iphone();
    } else {
        capabilities = DesiredCapabilities.firefox();
    }

    String version = System.getProperty(SAUCE_VERSION_PROPERTY);
    if (version == null || "0".equals(version)) { // Blank or 0 leaves version blank for use with chrome

        if (!"chrome".equalsIgnoreCase(System.getProperty(SAUCE_BROWSER_PROPERTY))) {
            throw new RuntimeException("Blank or 0 version for a browser not chrome " + System.getProperty(SAUCE_BROWSER_PROPERTY));
        }

        capabilities.setCapability("version", ""); // saucelabs requires blank for chrome (latest version)
    } else {
        capabilities.setCapability("version", version); // saucelabs requires blank for chrome (latest version)
    }

    capabilities.setCapability("platform", System.getProperty(SAUCE_PLATFORM_PROPERTY, Platform.UNIX.toString()).replaceAll("_", " "));
    capabilities.setCapability("idle-timeout", Integer.parseInt(System.getProperty(SAUCE_IDLE_TIMEOUT_SECONDS_PROPERTY, "180")));
    capabilities.setCapability("max-duration", Integer.parseInt(System.getProperty(SAUCE_MAX_DURATION_SECONDS_PROPERTY, "600")));
    capabilities.setCapability("name",  className + "." + testName + "-" + AutomatedFunctionalTestUtils.DTS);
    capabilities.setCapability("disable-popup-handler", System.getProperty(SAUCE_POPUP_PROPERTY, "false"));
    capabilities.setCapability("public", System.getProperty(SAUCE_SHARE_PROPERTY, "public restricted"));

    System.out.println("Requesting Saucelabs RemoteWebDriver with DesiredCapabilities of " + capabilities.toString());

    this.driver = new RemoteWebDriver(
            new URL("http://" + authentication.getUsername() + ":" + authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub"),
            capabilities);
    this.sessionId = ((RemoteWebDriver)driver).getSessionId().toString();

    System.out.println("SauceLabs job can be viewed at https://saucelabs.com/jobs/" + this.sessionId);
}
 
Example 19
Source File: ChromeWebDriverProxyTest.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
public ChromeWebDriverProxyTest() {
    super(ChromeWebDriverProxy.class, DesiredCapabilities.chrome());
}
 
Example 20
Source File: ChromeCapabilitiesFactory.java    From seleniumtestsframework with Apache License 2.0 4 votes vote down vote up
public DesiredCapabilities createCapabilities(final DriverConfig webDriverConfig) {

        DesiredCapabilities capability = null;
        capability = DesiredCapabilities.chrome();
        capability.setBrowserName(DesiredCapabilities.chrome().getBrowserName());

        ChromeOptions options = new ChromeOptions();
        if (webDriverConfig.getUserAgentOverride() != null) {
            options.addArguments("--user-agent=" + webDriverConfig.getUserAgentOverride());
        }

        capability.setCapability(ChromeOptions.CAPABILITY, options);

        if (webDriverConfig.isEnableJavascript()) {
            capability.setJavascriptEnabled(true);
        } else {
            capability.setJavascriptEnabled(false);
        }

        capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

        if (webDriverConfig.getBrowserVersion() != null) {
            capability.setVersion(webDriverConfig.getBrowserVersion());
        }

        if (webDriverConfig.getWebPlatform() != null) {
            capability.setPlatform(webDriverConfig.getWebPlatform());
        }

        if (webDriverConfig.getProxyHost() != null) {
            Proxy proxy = webDriverConfig.getProxy();
            capability.setCapability(CapabilityType.PROXY, proxy);
        }

        if (webDriverConfig.getChromeBinPath() != null) {
            capability.setCapability("chrome.binary", webDriverConfig.getChromeBinPath());
        }

        // Set ChromeDriver for local mode
        if (webDriverConfig.getMode() == DriverMode.LOCAL) {
            String chromeDriverPath = webDriverConfig.getChromeDriverPath();
            if (chromeDriverPath == null) {
                try {
                    if (System.getenv("webdriver.chrome.driver") != null) {
                        System.out.println("get Chrome driver from property:"
                                + System.getenv("webdriver.chrome.driver"));
                        System.setProperty("webdriver.chrome.driver", System.getenv("webdriver.chrome.driver"));
                    } else {
                        handleExtractResources();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } else {
                System.setProperty("webdriver.chrome.driver", chromeDriverPath);
            }
        }

        return capability;
    }