@Override public Object invoke(final Object proxy, final MethodInfo methodInfo, final Configuration configuration) { final Method method = methodInfo.getMethod(); assert proxy instanceof SearchContext; assert method.isAnnotationPresent(FindBy.class); final Map<String, String> parameters = getParameters(method, methodInfo.getArgs()); final String xpath = processTemplate(method.getAnnotation(FindBy.class).value(), parameters); final String name = Optional.ofNullable(method.getAnnotation(Name.class)).map(Name::value) .orElse(method.getName()); final SearchContext context = (SearchContext) proxy; final List<WebElement> originalElements = context.findElements(By.xpath(xpath)); final Type methodReturnType = ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0]; final List newElements = IntStream.range(0, originalElements.size()) .mapToObj(i -> { final WebElement originalElement = originalElements.get(i); final Configuration childConfiguration = configuration.child(); final Target target = new HardcodedTarget(listElementName(name, i), originalElement); return new Atlas(childConfiguration) .create(target, (Class<?>) methodReturnType); }) .collect(toList()); return new Atlas(configuration.child()) .create(newElements, method.getReturnType()); }
@Test public void shouldFindElementsCollection() { when(parent.findElements(By.xpath(SELECTOR))).thenReturn(collection); when(collection.size()).thenReturn(DEFAULT_SIZE); ParentElement parentElement = new Atlas() .extension(new FindByCollectionExtension()) .create(parent, ParentElement.class); assertThat(parentElement.collection().size()).isEqualTo(DEFAULT_SIZE); }
@Test(expected = NullPointerException.class) public void shouldNotInteractWithoutResult() { when(parent.findElements(By.xpath(SELECTOR))).thenReturn(null); when(collection.size()).thenReturn(DEFAULT_SIZE); ParentElement parentElement = new Atlas() .extension(new FindByCollectionExtension()) .create(parent, ParentElement.class); assertThat(parentElement.collection().size()).isEqualTo(DEFAULT_SIZE); }
@Test public void shouldParameterizedFindByCollection() { when(parent.findElements(any())).thenReturn(asList(mockWebElement())); atlas = new Atlas().extension(new FindByCollectionExtension()); String param = RandomStringUtils.randomAlphanumeric(10); ParentElement atlasWebElement = atlas.create(parent, ParentElement.class); atlasWebElement.elements(param); verify(parent, times(1)).findElements(By.xpath(String.format("//td[%s]", param))); }
public WebDriverConfiguration(final WebDriver webDriver) { registerContext(new WebDriverContext(webDriver)); registerExtension(new DriverProviderExtension()); registerExtension(new DefaultMethodExtension()); registerExtension(new FindByExtension()); registerExtension(new FindByCollectionExtension()); registerExtension(new ShouldMethodExtension()); registerExtension(new WaitUntilMethodExtension()); registerExtension(new WrappedElementMethodExtension()); registerExtension(new ExecuteJScriptMethodExtension()); } }