WaitForPageLoadedModifier implements CollectorJob { @Override public CollectorStepResult collect() throws ProcessingException { CollectorStepResult result; final String currentUrl = webDriver.getCurrentUrl(); LOG.info("Start WaitForPageLoadedModifier for page {}", currentUrl); int waitTimeoutMs = PAGE_LOAD_WAIT_TIMEOUT_MS; int previousLoadedElementsCount; int currentLoadedElementsCount = 0; try { do { previousLoadedElementsCount = currentLoadedElementsCount; Thread.sleep(PAGE_LOAD_CHECK_TIME_MS); waitTimeoutMs -= PAGE_LOAD_CHECK_TIME_MS; currentLoadedElementsCount = webDriver.findElements(By.xpath(" LOG.debug("Currently found {} elements. Left {} ms to timeout.", currentLoadedElementsCount, waitTimeoutMs); } while (currentLoadedElementsCount > previousLoadedElementsCount && waitTimeoutMs > 0); result = CollectorStepResult.newModifierResult(); } catch (InterruptedException e) { LOG.error("WaitForPageLoadedModifier interrupted", e); throw new ProcessingException("WaitForPageLoadedModifier interrupted", e); } if (waitTimeoutMs <= 0) { LOG.warn("Page {} reload timeout!", currentUrl); result.addError(String.format("Page %s was timed out before loading!", currentUrl)); } return result; } WaitForPageLoadedModifier(WebDriver webDriver); @Override CollectorStepResult collect(); @Override void setParameters(Map<String, String> params); static final String NAME; }
@Test public void testAlreadyLoaded() throws Exception { when(webDriver.findElements(By.xpath(" when(elements.size()).thenReturn(100); tested.collect(); verify(webDriver, times(2)).findElements(Matchers.<By>any()); }
@Test public void testTimeout() throws Exception { when(webDriver.findElements(By.xpath(" Integer[] sizes = new Integer[15]; for (int i = 1; i <= 15; i++) { sizes[i - 1] = 100 + i; } when(elements.size()).thenReturn(100, sizes); tested.collect(); verify(webDriver, times(10)).findElements(Matchers.<By>any()); }
@Test public void testElementsLoaded() throws Exception { when(webDriver.findElements(By.xpath(" when(elements.size()).thenReturn(100, 101, 102, 102); tested.collect(); verify(webDriver, times(4)).findElements(Matchers.<By>any()); }