當前位置: 首頁>>代碼示例>>Java>>正文


Java ChromeDriver類代碼示例

本文整理匯總了Java中org.openqa.selenium.chrome.ChromeDriver的典型用法代碼示例。如果您正苦於以下問題:Java ChromeDriver類的具體用法?Java ChromeDriver怎麽用?Java ChromeDriver使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ChromeDriver類屬於org.openqa.selenium.chrome包,在下文中一共展示了ChromeDriver類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getDriver

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
public static WebDriver getDriver() {

        String browser = System.getenv("BROWSER");
        if (browser == null) {
            ChromeDriverManager.getInstance().setup();
            return new ChromeDriver();
        }
        switch (browser)
        {
            case "IE":
                InternetExplorerDriverManager.getInstance().setup();
                return new InternetExplorerDriver();
            case "FIREFOX":
                FirefoxDriverManager.getInstance().setup();
                return new FirefoxDriver();
            default:
                ChromeDriverManager.getInstance().setup();
                return new ChromeDriver();

        }
    }
 
開發者ID:getgauge-examples,項目名稱:java-maven-selenium,代碼行數:22,代碼來源:DriverFactory.java

示例2: siteUp

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
@BeforeMethod
public void siteUp () {

	final String exe = "chromedriver.exe";
	final String path = getClass ().getClassLoader ()
		.getResource (exe)
		.getPath ();
	final String webSite = "http://www.naukri.com";
	final String binaryPath = "C:\\Users\\DELL\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
	
	System.setProperty("webdriver.chrome.driver", path);
	ChromeOptions chromeOpt= new ChromeOptions();
	chromeOpt.setBinary(binaryPath);
	
	driver = new ChromeDriver (chromeOpt);
	driver.get(webSite);
	driver.manage ().timeouts ().implicitlyWait (10, TimeUnit.SECONDS);
	driver.manage().window().maximize();
	windowHandling ();
}
 
開發者ID:mfaisalkhatri,項目名稱:NaukriSite,代碼行數:21,代碼來源:Setup.java

示例3: createDriver

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
@Override
public WebDriver createDriver(DesiredCapabilities capabilities) {
    Map<String, Object> preferences = new Hashtable<>();
    preferences.put("profile.default_content_settings.popups", 0);
    preferences.put("download.prompt_for_download", "false");
    String downloadsPath = System.getProperty("user.home") + "/Downloads";
    preferences.put("download.default_directory", loadSystemPropertyOrDefault("fileDownloadPath", downloadsPath));
    preferences.put("plugins.plugins_disabled", new String[]{
            "Adobe Flash Player", "Chrome PDF Viewer"});
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", preferences);

    capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    return new ChromeDriver(capabilities);
}
 
開發者ID:alfa-laboratory,項目名稱:akita-testing-template,代碼行數:18,代碼來源:AkitaChromeDriverProvider.java

示例4: setUp

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
private ChromeDriver setUp(Properties properties) {
    System.setProperty("webdriver.chrome.driver", properties.getProperty("webdriver.chrome.driver"));
    String binaryPath = properties.getProperty(CHROME_DRIVER_BINARY_PATH);

    if (binaryPath == null) {
        throw new RuntimeException("Missing property : " + CHROME_DRIVER_BINARY_PATH);
    }

    Map<String, Object> prefs = new HashMap<>();
    prefs.put("profile.default_content_setting_values.notifications", 2);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    options.setBinary(binaryPath);
    options.addArguments("--headless");
    options.addArguments("--user-agent=" + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");
    return new ChromeDriver(options);
}
 
開發者ID:maxcleme,項目名稱:f4f-tts,代碼行數:18,代碼來源:ScrapperTtsUpdater.java

示例5: getScreenshot

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
/**
 * 對當前對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();
}
 
開發者ID:fengzhizi715,項目名稱:PicCrawler,代碼行數:23,代碼來源:Utils.java

示例6: getDriverUsingSwitch

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
public static WebDriver getDriverUsingSwitch() {
	final String browser = System.getProperty("browser");

	if (browser == null || browser.isEmpty()) {
		throw new IllegalStateException("'browser' property is missing!");
	}

	switch (browser) {
		case CHROME:
			return new ChromeDriver();
		case FIREFOX:
			return new FirefoxDriver();
		default:
			throw new IllegalArgumentException(browser + " browser is not supported!");
	}
}
 
開發者ID:sskorol,項目名稱:selenium-camp-17,代碼行數:17,代碼來源:Java7WebDriverFactory.java

示例7: createDriver

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
/**
 * Создание instance google chrome эмулирующего работу на мобильном устройстве (по умолчанию nexus 5)
 * Мобильное устройство может быть задано через системные переменные
 *
 * @param capabilities настройки Chrome браузера
 * @return возвращает новый instance Chrome драйера
 */

@Override
public WebDriver createDriver(DesiredCapabilities capabilities) {
    log.info("---------------run CustomMobileDriver---------------------");
    String mobileDeviceName = loadSystemPropertyOrDefault("device", "Nexus 5");
    Map<String, String> mobileEmulation = new HashMap<>();
    mobileEmulation.put("deviceName", mobileDeviceName);

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

    DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
    desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    desiredCapabilities.setBrowserName(desiredCapabilities.chrome().getBrowserName());
    return new ChromeDriver(desiredCapabilities);
}
 
開發者ID:alfa-laboratory,項目名稱:akita,代碼行數:24,代碼來源:MobileChrome.java

示例8: chrome

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
private WebDriver chrome(Settings settings, DesiredCapabilities customDesiredCapabilities) {
    ChromeDriverManager.getInstance().setup();
    DesiredCapabilities desiredCapabilities = getChromeDesiredCapabilities(settings);
    if (!customDesiredCapabilities.asMap().isEmpty()) {
        desiredCapabilities.merge(customDesiredCapabilities);
    }
    return new ChromeDriver(desiredCapabilities);
}
 
開發者ID:WileyLabs,項目名稱:teasy,代碼行數:9,代碼來源:SeleniumTestExecutionListener.java

示例9: chrome_config_test

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
@Test
@NeedRestartDriver
public void chrome_config_test() {
    openPage("main.html", BasePage.class);
    WebDriver webDriver = ((FramesTransparentWebDriver) SeleniumHolder.getWebDriver()).getWrappedDriver();

    Assert.assertTrue(webDriver instanceof ChromeDriver);

    Assert.assertEquals(((RemoteWebDriver) webDriver).getCapabilities().getCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR), UnexpectedAlertBehaviour.IGNORE.toString());
}
 
開發者ID:WileyLabs,項目名稱:teasy,代碼行數:11,代碼來源:ChromeConfigTest.java

示例10: getObject

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
@Override
public ChromeDriver getObject() throws BeansException {
	if (properties.getChrome().isEnabled()) {
		try {
			return new ChromeDriver(chromeDriverService);
		} catch (IllegalStateException e) {
			e.printStackTrace();
			// swallow the exception
		}
	}
	return null;
}
 
開發者ID:PacktPublishing,項目名稱:Learning-Spring-Boot-2.0-Second-Edition,代碼行數:13,代碼來源:ChromeDriverFactory.java

示例11: testWithMockedChrome

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
@Test
public void testWithMockedChrome() {
	load(new Class[]{},
		"com.greglturnquist.webdriver.firefox.enabled:false",
		"com.greglturnquist.webdriver.safari.enabled:false");
	WebDriver driver = context.getBean(WebDriver.class);
	assertThat(ClassUtils.isAssignable(TakesScreenshot.class,
		driver.getClass())).isTrue();
	assertThat(ClassUtils.isAssignable(ChromeDriver.class,
		driver.getClass())).isTrue();
}
 
開發者ID:PacktPublishing,項目名稱:Learning-Spring-Boot-2.0-Second-Edition,代碼行數:12,代碼來源:WebDriverAutoConfigurationTests.java

示例12: setUp

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
@BeforeClass
public static void setUp() throws IOException {
	System.setProperty("webdriver.chrome.driver",
		"ext/chromedriver");
	service = createDefaultService();
	driver = new ChromeDriver(service);
	Path testResults = Paths.get("build", "test-results");
	if (!Files.exists(testResults)) {
		Files.createDirectory(testResults);
	}
}
 
開發者ID:PacktPublishing,項目名稱:Learning-Spring-Boot-2.0-Second-Edition,代碼行數:12,代碼來源:EndToEndTests.java

示例13: main

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
public static void main(String[] args) throws Exception{
    //配置ChromeDiver
    System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver.exe");
    //開啟新WebDriver進程
    WebDriver webDriver = new ChromeDriver();
    //全局隱式等待
    webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    //設定網址
    webDriver.get("https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F");
    //顯示等待控製對象
    WebDriverWait webDriverWait=new WebDriverWait(webDriver,10);
    //等待輸入框可用後輸入賬號密碼
    webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginName"))).sendKeys(args[0]);
    webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginPassword"))).sendKeys(args[1]);
    //點擊登錄
    webDriver.findElement(By.id("loginAction")).click();
    //等待2秒用於頁麵加載,保證Cookie響應全部獲取。
    sleep(2000);
    //獲取Cookie並打印
    Set<Cookie> cookies=webDriver.manage().getCookies();
    Iterator iterator=cookies.iterator();
    while (iterator.hasNext()){
        System.out.println(iterator.next().toString());
    }
    //關閉WebDriver,否則並不自動關閉
    webDriver.close();
}
 
開發者ID:CieloSun,項目名稱:FashionSpider,代碼行數:28,代碼來源:WeiboLoginAndGetCookie.java

示例14: testSelenium

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
public static void testSelenium() throws Exception {
    System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver.exe");
    WebDriver webDriver = new ChromeDriver();
    webDriver.get("http://huaban.com/");
    Thread.sleep(5000);
    WebElement webElement = webDriver.findElement(By.xpath("/html"));
    System.out.println(webElement.getAttribute("outerHTML"));
    webDriver.close();
}
 
開發者ID:CieloSun,項目名稱:FashionSpider,代碼行數:10,代碼來源:Test.java

示例15: mockProvider

import org.openqa.selenium.chrome.ChromeDriver; //導入依賴的package包/類
private void mockProvider(final WebDriverProvider provider, final int duplicatesAmount,
                          final BeforeMethodListener listener) {
    final WebDriverProvider spyProvider = spy(provider);
    final WebDriver mockDriver = mock(ChromeDriver.class);
    doReturn(mockDriver).when(spyProvider).createDriver(any(), any());
    final List<WebDriverProvider> providers = duplicatesAmount > 1
            ? IntStreamEx.range(0, duplicatesAmount).mapToObj(i -> provider).toList()
            : singletonList(spyProvider);
    doReturn(providers).when(listener).getWebDriverProviders();
}
 
開發者ID:sskorol,項目名稱:webdriver-supplier,代碼行數:11,代碼來源:ListenerTests.java


注:本文中的org.openqa.selenium.chrome.ChromeDriver類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。