当前位置: 首页>>代码示例>>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;未经允许,请勿转载。