Java Code Examples for org.openqa.selenium.chrome.ChromeDriver

The following examples show how to use org.openqa.selenium.chrome.ChromeDriver. These examples are extracted from open source projects. 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 Project: shadow-automation-selenium   Source File: Shadow.java    License: Apache License 2.0 6 votes vote down vote up
public Shadow(WebDriver driver) {
	
	if (driver instanceof ChromeDriver) {
		sessionId  = ((ChromeDriver)driver).getSessionId();
		chromeDriver = (ChromeDriver)driver;
	} else if (driver instanceof FirefoxDriver) {
		sessionId  = ((FirefoxDriver)driver).getSessionId();
		firfoxDriver = (FirefoxDriver)driver;
	} else if (driver instanceof InternetExplorerDriver) {
		sessionId  = ((InternetExplorerDriver)driver).getSessionId();
		ieDriver = (InternetExplorerDriver)driver;
	} else if (driver instanceof RemoteWebDriver) {
		sessionId  = ((RemoteWebDriver)driver).getSessionId();
		remoteWebDriver = (RemoteWebDriver)driver;
	} 
	this.driver = driver;
}
 
Example 2
Source Project: selenium-jupiter   Source File: ChromeDriverHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void resolve() {
    try {
        Optional<Object> testInstance = context.getTestInstance();
        Optional<Capabilities> capabilities = annotationsReader
                .getCapabilities(parameter, testInstance);
        ChromeOptions chromeOptions = (ChromeOptions) getOptions(parameter,
                testInstance);

        if (chromeOptions.asMap().get(CAPABILITY).toString().toLowerCase()
                .contains("chromium")) {
            WebDriverManager.chromiumdriver().setup();
        }

        if (capabilities.isPresent()) {
            chromeOptions.merge(capabilities.get());
        }

        object = new ChromeDriver(chromeOptions);
    } catch (Exception e) {
        handleException(e);
    }
}
 
Example 3
Source Project: JYTB   Source File: BotWorker.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void setChromeDriver() {
        ChromeOptions options = new ChromeOptions();
        List<String> chromeOptions = new ArrayList<>();
        LoggingPreferences logPrefs = new LoggingPreferences();

        chromeOptions.add(String.format("--proxy-server=%s", proxies.getCurrentProxyModel().getIp()));
        chromeOptions.add(String.format("--user-agent=%s", userAgent.randomUA()));
        chromeOptions.add("--mute-audio");

        logPrefs.enable(LogType.BROWSER, Level.ALL);
        logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
        options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

        options.addArguments(chromeOptions);
        options.setBinary(this.driverLocation);
        options.setHeadless(true);
//        options.setProxy(this.proxies.getCurrentProxy());
        options.setCapability("proxy", this.proxies.getCurrentProxy());

        this.webDriver = new ChromeDriver(options);
        Log.WINFO(this.workerName, this.workerColor, "Chrome Driver Set.");
    }
 
Example 4
public static void main(String... args){

        System.setProperty("webdriver.chrome.driver",
                "./src/test/resources/drivers/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {
            EventFiringWebDriver eventFiringDriver = new
                    EventFiringWebDriver(driver);
            IAmTheEventListener eventListener = new IAmTheEventListener();
            eventFiringDriver.register(eventListener);
            eventFiringDriver.get("http://www.google.com");
            eventFiringDriver.get("http://www.facebook.com");
            eventFiringDriver.navigate().back();
        } finally {
            driver.close();
            driver.quit();
        }
    }
 
Example 5
public static void main(String... args){

        System.setProperty("webdriver.chrome.driver",
                "./src/test/resources/drivers/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {
            EventFiringWebDriver eventFiringDriver = new
                    EventFiringWebDriver(driver);
            IAmTheEventListener eventListener = new IAmTheEventListener();
            IAmTheEventListener2 eventListener2 = new
                    IAmTheEventListener2();
            eventFiringDriver.register(eventListener);
            eventFiringDriver.register(eventListener2);
            eventFiringDriver.get("http://www.google.com");
            eventFiringDriver.get("http://www.facebook.com");
            eventFiringDriver.navigate().back();
        } finally {
            driver.close();
            driver.quit();
        }
    }
 
