Skip to content

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

Description

@gevorgmushyan

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);
                        }
                    }
            );
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions