/** * If there is an implicit wait set, then you might need to wait for a shorter period. This * method makes it possible with setting the implicit wait temporarily to 0. * * @param webDriver webdriver to be used * @param seconds timeout in seconds * @return a {@link Wait} instance, which set the implicit wait temporarily to 0. */ public Wait<WebDriver> waitOverridingImplicitWait(WebDriver webDriver, int seconds) { //API comes from selenium -> cannot migrate guava to java 8 //noinspection Guava return new Wait<WebDriver>() { @Override public <T> T until(Function<? super WebDriver, T> function) { Timeouts timeouts = webDriver.manage().timeouts(); try { timeouts.implicitlyWait(0, TimeUnit.SECONDS); return new WebDriverWait(webDriver, seconds, CONDITION_POLL_INTERVAL).until(function); } finally { timeouts.implicitlyWait(implicitWait.getNano(), TimeUnit.NANOSECONDS); } } }; }
/** * Set configured timeout intervals in the specified driver. * * @param driver driver object in which to configure timeout intervals * @param config configuration object that specifies timeout intervals */ public static void setDriverTimeouts(WebDriver driver, SeleniumConfig config) { Timeouts timeouts = driver.manage().timeouts(); timeouts.setScriptTimeout(WaitType.SCRIPT.getInterval(config), TimeUnit.SECONDS); timeouts.implicitlyWait(WaitType.IMPLIED.getInterval(config), TimeUnit.SECONDS); timeouts.pageLoadTimeout(WaitType.PAGE_LOAD.getInterval(config), TimeUnit.SECONDS); }
@Override public void postConstruct(D webDriver) { Timeouts timeouts = webDriver.manage().timeouts(); if (implicitWait != null) { timeouts.implicitlyWait(implicitWait.getSeconds(), TimeUnit.SECONDS); } if (scriptTimeout != null) { timeouts.setScriptTimeout(scriptTimeout.getSeconds(), TimeUnit.SECONDS); } if (pageLoadTimeout != null) { timeouts.pageLoadTimeout(pageLoadTimeout.getSeconds(), TimeUnit.SECONDS); } }
/** * Acquire the element reference that's wrapped by the specified robust element wrapper. * * @param wrapper robust element wrapper * @return wrapped element reference */ private static RobustElementWrapper acquireReference(RobustElementWrapper wrapper) { SearchContext context = wrapper.context.getWrappedContext(); if (wrapper.strategy == Strategy.LOCATOR) { Timeouts timeouts = wrapper.driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); try { if (wrapper.index > 0) { List<WebElement> elements = context.findElements(wrapper.locator); if (wrapper.index < elements.size()) { wrapper.wrapped = elements.get(wrapper.index); } else { throw new NoSuchElementException( String.format("Too few elements located %s: need: %d; have: %d", wrapper.locator, wrapper.index + 1, elements.size())); } } else { wrapper.wrapped = context.findElement(wrapper.locator); } } catch (NoSuchElementException e) { if (wrapper.index != OPTIONAL) { throw e; } wrapper.deferredException = e; wrapper.wrapped = null; } finally { timeouts.implicitlyWait(WaitType.IMPLIED.getInterval(), TimeUnit.SECONDS); } } else { List<Object> args = new ArrayList<>(); List<WebElement> contextArg = new ArrayList<>(); if (context instanceof WebElement) { contextArg.add((WebElement) context); } String js; args.add(contextArg); args.add(wrapper.selector); if (wrapper.strategy == Strategy.JS_XPATH) { js = LOCATE_BY_XPATH; } else { js = LOCATE_BY_CSS; args.add(wrapper.index); } wrapper.wrapped = JsUtility.runAndReturn(wrapper.driver, js, args.toArray()); } if (wrapper.wrapped != null) { wrapper.acquiredAt = System.currentTimeMillis(); wrapper.deferredException = null; } return wrapper; }
@Test public void testWaitFixedTime() { final Options options = mockDriver.manage(); Mockito.when(options.timeouts()).thenReturn(Mockito.mock(Timeouts.class)); waitFixedTime(); }
protected void assertElementNotFound(String message, String cssPath) { Timeouts timeouts = wd.manage().timeouts(); timeouts.implicitlyWait(NO_ELEMENT_WAIT, TimeUnit.MILLISECONDS); assertEquals(message, 0, find(cssPath).size()); timeouts.implicitlyWait(IMPLICITLY_WAIT, TimeUnit.MILLISECONDS); }
final public void setTimeouts(Integer pageLoad, Integer script) { Timeouts timeOuts = driver.manage().timeouts(); timeOuts.pageLoadTimeout(pageLoad, TimeUnit.SECONDS); timeOuts.setScriptTimeout(script, TimeUnit.SECONDS); }
@Override public void configure(Config conf) { super.configure(conf); // see https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setJavascriptEnabled(true); String userAgentString = getAgentString(conf); // custom capabilities Map<String, Object> confCapabilities = (Map<String, Object>) conf .get("selenium.capabilities"); if (confCapabilities != null) { Iterator<Entry<String, Object>> iter = confCapabilities.entrySet() .iterator(); while (iter.hasNext()) { Entry<String, Object> entry = iter.next(); Object val = entry.getValue(); // substitute variable $useragent for the real value if (val instanceof String && "$useragent".equalsIgnoreCase(val.toString())) { val = userAgentString; } capabilities.setCapability(entry.getKey(), entry.getValue()); } } // number of instances to create per connection // https://github.com/DigitalPebble/storm-crawler/issues/505 int numInst = ConfUtils.getInt(conf, "selenium.instances.num", 1); // load adresses from config List<String> addresses = ConfUtils.loadListFromConf( "selenium.addresses", conf); if (addresses.size() == 0) { throw new RuntimeException("No value found for selenium.addresses"); } try { for (String cdaddress : addresses) { for (int inst = 0; inst < numInst; inst++) { RemoteWebDriver driver = new RemoteWebDriver(new URL( cdaddress), capabilities); Timeouts touts = driver.manage().timeouts(); int implicitWait = ConfUtils.getInt(conf, "selenium.implicitlyWait", 0); int pageLoadTimeout = ConfUtils.getInt(conf, "selenium.pageLoadTimeout", -1); int setScriptTimeout = ConfUtils.getInt(conf, "selenium.setScriptTimeout", 0); touts.implicitlyWait(implicitWait, TimeUnit.MILLISECONDS); touts.pageLoadTimeout(pageLoadTimeout, TimeUnit.MILLISECONDS); touts.setScriptTimeout(setScriptTimeout, TimeUnit.MILLISECONDS); drivers.add(driver); } } } catch (Exception e) { throw new RuntimeException(e); } }
public static WebDriver implicitWait(WebDriver webDriver) { Options webDriverOptions = webDriver.manage(); Timeouts timeouts = webDriverOptions.timeouts(); timeouts.implicitlyWait(30, TimeUnit.SECONDS); return webDriver; }