Example 6
@BeforeClass
	public static void init(){
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
		driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
//		try {
//		GraphicsConfiguration gc = GraphicsEnvironment
//			      .getLocalGraphicsEnvironment()
//			      .getDefaultScreenDevice()
//			      .getDefaultConfiguration();
//			screenRecorder = new ScreenRecorder(gc);
//			screenRecorder.start();
//			driver.get("http://localhost:8080/q2r/login");
//			Thread.sleep(5000);
//			driver.findElement(By.id("username")).sendKeys("admin");
//			driver.findElement(By.id("password")).sendKeys("admin");
//			driver.findElement(By.id("loginButton")).click();
//			Thread.sleep(5000);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
	}
 
Example 7
static Stream<Arguments> forcedTestProvider() {
    return Stream.of(
            Arguments.of(AppiumDriverHandler.class,
                    ForcedAppiumJupiterTest.class, AppiumDriver.class,
                    "appiumNoCapabilitiesTest"),
            Arguments.of(AppiumDriverHandler.class,
                    ForcedAppiumJupiterTest.class, AppiumDriver.class,
                    "appiumWithCapabilitiesTest"),
            Arguments.of(ChromeDriverHandler.class,
                    ForcedBadChromeJupiterTest.class, ChromeDriver.class,
                    "chromeTest"),
            Arguments.of(FirefoxDriverHandler.class,
                    ForcedBadFirefoxJupiterTest.class, FirefoxDriver.class,
                    "firefoxTest"),
            Arguments.of(EdgeDriverHandler.class,
                    ForcedEdgeJupiterTest.class, EdgeDriver.class,
                    "edgeTest"),
            Arguments.of(OperaDriverHandler.class,
                    ForcedOperaJupiterTest.class, OperaDriver.class,
                    "operaTest"),
            Arguments.of(SafariDriverHandler.class,
                    ForcedSafariJupiterTest.class, SafariDriver.class,
                    "safariTest"));
}
 
Example 8
public static WebDriver getChromeDriver() {

        /*
            Need to have Chrome (eg version 53.x) and the Chrome Driver (eg 2.24),
            whose executable should be saved directly under your home directory

            see https://sites.google.com/a/chromium.org/chromedriver/getting-started
         */

        boolean isOk = setupDriverExecutable("chromedriver", "webdriver.chrome.driver");
        if(! isOk){
            return null;
        }

        return new ChromeDriver();
    }
 
Example 9
Source Project: query2report   Source File: SalesDemo.java    License: GNU General Public License v3.0 6 votes vote down vote up
@BeforeClass
public static void init(){
	driver = new ChromeDriver();
	driver.manage().window().maximize();
	driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
	driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
	try{
		driver.get("http://localhost:8080/q2r/login.html");
		Thread.sleep(1000);
		driver.findElement(By.id("username")).sendKeys("admin");
		driver.findElement(By.id("password")).sendKeys("admin");
		driver.findElement(By.id("loginButton")).click();
		Thread.sleep(1000);
	}catch(Exception e){
		e.printStackTrace();
	}
}
 
Example 10
Source Project: htmlunit   Source File: HtmlFileInputTest.java    License: Apache License 2.0 6 votes vote down vote up
private void contentType(final String extension) throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "upload1");

    final File tmpFile = File.createTempFile("htmlunit-test", "." + extension);
    try {
        String path = tmpFile.getAbsolutePath();
        if (driver instanceof InternetExplorerDriver || driver instanceof ChromeDriver) {
            path = path.substring(path.indexOf('/') + 1).replace('/', '\\');
        }
        driver.findElement(By.name("myInput")).sendKeys(path);
        driver.findElement(By.id("mySubmit")).click();
    }
    finally {
        assertTrue(tmpFile.delete());
    }

    final String pageSource = driver.getPageSource();
    assertTrue(pageSource, pageSource.contains(getExpectedAlerts()[0]));
    assertFalse(pageSource, pageSource.contains(getExpectedAlerts()[1]));
}
 
Example 11
Source Project: qa-automation-samples   Source File: DriverFactory.java    License: MIT License 6 votes vote down vote up
public static WebDriver createDriver(String mvnParameter){
    if (mvnParameter == null){
        System.setProperty(Browsers.CHROME_MAC.getBrowserType(),
        		OperationSystems.MAC_OS_X.getDriversPath().concat(Browsers.CHROME_MAC.getExecutable()));
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-fullscreen", "--disable-gpu");
        return new ChromeDriver(options);
    }

    try {
        String soName = getSoName();
        String browserType = getBrowserTypeFromParameter(mvnParameter);
        System.setProperty(browserType, getDriversPath(soName).concat(getFullExecutableNameFromParameter(mvnParameter)));
    }catch (Exception e){
        System.out.println("ERROR: Please select one of the valid browsers for the test.");
        Browsers.showAvaliableBrowsersOptions();
        System.exit(1);
    }
		
    return Browsers.valueOf(mvnParameter).createDriverInstance();
}
 
