public static DesiredCapabilities of(Browser browser) { if (browser == null) { return null; } DesiredCapabilities caps; switch (browser) { case chrome: caps = DesiredCapabilities.chrome(); break; case ff: caps = DesiredCapabilities.firefox(); // caps.setCapability(FirefoxDriver.MARIONETTE, // // Boolean.getBoolean(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE)); break; case htmlunit: caps = DesiredCapabilities.htmlUnit(); caps.setJavascriptEnabled(false); break; case htmlunit_js: caps = DesiredCapabilities.htmlUnit(); caps.setJavascriptEnabled(true); break; case ie: caps = DesiredCapabilities.internetExplorer(); break; case operablink: caps = DesiredCapabilities.operaBlink(); break; case phantomjs: caps = DesiredCapabilities.phantomjs(); break; case safari: caps = DesiredCapabilities.safari(); break; default: throw new RuntimeException("Cannot determine browser config to use"); } String version = System.getProperty("selenium.browser.version"); if (version != null) { caps.setVersion(version); } caps.setCapability(HAS_NATIVE_EVENTS, Boolean.getBoolean("selenium.browser.native_events")); return caps; }
@Override public void setSystemProperty(Configuration configInstance, Capabilities capabilities) { String brofserName = capabilities.getBrowserName(); if (DesiredCapabilities.chrome().getBrowserName().equals(brofserName)) CHROME.setSystemProperty(configInstance); if (DesiredCapabilities.internetExplorer().getBrowserName().equals(brofserName)) INTERNETEXPLORER.setSystemProperty(configInstance); if (DesiredCapabilities.phantomjs().getBrowserName().equals(brofserName)) PHANTOMJS.setSystemProperty(configInstance); }
private static void initDriver(String browser) { String browserName = System.getProperty("browser", browser); String browserRemoteName = System.getProperty("remote", "local"); if (browserRemoteName.equals("local")) { switch (browserName) { case "chrome": default: driver = new ChromeDriver(); break; case "firefox": driver = new FirefoxDriver(); break; case "ie": driver = new InternetExplorerDriver(); break; case "html": driver = new HtmlUnitDriver(); break; case "phantomjs": driver = new PhantomJSDriver(); break; } } else { DesiredCapabilities caps; switch (browserName) { case "chrome": default: caps = DesiredCapabilities.chrome(); break; case "firefox": caps = DesiredCapabilities.firefox(); break; case "ie": caps = DesiredCapabilities.internetExplorer(); break; case "html": caps = DesiredCapabilities.htmlUnitWithJs(); break; case "phantomjs": caps = DesiredCapabilities.phantomjs(); break; } try { driver = new RemoteWebDriver(new URL(browserRemoteName), caps); } catch (Exception ex) { System.out.println(ex.getMessage()); } } driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); }
{ put(DesiredCapabilities.chrome(), "org.openqa.selenium.chrome.ChromeDriver"); put(DesiredCapabilities.firefox(), "org.openqa.selenium.firefox.FirefoxDriver"); put(DesiredCapabilities.htmlUnit(), "org.openqa.selenium.htmlunit.HtmlUnitDriver"); put( DesiredCapabilities.internetExplorer(), "org.openqa.selenium.ie.InternetExplorerDriver"); put(DesiredCapabilities.opera(), "com.opera.core.systems.OperaDriver"); put(DesiredCapabilities.safari(), "org.openqa.selenium.safari.SafariDriver"); put(DesiredCapabilities.iphone(), "org.openqa.selenium.iphone.IPhoneDriver"); put(DesiredCapabilities.ipad(), "org.openqa.selenium.iphone.IPhoneDriver"); put(DesiredCapabilities.phantomjs(), "org.openqa.selenium.phantomjs.PhantomJSDriver"); }
private WebDriver createWebDriver(TestName testName) throws IOException { String browser = System.getenv("BROWSER"); if (browser == null) browser = "firefox"; browser = browser.toLowerCase(Locale.ENGLISH); switch (browser) { case "firefox": FirefoxProfile profile = new FirefoxProfile(); profile.setPreference(LANGUAGE_SELECTOR, "en"); return new FirefoxDriver(profile); case "ie": case "iexplore": case "iexplorer": return new InternetExplorerDriver(); case "chrome": Map<String, String> prefs = new HashMap<String, String>(); prefs.put(LANGUAGE_SELECTOR, "en"); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("prefs", prefs); return new ChromeDriver(options); case "safari": return new SafariDriver(); case "htmlunit": return new HtmlUnitDriver(true); case "saucelabs": case "saucelabs-firefox": DesiredCapabilities caps = DesiredCapabilities.firefox(); caps.setCapability("version", "29"); caps.setCapability("platform", "Windows 7"); caps.setCapability("name", testName.get()); // if running inside Jenkins, expose build ID String tag = System.getenv("BUILD_TAG"); if (tag != null) caps.setCapability("build", tag); return new SauceLabsConnection().createWebDriver(caps); case "phantomjs": DesiredCapabilities capabilities = DesiredCapabilities.phantomjs(); capabilities.setCapability(LANGUAGE_SELECTOR, "en"); capabilities.setCapability(LANGUAGE_SELECTOR_PHANTOMJS, "en"); return new PhantomJSDriver(capabilities); default: throw new Error("Unrecognized browser type: " + browser); } }
// li.active>span.next public static void main(String[] args) { if (args.length < 3) { System.out.println("please input two paremeter!"); System.exit(0); } String url = args[0]; String phantomJsPath = args[1]; String screenShot = args[2]; DesiredCapabilities cap = DesiredCapabilities.phantomjs(); cap.setCapability( PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] { "--webdriver-loglevel=DEBUG", // "--proxy=org.openqa.grid.selenium.proxy.DefaultRemoteProxy", }); cap.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "zh_CN"); // cap.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Macintosh; Intel Mac OS X // 10.9; rv:25.0) Gecko/20100101 Firefox/25.0"); // "C:\\Users\\Administrator\\Desktop\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe" cap.setCapability("phantomjs.binary.path", phantomJsPath); cap.setJavascriptEnabled(true); driver = new PhantomJSDriver(cap); WebDriverWait wait = null; wait = new WebDriverWait(driver, 60); driver.get(url); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } screenShot((TakesScreenshot) driver, screenShot); }
/** * There is information about supported {@link WebDriver} implementors <br> * - {@link FirefoxDriver}<br> * - {@link ChromeDriver}<br> * - {@link InternetExplorerDriver}<br> * - {@link SafariDriver}<br> * - {@link PhantomJSDriver}<br> * - {@link RemoteWebDriver}<br> * - {@link AndroidDriver}<br> * - {@link IOSDriver}<br> * <br> * - is browser only<br> * - is for mobile applications only<br> * Additional info:<br> * - default {@link Capabilities}<br> * - information about {@link DriverService}<br> * - can {@link WebDriver} be started remotely?<br> * - is {@link WebDriver} require URL of remotely or locally started<br> * server. ({@link SeleniumServer} or Appium Node.js server (see * http://appium.io/slate/en/master/?ruby#appium-design)<br> */ public enum ESupportedDrivers { /** * <b>Required {@link WebDriver} implementor</b>: {@link FirefoxDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: {@link DesiredCapabilities#firefox()}<br> * <br> * <b>{@link DriverService}</b>: none<br> * <br> * <b>Starts</b>: locally<br> * browser only */ FIREFOX( DesiredCapabilities.firefox(), FirefoxDriver.class, true, false, null, null, false, false), /** * <b>Required {@link WebDriver} implementor</b>: {@link ChromeDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: {@link DesiredCapabilities#chrome()}<br> * <br> * <b>{@link DriverService}</b>: {@link ChromeDriverService}<br> * <br> * <b>Starts</b>: locally<br> * browser only */ CHROME( DesiredCapabilities.chrome(), ChromeDriver.class, true, false, EServices.CHROMESERVICE, null, false, false), /** * <b>Required {@link WebDriver} implementor</b>: {@link InternetExplorerDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: {@link DesiredCapabilities#internetExplorer()}<br> * <br> * <b>{@link DriverService}</b>: {@link InternetExplorerDriverService}<br> * <br> * <b>Starts</b>: locally<br> * browser only<br> * <br> * <b>Additionally:</b> Windows only<br> */ INTERNETEXPLORER( DesiredCapabilities.internetExplorer(), InternetExplorerDriver.class, true, false, EServices.IEXPLORERSERVICE, null, false, false), /** * <b>Required {@link WebDriver} implementor</b>: {@link SafariDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: {@link DesiredCapabilities#safari()}<br> * <br> * <b>{@link DriverService}</b>: none<br> * <br> * <b>Starts</b>: locally<br> * browser only */ SAFARI(DesiredCapabilities.safari(), SafariDriver.class, true, false, null, null, false, false), /** * <b>Required {@link WebDriver} implementor</b>: {@link PhantomJSDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: {@link DesiredCapabilities#phantomjs()}<br> * <br> * <b>{@link DriverService}</b>: {@link PhantomJSDriverService}<br> * <br> * <b>Starts</b>: locally<br> * browser only */ PHANTOMJS( DesiredCapabilities.phantomjs(), PhantomJSDriver.class, true, false, EServices.PHANTOMJSSERVICE, null, false, false), /** * <b>Required {@link WebDriver} implementor</b>: {@link AndroidDriver}<br> * <br> * <b>Default {@link Capabilities}</b>: {@link ExtendedDesiredCapabilities#androidChrome()}<br> * <br> * <b>{@link DriverService}</b>: none<br> * <br> * <b>Starts</b>: remotely. It requires {@link URL} of the host where Appium node server is * started, e.g. http://127.0.0.1:4723/wd/hub (local host). Please find information here:<br> * http://appium.io/getting-started.html<br> * browser only */ ANDROID_CHROME( ExtendedDesiredCapabilities.androidChrome(), AndroidDriver.class, true, false, null, null, true, true), /** * <b>Required {@link WebDriver} implementor</b>: {@link AndroidDriver}<br> * <br> * <b>Default {@link Capabilities}</b>: {@link ExtendedDesiredCapabilities#androidBrowser()}<br> * <br> * <b>{@link DriverService}</b>: none<br> * <br> * <b>Starts</b>: remotely. It requires {@link URL} of the host where Appium node server is * started, e.g. http://127.0.0.1:4723/wd/hub (local host). Please find information here:<br> * http://appium.io/getting-started.html<br> * browser only */ ANDROID_BROWSER( ExtendedDesiredCapabilities.androidBrowser(), AndroidDriver.class, true, false, null, null, true, true), /** * <b>Required {@link WebDriver} implementor</b>: {@link AndroidDriver}<br> * <br> * <b>Default {@link Capabilities}</b>: {@link ExtendedDesiredCapabilities#androidChromium()}<br> * <br> * <b>{@link DriverService}</b>: none<br> * <br> * <b>Starts</b>: remotely. It requires {@link URL} of the host where Appium node server is * started, e.g. http://127.0.0.1:4723/wd/hub (local host). Please find information here:<br> * http://appium.io/getting-started.html<br> * browser only */ ANDROID_CHROMIUM( ExtendedDesiredCapabilities.androidChromium(), AndroidDriver.class, true, false, null, null, true, true), /** * <b>Required {@link WebDriver} implementor</b>: {@link IOSDriver}<br> * <br> * <b>Default {@link Capabilities}</b>: {@link ExtendedDesiredCapabilities#iosSafari()}<br> * <br> * <b>{@link DriverService}</b>: none<br> * <br> * <b>Starts</b>: remotely. It requires {@link URL} of the host where Appium node server is * started, e.g. http://127.0.0.1:4723/wd/hub (local host). Please find information here:<br> * http://appium.io/getting-started.html<br> * browser only */ IOS_SAFARI( ExtendedDesiredCapabilities.iosSafari(), IOSDriver.class, true, false, null, null, true, true), /** * <b>Required {@link WebDriver} implementor</b>: {@link RemoteWebDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: {@link DesiredCapabilities#firefox()()}<br> * <br> * <b>Starts</b>: locally and remotely. When it is started locally {@link SeleniumServer} is * started it the same time.<br> * browser only<br> * If it needs to be launched remotely it requires {@link URL} of the host where Appium node * server (if {@link ExtendedDesiredCapabilities#androidChrome()} or {@link * ExtendedDesiredCapabilities#iosSafari()} are used) or {@link SeleniumServer} are started, e.g. * http://127.0.0.1:4723/wd/hub (local host). Information about Appium node server please find * here:<br> * http://appium.io/getting-started.html<br> * Information about {@link SeleniumServer}: https://code.google.com/p/selenium/wiki/Grid2<br> * <br> * <b>{@link DriverService}</b>: optionally. If it is run locally with {@link * DesiredCapabilities#chrome()}, {@link DesiredCapabilities#internetExplorer()} or {@link * DesiredCapabilities#phantomjs()} then {@link ChromeDriverService}, {@link * InternetExplorerDriverService} or {@link PhantomJSDriverService} should be set up respectively * <br> */ REMOTE( DesiredCapabilities.firefox(), RemoteWebDriver.class, true, false, null, new RemoteSeleniumServerLauncher(), true, false) { @Override public void setSystemProperty(Configuration configInstance, Capabilities capabilities) { String brofserName = capabilities.getBrowserName(); if (DesiredCapabilities.chrome().getBrowserName().equals(brofserName)) CHROME.setSystemProperty(configInstance); if (DesiredCapabilities.internetExplorer().getBrowserName().equals(brofserName)) INTERNETEXPLORER.setSystemProperty(configInstance); if (DesiredCapabilities.phantomjs().getBrowserName().equals(brofserName)) PHANTOMJS.setSystemProperty(configInstance); } }, /** * <b>Required {@link WebDriver} implementor</b>: {@link AndroidDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: empty. They should be defined explicitly. Here is an * example: <br> * "deviceName":{<br> * "value":"desired device name e.g. Android Emulator, NEXUS and so on" * <br> * },<br> * "app":{<br> * "value":"absolute path to desired *.apk file"<br> * }<br> * <br> * It is enough for the successful starting. Also see {@link MobileCapabilityType}<br> * <br> * <b>Starts</b>: remotely. It requires {@link URL} of the host where Appium node server is * started, e.g. http://127.0.0.1:4723/wd/hub (local host). Please find information here:<br> * http://appium.io/getting-started.html<br> * mobile apps only */ ANDROID_APP(new DesiredCapabilities(), AndroidDriver.class, false, true, null, null, true, true), /** * <b>Required {@link WebDriver} implementor</b>: {@link AndroidDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: empty. They should be defined explicitly. Here is an * example: <br> * "deviceName":{<br> * "value":"desired device name e.g. Android Emulator, NEXUS and so on" * <br> * },<br> * "app":{<br> * "value":"absolute path to desired *.apk file"<br> * }<br> * <br> * It is enough for the successful starting. Also see {@link MobileCapabilityType}<br> * <br> * <b>Starts</b>: remotely. It requires {@link URL} of the host where Appium node server is * started, e.g. http://127.0.0.1:4723/wd/hub (local host). Please find information here:<br> * http://appium.io/getting-started.html<br> * mobile apps only */ SELENDROID_APP( ExtendedDesiredCapabilities.selendroid(), AndroidDriver.class, false, true, null, null, true, true), /** * <b>Required {@link WebDriver} implementor</b>: {@link IOSDriver} <br> * <br> * <b>Default {@link Capabilities}</b>: empty. They should be defined explicitly. Here is an * example: <br> * "platformVersion":{<br> * "value":"7.1"<br> * },<br> * "deviceName":{<br> * "value":"desired device name e.g. iPhone Simulator"<br> * },<br> * "app":{<br> * "value":"absolute path to desired *.app"<br> * }<br> * <br> * It is enough for the successful starting. Also see {@link MobileCapabilityType}<br> * <br> * <b>Starts</b>: remotely. It requires {@link URL} of the host where Appium node server is * started, e.g. http://127.0.0.1:4723/wd/hub (local host). Please find information here:<br> * http://appium.io/getting-started.html<br> * mobile apps only */ IOS_APP(new DesiredCapabilities(), IOSDriver.class, false, true, null, null, true, true); public static ESupportedDrivers parse(String original) { String parcingStr = original.toUpperCase().trim(); ESupportedDrivers[] values = ESupportedDrivers.values(); for (ESupportedDrivers enumElem : values) if (parcingStr.equals(enumElem.toString())) return enumElem; throw new IllegalArgumentException( "Webdriver with specified name " + original + " is not supported"); } private DesiredCapabilities capabilities; private Class<? extends WebDriver> driverClazz; private EServices service; final ILocalServerLauncher serverLauncher; private final boolean startsRemotely; private final boolean requiresRemoteURL; private final boolean isForBrowser; private final boolean isForMobileApp; private ESupportedDrivers( DesiredCapabilities capabilities, Class<? extends WebDriver> driverClazz, boolean isForBrowser, boolean isForMobileApp, EServices sevice, ILocalServerLauncher serverLauncher, boolean startsRemotely, boolean requiresRemoteURL) { this.capabilities = capabilities; this.driverClazz = driverClazz; this.service = sevice; this.serverLauncher = serverLauncher; this.startsRemotely = startsRemotely; this.requiresRemoteURL = requiresRemoteURL; this.isForBrowser = isForBrowser; this.isForMobileApp = isForMobileApp; } /** @return {@link Capabilities} Default capabilities of given {@link WebDriver} implementor */ public DesiredCapabilities getDefaultCapabilities() { return capabilities; } /** @return Class of supported {@link WebDriver} implementor */ public Class<? extends WebDriver> getUsingWebDriverClass() { return driverClazz; } /** * Starts remote server locally It is possible to launch {@link SeleniumServer} locally for now */ public synchronized void launchRemoteServerLocallyIfWasDefined() { if (serverLauncher == null) return; if (serverLauncher.isLaunched()) return; try { serverLauncher.launch(); } catch (Exception e) { throw new RuntimeException(e); } } /** @return flag of necessity of URL (remote server) */ public boolean requiresRemoteURL() { return requiresRemoteURL; } /** * @param configInstance . An instance of {@link Configuration} where path to chromedriver*, * IEDriverServer.exe or phantomjs* are defined */ private void setSystemProperty(Configuration configInstance) { if (service != null) this.service.setSystemProperty(configInstance); } /** * It is useful for {@link RemoteWebDriver} instantiation. Local services depend on capabilities */ public void setSystemProperty(Configuration configInstance, Capabilities ignored) { setSystemProperty(configInstance); } /** @return flag of of possibility to start {@link WebDriver} implementor remotely */ public boolean startsRemotely() { return startsRemotely; } public boolean isForBrowser() { return isForBrowser; } public boolean isForMobileApp() { return isForMobileApp; } }
public RemoteWebDriver getPhantomDriver() { return getRemoteDriver(DesiredCapabilities.phantomjs()); }
// li.active>span.next public static void main(String[] args) { if (args.length < 2) { System.out.println("please input two paremeter!"); System.exit(0); } String filePath = args[0]; String phantomJsPath = args[1]; DesiredCapabilities cap = DesiredCapabilities.phantomjs(); cap.setCapability( PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] { // "--webdriver-loglevel=DEBUG", // "--proxy=org.openqa.grid.selenium.proxy.DefaultRemoteProxy", }); cap.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "zh_CN"); // cap.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Macintosh; Intel Mac OS X // 10.9; rv:25.0) Gecko/20100101 Firefox/25.0"); // "C:\\Users\\Administrator\\Desktop\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe" cap.setCapability("phantomjs.binary.path", phantomJsPath); cap.setJavascriptEnabled(true); driver = new PhantomJSDriver(cap); WebDriverWait wait = null; wait = new WebDriverWait(driver, 60); driver.get(URL); /*try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); }*/ // screenShot((TakesScreenshot) driver, "/home/admin/"+"mo9"+c+".png"); List<WebElement> nextElement = null; ArrayList<String> credits = new ArrayList<String>(); int prePage = 1; int curPage = c; BufferedWriter br = null; File f = new File(filePath); try { br = new BufferedWriter(new FileWriter(f, true)); br.write("mo9账号,真实姓名,身份证号,欠款金额,逾期天数"); br.write("\r\n"); } catch (IOException e1) { e1.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } while (!isLast) { List<WebElement> elements = null; try { elements = wait.until( ExpectedConditions.visibilityOfAllElementsLocatedBy( By.cssSelector("div.faith-list>dl.fs12"))); } catch (Exception e) { e.printStackTrace(); driver.close(); driver.quit(); while (elements == null) { try { Thread.currentThread().sleep(720000); // c=((curPage/15)*15)+1; // curPage=c; driver = new PhantomJSDriver(cap); // driver=new FirefoxDriver(binary,profile); wait = new WebDriverWait(driver, 60); driver.get("https://www.mo9.com/creditCenter/p/" + curPage); elements = wait.until( ExpectedConditions.visibilityOfAllElementsLocatedBy( By.cssSelector("div.faith-list>dl.fs12"))); } catch (Exception e1) { e1.printStackTrace(); } } } System.out.println(curPage); for (WebElement element : elements) { StringBuffer sb = new StringBuffer(); String acount = element.findElement(By.cssSelector("div.faith-list>dl.fs12>dt.row01")).getText(); String name = element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row02")).getText(); String certiCard = element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row03")).getText(); String debt = element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row04")).getText(); String overDue = element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row05")).getText(); sb.append(acount + "," + name + "," + certiCard + "," + debt + "," + overDue); credits.add(sb.toString()); } nextElement = driver.findElements(By.cssSelector("li>a.next")); if (credits.size() % 150 == 0 || nextElement.size() == 0) { try { br = new BufferedWriter(new FileWriter(f, true)); for (String str : credits) { br.write(str); br.write("\r\n"); } credits.clear(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } try { // Random r=new Random(100); // int n=r.nextInt(90); Thread.currentThread().sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } } if (nextElement.size() == 0) { isLast = true; } else { nextElement.get(0).click(); curPage++; try { Thread.currentThread().sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public class DefaultDriverSessions implements DriverSessions { private static final Logger LOG = Logger.getLogger(DefaultDriverSessions.class.getName()); private final DriverFactory factory; private final Clock clock; protected final Map<SessionId, Session> sessionIdToDriver = new ConcurrentHashMap<>(); private static List<DriverProvider> defaultDriverProviders = new ImmutableList.Builder<DriverProvider>() .add( new DefaultDriverProvider( DesiredCapabilities.chrome(), "org.openqa.selenium.chrome.ChromeDriver")) .add( new DefaultDriverProvider( DesiredCapabilities.firefox(), "org.openqa.selenium.firefox.FirefoxDriver")) .add( new DefaultDriverProvider( DesiredCapabilities.internetExplorer(), "org.openqa.selenium.ie.InternetExplorerDriver")) .add( new DefaultDriverProvider( DesiredCapabilities.opera(), "com.opera.core.systems.OperaDriver")) .add( new DefaultDriverProvider( DesiredCapabilities.operaBlink(), "org.openqa.selenium.opera.OperaDriver")) .add( new DefaultDriverProvider( DesiredCapabilities.safari(), "org.openqa.selenium.safari.SafariDriver")) .add( new DefaultDriverProvider( DesiredCapabilities.phantomjs(), "org.openqa.selenium.phantomjs.PhantomJSDriver")) .add( new DefaultDriverProvider( DesiredCapabilities.htmlUnit(), "org.openqa.selenium.htmlunit.HtmlUnitDriver")) .build(); public DefaultDriverSessions() { this(Platform.getCurrent(), new DefaultDriverFactory()); } public DefaultDriverSessions(DriverFactory factory) { this(Platform.getCurrent(), factory); } protected DefaultDriverSessions(Platform runningOn, DriverFactory factory) { this(runningOn, factory, new SystemClock()); } protected DefaultDriverSessions(Platform runningOn, DriverFactory factory, Clock clock) { this.factory = factory; this.clock = clock; registerDefaults(runningOn); registerServiceLoaders(runningOn); } private void registerDefaults(Platform current) { for (DriverProvider provider : defaultDriverProviders) { registerDriverProvider(current, provider); } } private void registerServiceLoaders(Platform current) { for (DriverProvider provider : ServiceLoader.load(DriverProvider.class)) { registerDriverProvider(current, provider); } } private void registerDriverProvider(Platform current, DriverProvider provider) { Capabilities caps = provider.getProvidedCapabilities(); if (!platformMatches(current, caps)) { LOG.info( String.format( "Driver provider %s registration is skipped:%n" + "registration capabilities %s does not match the current platform %s", provider, caps, current)); return; } factory.registerDriverProvider(provider); } private boolean platformMatches(Platform current, Capabilities caps) { return caps.getPlatform() == null || caps.getPlatform() == Platform.ANY || current.is(caps.getPlatform()); } public void registerDriver(Capabilities capabilities, Class<? extends WebDriver> driverClass) { factory.registerDriverProvider(new DefaultDriverProvider(capabilities, driverClass)); } public SessionId newSession(Capabilities desiredCapabilities) throws Exception { SessionId sessionId = new SessionId(UUID.randomUUID().toString()); Session session = DefaultSession.createSession(factory, clock, sessionId, desiredCapabilities); sessionIdToDriver.put(sessionId, session); return sessionId; } public Session get(SessionId sessionId) { return sessionIdToDriver.get(sessionId); } public void deleteSession(SessionId sessionId) { final Session removedSession = sessionIdToDriver.remove(sessionId); if (removedSession != null) { removedSession.close(); } } public Set<SessionId> getSessions() { return Collections.unmodifiableSet(sessionIdToDriver.keySet()); } }
public static DesiredCapabilities of(Browser browser) { if (browser == null) { return null; } DesiredCapabilities caps; switch (browser) { case android: case android_real_phone: caps = DesiredCapabilities.android(); break; case chrome: caps = DesiredCapabilities.chrome(); break; case ff: caps = DesiredCapabilities.firefox(); break; case htmlunit: caps = DesiredCapabilities.htmlUnit(); caps.setJavascriptEnabled(false); break; case htmlunit_js: caps = DesiredCapabilities.htmlUnit(); caps.setJavascriptEnabled(true); break; case ie: caps = DesiredCapabilities.internetExplorer(); break; case opera: OperaProfile profile = new OperaProfile(); profile.preferences().set("Geolocation", "Enable geolocation", true); // This pref allows all sites to access geolocation without prompting. // A 0 value would deny all sites and -1 would prompt for all sites. profile.preferences().set("User Prefs", "Geolocation site state", 1); caps = DesiredCapabilities.opera(); caps.setCapability("opera.profile", profile); break; case opera_mobile: caps = DesiredCapabilities.opera(); caps.setCapability(OperaSettings.Capability.PRODUCT.getCapability(), OperaProduct.MOBILE); break; case phantomjs: caps = DesiredCapabilities.phantomjs(); break; case safari: caps = DesiredCapabilities.safari(); break; case ipad: caps = DesiredCapabilities.ipad(); break; case iphone: caps = DesiredCapabilities.iphone(); break; default: throw new RuntimeException("Cannot determine browser config to use"); } String version = System.getProperty("selenium.browser.version"); if (version != null) { caps.setVersion(version); } caps.setCapability(HAS_NATIVE_EVENTS, Boolean.getBoolean("selenium.browser.native_events")); return caps; }