Search in sources :

Example 21 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project selenium_java by sergueik.

the class XMLHttpRequestAsyncTest method beforeSuite.

// common code
@BeforeSuite
@SuppressWarnings("deprecation")
public void beforeSuite() throws Exception {
    getOsName();
    if (browser.equals("chrome")) {
        System.setProperty("webdriver.chrome.driver", (new File("c:/java/selenium/chromedriver.exe")).getAbsolutePath());
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        Map<String, Object> chromePrefs = new HashMap<>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        String downloadFilepath = System.getProperty("user.dir") + System.getProperty("file.separator") + "target" + System.getProperty("file.separator");
        chromePrefs.put("download.default_directory", downloadFilepath);
        chromePrefs.put("enableNetwork", "true");
        options.setExperimentalOption("prefs", chromePrefs);
        for (String optionAgrument : (new String[] { "allow-running-insecure-content", "allow-insecure-localhost", "enable-local-file-accesses", "disable-notifications", /* "start-maximized" , */
        "browser.download.folderList=2", "--browser.helperApps.neverAsk.saveToDisk=image/jpg,text/csv,text/xml,application/xml,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/excel,application/pdf", String.format("browser.download.dir=%s", downloadFilepath) /* "user-data-dir=/path/to/your/custom/profile"  , */
        })) {
            options.addArguments(optionAgrument);
        }
        // options for headless
        /*
			for (String optionAgrument : (new String[] { "headless",
					"window-size=1200x600", })) {
				options.addArguments(optionAgrument);
			}
			*/
        capabilities.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        driver = new ChromeDriver(capabilities);
    } else if (browser.equals("firefox")) {
        System.setProperty("webdriver.gecko.driver", osName.toLowerCase().startsWith("windows") ? new File("c:/java/selenium/geckodriver.exe").getAbsolutePath() : "/tmp/geckodriver");
        System.setProperty("webdriver.firefox.bin", osName.toLowerCase().startsWith("windows") ? new File("c:/Program Files (x86)/Mozilla Firefox/firefox.exe").getAbsolutePath() : "/usr/bin/firefox");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        // use legacy FirefoxDriver
        capabilities.setCapability("marionette", false);
        // http://www.programcreek.com/java-api-examples/index.php?api=org.openqa.selenium.firefox.FirefoxProfile
        capabilities.setCapability("locationContextEnabled", false);
        capabilities.setCapability("acceptSslCerts", true);
        capabilities.setCapability("elementScrollBehavior", 1);
        FirefoxProfile profile = new FirefoxProfile();
        profile.setAcceptUntrustedCertificates(true);
        profile.setAssumeUntrustedCertificateIssuer(true);
        profile.setEnableNativeEvents(false);
        System.out.println(System.getProperty("user.dir"));
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);
        try {
            driver = new FirefoxDriver(capabilities);
        } catch (WebDriverException e) {
            e.printStackTrace();
            throw new RuntimeException("Cannot initialize Firefox driver");
        }
    }
    actions = new Actions(driver);
    /*
		System.setProperty("webdriver.chrome.driver",
				"c:/java/selenium/chromedriver.exe");
		driver = new ChromeDriver();
		*/
    driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
    wait = new WebDriverWait(driver, flexibleWait);
    wait.pollingEvery(pollingInterval, TimeUnit.MILLISECONDS);
    actions = new Actions(driver);
    // Declare JavascriptExecutor
    js = (JavascriptExecutor) driver;
}
Also used : HashMap(java.util.HashMap) Actions(org.openqa.selenium.interactions.Actions) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File) WebDriverException(org.openqa.selenium.WebDriverException) BeforeSuite(org.testng.annotations.BeforeSuite)

Example 22 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project selenium_java by sergueik.

the class HeadlessChromeProvider method get.

