List of usage examples for org.openqa.selenium WebDriver manage
Options manage();
From source file:com.pentaho.ctools.utils.HttpUtils.java
License:Apache License
/** * This method shall look for HttpErrors return the status of HTTP request. Will return false if no error with that number was found. * * @param driver/* w w w. j a va 2s .c o m*/ * @param ErrorNumber * @return * @throws Exception */ public static boolean GetHttpError(WebDriver driver, String ErrorNumber) { Boolean errorFound = false; driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); for (int i = 0; i < 1000; i++) { try { driver.findElement(By.id("web_" + ErrorNumber)); errorFound = true; } catch (NoSuchElementException s) { LOG.error("NoSuchElement - got it."); break; } catch (StaleElementReferenceException s) { LOG.error("Stale - got it."); } } driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); return errorFound; }
From source file:com.qwazr.crawler.web.driver.BrowserDriver.java
License:Apache License
BrowserDriver(BrowserDriverEnum type, WebDriver driver, WebCrawlDefinition.ProxyDefinition currentProxy) { this.type = type; this.driver = driver; this.currentProxy = currentProxy; Timeouts timeouts = driver.manage().timeouts(); timeouts.implicitlyWait(1, TimeUnit.MINUTES); timeouts.setScriptTimeout(2, TimeUnit.MINUTES); timeouts.pageLoadTimeout(3, TimeUnit.MINUTES); }
From source file:com.qwazr.crawler.web.driver.BrowserDriverBuilder.java
License:Apache License
public BrowserDriver build() throws ReflectiveOperationException, SecurityException { BrowserDriverEnum browserType = BrowserDriverEnum.html_unit; final WebCrawlDefinition.ProxyDefinition proxyDef; DesiredCapabilities capabilities = null; if (crawlDefinition != null) { // Choose a browser type if (crawlDefinition.browser_type != null) browserType = crawlDefinition.browser_type; proxyDef = selectProxy();//ww w . j ava2 s. c o m // Setup the proxy if (proxyDef != null) { capabilities = checkCapabilities(capabilities); org.openqa.selenium.Proxy proxy = new Proxy(); if (proxyDef.http_proxy != null) proxy.setHttpProxy(proxyDef.http_proxy); if (proxyDef.ftp_proxy != null) proxy.setFtpProxy(proxyDef.ftp_proxy); if (proxyDef.ssl_proxy != null) proxy.setSslProxy(proxyDef.ssl_proxy); if (proxyDef.socks_proxy != null) proxy.setSocksProxy(proxyDef.socks_proxy); if (proxyDef.socks_username != null) proxy.setSocksUsername(proxyDef.socks_username); if (proxyDef.socks_password != null) proxy.setSocksPassword(proxyDef.socks_password); if (proxyDef.no_proxy != null) proxy.setNoProxy(proxyDef.no_proxy); if (proxyDef.proxy_autoconfig_url != null) proxy.setProxyAutoconfigUrl(proxyDef.proxy_autoconfig_url); capabilities.setCapability(CapabilityType.PROXY, proxy); } // Setup the language if (crawlDefinition.browser_language != null) { capabilities = checkCapabilities(capabilities); capabilities.setCapability(AdditionalCapabilities.QWAZR_BROWSER_LANGUAGE, crawlDefinition.browser_language); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", crawlDefinition.browser_language); } // Download images if (crawlDefinition.download_images != null) { capabilities = checkCapabilities(capabilities); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "loadImages", crawlDefinition.download_images); } // Web security if (crawlDefinition.web_security != null) { capabilities = checkCapabilities(capabilities); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "webSecurityEnabled", crawlDefinition.web_security); } // Choose a browser name if (crawlDefinition.browser_name != null) { capabilities = checkCapabilities(capabilities); capabilities.setBrowserName(crawlDefinition.browser_name); } if (crawlDefinition.browser_version != null) { capabilities = checkCapabilities(capabilities); capabilities.setVersion(crawlDefinition.browser_version); } // Javascript capability if (crawlDefinition.javascript_enabled != null) { capabilities = checkCapabilities(capabilities); capabilities.setJavascriptEnabled(crawlDefinition.javascript_enabled); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "javascriptEnabled", crawlDefinition.javascript_enabled); } } else proxyDef = null; if (browserType == BrowserDriverEnum.phantomjs) { capabilities = checkCapabilities(capabilities); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] { "--webdriver-loglevel=NONE", "--ignore-ssl-errors=true" }); } final WebDriver driver = browserType.getNewInstance(capabilities); try { final BrowserDriver browserDriver = new BrowserDriver(browserType, driver, proxyDef); browserDriver.setTimeouts(crawlDefinition.implicitly_wait, crawlDefinition.page_load_timeout, crawlDefinition.script_timeout); if (crawlDefinition.cookies != null) for (Map.Entry<String, String> cookie : crawlDefinition.cookies.entrySet()) driver.manage().addCookie(new Cookie(cookie.getKey(), cookie.getValue())); return browserDriver; } catch (Exception e) { driver.quit(); throw e; } }
From source file:com.seleniumtests.driver.WebUtility.java
License:Apache License
public static void main(final String[] args) { WebUIDriver.getWebUIDriver().setMode("ExistingGrid"); WebUIDriver.getWebUIDriver().setHubUrl(" "); WebDriver driver = WebUIDriver.getWebDriver(true); System.out/* ww w . j av a2s . c om*/ .print(driver.manage().window().getSize().width + ":" + driver.manage().window().getSize().height); }
From source file:com.seltaf.utils.WebUtility.java
License:Apache License
public static void main(final String[] args) { DriverManager.getDriverManager().setMode("ExistingGrid"); DriverManager.getDriverManager().setHubUrl(" "); WebDriver driver = DriverManager.getWebDriver(true); System.out//from ww w. j a v a 2s . co m .print(driver.manage().window().getSize().width + ":" + driver.manage().window().getSize().height); }
From source file:com.smash.revolance.ui.model.bot.BrowserFactory.java
License:Open Source License
public static void instanceciateNavigator(User user, BrowserType browserType) throws InstanciationError { final Logger logger = user.getLogger(); if (!user.isExplorationDone()) { WebDriver browser = null; DriverService service = null;/*from www .ja va 2 s. com*/ if (browserType == BrowserType.Firefox) { browser = new FirefoxDriver(); } else if (browserType == BrowserType.Chrome) { File driver = new File(user.getDriverPath()); File binary = new File(user.getBrowserPath()); // ImmutableMap<String, String> env = new ImmutableMap.Builder<String, String>().build(); ChromeDriverService.Builder serviceBuilder = new ChromeDriverService.Builder() .usingDriverExecutable(driver).usingAnyFreePort(); // serviceBuilder.withEnvironment( env ); service = serviceBuilder.build(); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability("chrome.binary", binary.getAbsolutePath()); // capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized")); browser = new ChromeDriver((ChromeDriverService) service, capabilities); } else if (browserType == BrowserType.PhantomJS) { DesiredCapabilities cfg = DesiredCapabilities.phantomjs(); cfg.setJavascriptEnabled(true); try { service = ResolvingPhantomJSDriverService.createDefaultService(cfg); // service resolving phantomjs binary automatically browser = new PhantomJSDriver(service, cfg); } catch (IOException e) { throw new InstanciationError("Unable to start ghost web driver!", e); } } else if (browserType == BrowserType.MockedWebDriver) { try { System.setProperty("webdriver.remote.server", String.valueOf(9090)); browser = new MockedWebDriver(9090); } catch (MalformedURLException e) { throw new InstanciationError("Unable to start the mocked web driver!", e); } } if (browser != null) { browser.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); logger.log(Level.INFO, "Launching a " + browser + " browser"); browser.manage().window().setSize(new Dimension(user.getBrowserWidth(), user.getBrowserHeight())); logger.log(Level.INFO, "Setting up resolution to: " + user.getBrowserWidth() + "x" + user.getBrowserHeight()); user.setBrowser(browser); user.setBrowserActive(true); } else { logger.log(Level.ERROR, "Unable to start the browser: " + browser); } if (service != null) { user.setDriverService(service); } } }
From source file:com.sol.browser.DriverFactory.java
License:Apache License
/** * Returns an instance of the requested WebDriver. * This method also sets the implicite waiting time according to the * property in the property file./*from w w w .ja v a 2 s. co m*/ * @param driverType - name of the requested driver * @return if the requested driver is valid, returns an instant of the * requested driver. If the requested driver is invalid, null is returned. * @throws FileNotFoundException if property file is not found * @throws IOException * @throws NullPointerException if {@code browser} is {@code null} * @throws IllegalArgumentException if the driver type is not one of the * following: Chrome, Firefox, or HTMLUnit. * <br><b>Note:</b> The following keys <em>must</em> be present in the * properties file (and containing values): the <em>absolute</em> path * to the requested WebDriver (in case of ChromeDriver), and the default * waiting time for the implicit wait. Missing keys may cause undefined * behavior. * @see BrowserPropertyFile */ public static WebDriver getDriver(final DriverType driverType) throws FileNotFoundException, IOException, NullPointerException, IllegalArgumentException { Objects.requireNonNull(driverType); WebDriver driver = null; BrowserPropertyFile properties = BrowserPropertyFile.getInstance(); switch (driverType) { case CHROME: String pathToDriver = properties.getProperty(BrowserPropertyConstants.PATH_TO_DRIVER); System.setProperty("webdriver.chrome.driver", pathToDriver); driver = new ChromeDriver(); break; case FIREFOX: driver = new FirefoxDriver(); break; case HTMLUNIT: driver = new HtmlUnitDriver(); break; default: throw new IllegalArgumentException("Illegal driver type"); } String waitingTime = properties.getProperty(BrowserPropertyConstants.IMPLICITE_WAITING_TIME); long implicitWaitingTime = Long.parseLong(waitingTime); driver.manage().timeouts().implicitlyWait(implicitWaitingTime, TimeUnit.SECONDS); return driver; }
From source file:com.spambot.invisicow.SeleniumDriver.java
public void drive(int opt) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://findtheinvisiblecow.com"); WebElement body = driver.findElement(By.tagName("body")); new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(body)); //start game WebElement button = driver.findElement(By.xpath("//div[@id='loader']//button")); new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(button)); button.click();/*from ww w.j a va 2 s . c o m*/ for (int i = 0; i < opt - 1; i++) { //so the user sees something Thread.sleep(2500); //win game if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor) driver).executeScript("find.gameStop(true);"); } //again? WebElement congrats = driver.findElement(By.id("modal-congratulations")); new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(congrats)); Thread.sleep(1500); WebElement again = driver.findElement(By.xpath("//div[@id='modal-congratulations']//button")); again.click(); } Thread.sleep(2500); //win game one last time if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor) driver).executeScript("find.gameStop(true);"); } Thread.sleep(3000); JOptionPane.showMessageDialog(null, "Goodbye!"); driver.quit(); }
From source file:com.springer.omelet.driver.DriverUtility.java
License:Apache License
/*** * Generic waitFor Function which waits for condition to be successful else return null * /*from ww w . ja v a 2s .c om*/ * @param expectedCondition:ExpectedCondition<T> * @param driver:WebDriver * @param timeout in seconds * @return <T> or null */ public static <T> T waitFor(ExpectedCondition<T> expectedCondition, WebDriver driver, int timeOutInSeconds) { Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); try { T returnValue = new WebDriverWait(driver, timeOutInSeconds).pollingEvery(500, TimeUnit.MILLISECONDS) .until(expectedCondition); return returnValue; } catch (TimeoutException e) { return null; } finally { driver.manage().timeouts().implicitlyWait(Driver.getBrowserConf().getDriverTimeOut(), TimeUnit.SECONDS); stopwatch.stop(); LOGGER.debug("Time Taken for waitFor method for Expected Condition is:" + stopwatch.elapsedTime(TimeUnit.SECONDS)); } }
From source file:com.sugarcrm.candybean.automation.VInterface.java
License:Open Source License
private WebDriver getWebDriver(Type iType) throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); // capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT); WebDriver wd = null; switch (iType) { case FIREFOX: String profileName = this.config.getValue("browser.firefox_profile", "default"); File ffBinaryPath = new File(this.config.getPathValue("browser.firefox_binary")); FirefoxProfile ffProfile = (new ProfilesIni()).getProfile(profileName); // ffProfile.setEnableNativeEvents(false); FirefoxBinary ffBinary = new FirefoxBinary(ffBinaryPath); // if (System.getProperty("headless") != null) { // FirefoxBinary ffBinary = new FirefoxBinary();//new // File("//home//conrad//Applications//firefox-10//firefox")); // ffBinary.setEnvironmentProperty("DISPLAY", ":1"); // webDriver = new FirefoxDriver(ffBinary, ffProfile); // }//from ww w. j a v a 2s.c om candybean.log.info("Instantiating Firefox with profile name: " + profileName + " and binary path: " + ffBinaryPath); wd = new FirefoxDriver(ffBinary, ffProfile); break; case CHROME: ChromeOptions chromeOptions = new ChromeOptions(); String chromeDriverLogPath = this.config.getPathValue("browser.chrome_driver_log_path"); candybean.log.info("chromeDriverLogPath: " + chromeDriverLogPath); chromeOptions.addArguments("--log-path=" + chromeDriverLogPath); String chromeDriverPath = this.config.getPathValue("browser.chrome_driver_path"); candybean.log.info("chromeDriverPath: " + chromeDriverPath); // chromeOptions.setBinary(new File(chromeDriverPath)); System.setProperty("webdriver.chrome.driver", chromeDriverPath); candybean.log.info("Instantiating Chrome with:\n log path:" + chromeDriverLogPath + "\n driver path: " + chromeDriverPath); wd = new ChromeDriver(chromeOptions); break; case IE: String ieDriverPath = this.config.getPathValue("browser.ie_driver_path"); candybean.log.info("ieDriverPath: " + ieDriverPath); System.setProperty("webdriver.ie.driver", ieDriverPath); capabilities = DesiredCapabilities.internetExplorer(); wd = new InternetExplorerDriver(capabilities); break; case SAFARI: throw new Exception("Selenium: safari browser not yet supported."); case ANDROID: capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android"); capabilities.setCapability(CapabilityType.PLATFORM, "Mac"); capabilities.setCapability("app", "https://s3.amazonaws.com/voodoo2/ApiDemos-debug.apk"); wd = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); break; case IOS: capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS"); capabilities.setCapability(CapabilityType.VERSION, "6.0"); capabilities.setCapability(CapabilityType.PLATFORM, "Mac"); capabilities.setCapability("app", "https://s3.amazonaws.com/voodoo2/TestApp.zip"); wd = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); break; default: throw new Exception("Selenium: browser type not recognized."); } long implicitWait = Long.parseLong(config.getValue("perf.implicit_wait_seconds")); if (System.getProperty("headless") == null) { java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); wd.manage().window().setSize(new Dimension(screenSize.width, screenSize.height)); } wd.manage().timeouts().implicitlyWait(implicitWait, TimeUnit.SECONDS); return wd; }