Example 12
Source Project: demo-java   Source File: LocalExecutionTest.java    License: MIT License 6 votes vote down vote up
@Test
public void localExecution() {
    // Options:
    //
    // 1. Specify location of driver
    // System.setProperty("webdriver.chrome.driver", "lib/drivers/chromedriver");
    //
    // 2. Add driver to PATH ENV
    //
    // 3. Use Driver manager:
    WebDriverManager.chromedriver().setup();

    // Start session (opens browser)
    WebDriver driver = new ChromeDriver();

    // Quit session (closes browser)
    driver.quit();
}
 
Example 13
Source Project: PicCrawler   Source File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 对当前对url进行截屏,一方面可以做调试使用看能否进入到该页面,另一方面截屏的图片未来可以做ocr使用
 * @param url
 */
public static void getScreenshot(String url) {

    //启动chrome实例
    WebDriver driver = new ChromeDriver();
    driver.get(url);
    //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
    File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    //利用IOUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。

    try {
        IOUtils.copyFile(srcFile, new File("screenshot.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    //关闭浏览器
    driver.quit();
}
 
Example 14
@BeforeClass
public static void init(){
	driver = new ChromeDriver();
	driver.manage().window().maximize();
	driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
	driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
	try{
		driver.get("http://localhost:8080/q2r/login.html");
		Thread.sleep(1000);
		driver.findElement(By.id("username")).sendKeys("admin");
		driver.findElement(By.id("password")).sendKeys("admin");
		driver.findElement(By.id("loginButton")).click();
		Thread.sleep(1000);
	}catch(Exception e){
		e.printStackTrace();
	}
}
 
Example 15
@BeforeClass
public static void init(){
	driver = new ChromeDriver();
	driver.manage().window().maximize();
	driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
	driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
	try{
		driver.get("http://localhost:8080/q2r/login.html");
		Thread.sleep(1000);
		driver.findElement(By.id("username")).sendKeys("admin");
		driver.findElement(By.id("password")).sendKeys("admin");
		driver.findElement(By.id("loginButton")).click();
		Thread.sleep(1000);
	}catch(Exception e){
		e.printStackTrace();
	}
}
 
Example 16
Source Project: org.hl7.fhir.core   Source File: ValidatorGuiTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Page boots correctly, and displays index.html")
public void UI_contains_correct_heading() throws IOException {
  ValidatorGui.start(new CliContext(), null, false);
  WebDriverManager.chromedriver().setup();
  ChromeOptions options = new ChromeOptions();
  options.addArguments("--headless");
  options.addArguments("--disable-gpu");
  WebDriver driver = new ChromeDriver(options);
  driver.get("http://localhost:" + ValidatorGui.getPort() + "/home");

  Assertions.assertTrue(driver.getPageSource().contains(HTML_TITLE_TAG));
  driver.quit();
  ValidatorGui.stop();
}
 
Example 17
@BeforeClass
public static void init(){
	driver = new ChromeDriver();
	driver.manage().window().maximize();
	driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
	driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
}
 
Example 18
Source Project: healenium-web   Source File: HealTurnedOffTest.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void createDriver() {
    ChromeOptions options = new ChromeOptions();
    options.setHeadless(true);
    WebDriver delegate = new ChromeDriver(options);
    Config config = ConfigFactory.load("test.conf");
    SelfHealingEngine engine = new SelfHealingEngine(delegate, config);
    driver = SelfHealingDriver.create(engine);
}
 
Example 19
Source Project: coteafs-selenium   Source File: Browser.java    License: Apache License 2.0 5 votes vote down vote up
private static WebDriver setupChromeDriver() throws MalformedURLException {
    LOG.i("Setting up Chrome driver...");
    System.setProperty("webdriver.chrome.silentOutput", "true");
    setupDriver(chromedriver());
    final ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--dns-prefetch-disable");
    if (appSetting().isHeadlessMode()) {
        chromeOptions.addArguments("--headless");
    }
    chromeOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    final ChromeDriverService chromeService = ChromeDriverService.createDefaultService();
    return new ChromeDriver(chromeService, chromeOptions);
}
 
Example 20
Source Project: nifi-registry   Source File: ITRenameBucket.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    baseUrl = "http://localhost:18080/nifi-registry";
    wait = new WebDriverWait(driver, 30);
}
 
Example 21
@BeforeMethod
public void setup() throws IOException {

    System.setProperty("webdriver.chrome.driver",
            "./src/test/resources/drivers/chromedriver");

    driver = new ChromeDriver();
    driver.navigate().to("http://demo-store.seleniumacademy.com/");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("./target/screenshot.png"));

}
 
Example 22
@BeforeMethod
public void setup() throws IOException {
    System.setProperty("webdriver.chrome.driver",
            "./src/test/resources/drivers/chromedriver");
    driver = new ChromeDriver();
   driver.get("http://demo-store.seleniumacademy.com");
}
 
Example 23
@BeforeMethod
public void setup() {

    System.setProperty("webdriver.chrome.driver",
            "./src/test/resources/drivers/chromedriver");
    driver = new ChromeDriver();
    driver.get("http://demo-store.seleniumacademy.com/");

}
 
Example 24
@BeforeMethod
public void setup() {

    System.setProperty("webdriver.chrome.driver",
            "./src/test/resources/drivers/chromedriver");
    driver = new ChromeDriver();
    driver.get("http://demo-store.seleniumacademy.com/");

}
 
Example 25
@BeforeMethod
public void setup() {

    System.setProperty("webdriver.chrome.driver",
            "./src/test/resources/drivers/chromedriver");
    driver = new ChromeDriver();
    driver.get("http://demo-store.seleniumacademy.com/");

}
 
Example 26
@BeforeClass
public void setup() {

    System.setProperty("webdriver.chrome.driver",
            "./src/test/resources/drivers/chromedriver");
    driver = new ChromeDriver();

}
 
Example 27
Source Project: akita   Source File: CustomDriverProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public WebDriver createDriver(DesiredCapabilities capabilities) {
    Configuration.browserSize = String.format("%sx%s", loadSystemPropertyOrDefault(WINDOW_WIDTH, DEFAULT_WIDTH),
            loadSystemPropertyOrDefault(WINDOW_HEIGHT, DEFAULT_HEIGHT));
    String expectedBrowser = loadSystemPropertyOrDefault(BROWSER, CHROME);
    String remoteUrl = loadSystemPropertyOrDefault(REMOTE_URL, LOCAL);
    BlackList blackList = new BlackList();
    boolean isProxyMode = loadSystemPropertyOrDefault(PROXY, false);
    if (isProxyMode) {
        enableProxy(capabilities);
    }

    log.info("remoteUrl=" + remoteUrl + " expectedBrowser= " + expectedBrowser + " BROWSER_VERSION=" + System.getProperty(CapabilityType.BROWSER_VERSION));

    switch (expectedBrowser.toLowerCase()) {
        case (FIREFOX):
            return LOCAL.equalsIgnoreCase(remoteUrl) ? createFirefoxDriver(capabilities) : getRemoteDriver(getFirefoxDriverOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());
        case (MOBILE_DRIVER):
            return LOCAL.equalsIgnoreCase(remoteUrl) ? new ChromeDriver(getMobileChromeOptions(capabilities)) : getRemoteDriver(getMobileChromeOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());
        case (OPERA):
            return LOCAL.equalsIgnoreCase(remoteUrl) ? createOperaDriver(capabilities) : getRemoteDriver(getOperaRemoteDriverOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());
        case (SAFARI):
            return LOCAL.equalsIgnoreCase(remoteUrl) ? createSafariDriver(capabilities) : getRemoteDriver(getSafariDriverOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());
        case (INTERNET_EXPLORER):
            return LOCAL.equalsIgnoreCase(remoteUrl) ? createIEDriver(capabilities) : getRemoteDriver(getIEDriverOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());
        case (IE):
            return LOCAL.equalsIgnoreCase(remoteUrl) ? createIEDriver(capabilities) : getRemoteDriver(getIEDriverOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());
        case (EDGE):
            return LOCAL.equalsIgnoreCase(remoteUrl) ? createEdgeDriver(capabilities) : getRemoteDriver(getEdgeDriverOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());
        default:
            return LOCAL.equalsIgnoreCase(remoteUrl) ? createChromeDriver(capabilities) : getRemoteDriver(getChromeDriverOptions(capabilities), remoteUrl, blackList.getBlacklistEntries());

    }
}
 
Example 28
@BeforeClass
public static void init(){
	driver = new ChromeDriver();
	driver.manage().window().maximize();
	driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
	driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
}
 
Example 29
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 30
@BeforeMethod
public void setup() throws IOException {

    System.setProperty("webdriver.chrome.driver",
            "./src/test/resources/drivers/chromedriver");
    driver = new ChromeDriver();
}