@Override
public ChromeDriver get() {
    if (config == null) {
        config = ConfigLoader.load();
    }
    getOsName();
    if (config.hasPath("webdriver.chrome.driver")) {
        setProperty("webdriver.chrome.driver", config.getString("webdriver.chrome.driver"));
    } else {
        try {
            final File tempDriver = osName.toLowerCase().startsWith("windows") ? new File("c:/java/selenium/chromedriver.exe") : new File("/tmp/headless_chromedriver");
            if (!tempDriver.exists()) {
                copyInputStreamToFile(// TODO: check if resource exist
                getResource(this.getClass(), osName.toLowerCase().startsWith("windows") ? "chromedriver_win64" : "chromedriver_linux64").openStream(), tempDriver);
            }
            tempDriver.setExecutable(true);
            setProperty("webdriver.chrome.driver", tempDriver.getPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    final ChromeOptions chromeOptions = new ChromeOptions();
    final String binary = config.hasPath("webdriver.chrome.binary") ? config.getString("webdriver.chrome.binary") : osName.toLowerCase().startsWith("windows") ? (new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")).getAbsolutePath() : "/usr/bin/google-chrome";
    chromeOptions.setBinary(binary);
    final String windowSize;
    if (config.hasPath("chrome.window.size")) {
        windowSize = config.getString("chrome.window.size");
    } else {
        windowSize = "1920,1200";
    }
    final boolean useHeadless = !config.hasPath("chrome.headless") || config.hasPath("chrome.headless") && config.getBoolean("chrome.headless");
    if (useHeadless) {
        chromeOptions.addArguments("--headless", "--disable-gpu", "--incognito", "--whitelisted-ips=''", "window-size=" + windowSize);
    } else {
        LOG.warn("Will not use headless mode.");
        chromeOptions.addArguments("--incognito", "window-size=" + windowSize);
    }
    final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CAPABILITY, chromeOptions);
    LOG.info("Providing chromedriver from {} for {}.", getProperty("webdriver.chrome.driver"), binary);
    return new ChromeDriver(capabilities);
}
Also used : DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) IOException(java.io.IOException) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File) FileUtils.copyInputStreamToFile(org.apache.commons.io.FileUtils.copyInputStreamToFile)

Example 23 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project selenium_java by sergueik.

the class HeadlessChromeDriverTest method testScreenshot.

@Ignore
@Test
public void testScreenshot() throws Exception {
    final ChromeDriver driver = headlessChromeDriver.getDriver();
    driver.get("https://reinvent-software.de");
    final File pngFile = new File("/tmp/screenshot.png");
    headlessChromeDriver.screenshot(pngFile);
    assertTrue(pngFile.exists());
    assertThat(pngFile.length(), is(greaterThanOrEqualTo(500000L)));
}
Also used : ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) HeadlessChromeDriver(software.reinvent.headless.chrome.HeadlessChromeDriver) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 24 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project selenium_java by sergueik.

the class HeadlessChromeDriverTest method testElement.

@Test
public void testElement() throws Exception {
    final ChromeDriver driver = headlessChromeDriver.getDriver();
    driver.get("https://habrahabr.ru/");
    final String text = driver.findElementsByCssSelector("div.main-navbar__section svg").get(0).getAttribute("class");
    // System.err.println("Class: " + text);
    assertTrue(text.matches(".*icon-svg_logo-habrahabr.*"));
}
Also used : ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) HeadlessChromeDriver(software.reinvent.headless.chrome.HeadlessChromeDriver) Test(org.junit.Test)

Example 25 with ChromeDriver

use of org.openqa.selenium.chrome.ChromeDriver in project selenium_java by sergueik.

the class MinimalTest method setup.

@BeforeClass
public static void setup() throws IOException {
    getOsName();
    System.setProperty("webdriver.chrome.driver", osName.toLowerCase().startsWith("windows") ? (new File("c:/java/selenium/chromedriver.exe")).getAbsolutePath() : "/home/vagrant/chromedriver");
    ChromeOptions options = new ChromeOptions();
    options.setBinary(osName.toLowerCase().startsWith("windows") ? (new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")).getAbsolutePath() : "/usr/bin/google-chrome");
    options.addArguments("headless");
    options.addArguments("disable-gpu");
    driver = new ChromeDriver(options);
}
Also used : ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Aggregations

ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)98 ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)47 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)39 File (java.io.File)33 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)27 WebDriver (org.openqa.selenium.WebDriver)25 HashMap (java.util.HashMap)24 Before (org.junit.Before)15 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)13 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)13 URL (java.net.URL)12 InternetExplorerDriver (org.openqa.selenium.ie.InternetExplorerDriver)12 Test (org.junit.Test)10 ChromeDriverService (org.openqa.selenium.chrome.ChromeDriverService)10 FirefoxProfile (org.openqa.selenium.firefox.FirefoxProfile)10 HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)9 Actions (org.openqa.selenium.interactions.Actions)9 PhantomJSDriver (org.openqa.selenium.phantomjs.PhantomJSDriver)9 BeforeClass (org.junit.BeforeClass)7 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)7