Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional features for AssignDriver class for Collection of Qurey fields #7

Open
gevorgmushyan opened this issue Feb 14, 2019 · 0 comments

Comments

@gevorgmushyan
Copy link

gevorgmushyan commented Feb 14, 2019

In case of Collection of Qurey fields ex.

List links = IntStream.range(1, 6)
.mapToObj(i -> new Query().defaultLocator(By.cssSelector("table > tbody > tr:nth-child(" + i + ") a")))
.collect(Collectors.toList());

the initQueryObjects method unable to set Driver object.

In this case we can change initQueryObjects method```

private static void initQueryObject(Object object, Field field, RemoteWebDriver driver) {
try {
field.setAccessible(true);
Query queryObject = (Query) field.get(object);
if (null != queryObject) {
queryObject.usingDriver(driver);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

private static void initQueryCollection(Object object, Field field, RemoteWebDriver driver) {
    try {
        field.setAccessible(true);
        @SuppressWarnings("unchecked") Collection<?> unsafeCollection = (Collection) field.get(object);
        unsafeCollection
                .stream()
                // making this collection safe
                .filter(Query.class::isInstance)
                .map(Query.class::cast)
                .filter(Objects::nonNull)
                .forEach(query -> query.usingDriver(driver));
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

public static void initQueryObjects(Object object, RemoteWebDriver driver) {
    Arrays.stream(object.getClass().getDeclaredFields())
            .forEach(
                    field -> {
                        if (field.getType() == Query.class) {
                            initQueryObject(object, field, driver);
                        } else if (Collection.class.isAssignableFrom(field.getType())) {
                            initQueryCollection(object, field, driver);
                        }
                    }
            );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant