/** * Retrieves the first WComponent by its path in the WComponent tree. See * {@link #findWComponents(WComponent, String[])} for a description of paths. * <p> * Searches only visible components. * </p> * * @param component the component to search from. * @param path the path to the WComponent. * @return the first component matching the given path, or null if not found. */ public static ComponentWithContext findWComponent(final WComponent component, final String[] path) { return findWComponent(component, path, true); }
/** * Retrieves WComponents by their path in the WComponent tree. See {@link #findWComponents(WComponent, String[])} * for a description of paths. * <p> * Searches only visible components. * </p> * * @param component the component to search from. * @param path the path to the WComponent. * @return the component matching the given path, or null if not found. */ public static ComponentWithContext[] findWComponents(final WComponent component, final String[] path) { return findWComponents(component, path, true); }
/** * Retrieves the component with the given Id. * <p> * Searches visible and not visible components. * </p> * * @param root the root component to search from. * @param id the id to search for. * @return the component with the given id, or null if not found. */ public static WComponent getComponentWithId(final WComponent root, final String id) { return getComponentWithId(root, id, false); }
/** * Retrieves the context for the component with the given Id. * <p> * Searches visible and not visible components. * </p> * * @param root the root component to search from. * @param id the id to search for. * @return the context for the component with the given id, or null if not found. */ public static UIContext getContextForId(final WComponent root, final String id) { return getContextForId(root, id, false); }
/** * General utility method to visit every WComponent in the tree, taking repeaters etc. into account. * * @param node the node to traverse. * @param visitor the visitor to notify as the tree is traversed. */ public static void traverse(final WComponent node, final WComponentTreeVisitor visitor) { doTraverse(node, false, visitor); }
/** * Retrieves the context for the component with the given Id. * <p> * Searches visible and not visible components. * </p> * * @param root the root component to search from. * @param id the id to search for. * @return the context for the component with the given id, or null if not found. */ public static ComponentWithContext getComponentWithContextForId(final WComponent root, final String id) { return getComponentWithContextForId(root, id, false); }
/** * Retrieves the closest context for the component with the given Id. * <p> * Searches visible and not visible components. * </p> * * @param root the root component to search from. * @param id the id to search for. * @return the closest context for the component with the given id, or null if not found. */ public static UIContext getClosestContextForId(final WComponent root, final String id) { return getClosestContextForId(root, id, false); }
/** * Search for components implementing a particular class name. * <p> * Only search visible components and include the root component in the matching logic. * </p> * * @param root the root component to search from * @param className the class name to search for * * @return the list of components implementing the class name */ public static List<ComponentWithContext> findComponentsByClass(final WComponent root, final String className) { return findComponentsByClass(root, className, true, true); }
List<ComponentWithContext> visibles = TreeUtil.collateVisibles(root); UIContext uic = UIContextHolder.getCurrent(); visibles = TreeUtil.collateVisibles(root); Assert.assertEquals("Incorrect number of visible components", 9, visibles.size()); Assert.assertTrue("List should contain root", isInList(visibles, root, uic)); visibles = TreeUtil.collateVisibles(root); Assert.assertEquals("Incorrect number of visible components", 8, visibles.size()); Assert.assertTrue("List should contain root", isInList(visibles, root, uic)); visibles = TreeUtil.collateVisibles(root); Assert.assertEquals("Incorrect number of visible components", 6, visibles.size()); Assert.assertTrue("List should contain root", isInList(visibles, root, uic)); setActiveContext(uic); visibles = TreeUtil.collateVisibles(root); Assert.assertEquals("Incorrect number of visible components", 9, visibles.size()); Assert.assertTrue("List should contain root", isInList(visibles, root, uic)); visibles = TreeUtil.collateVisibles(root); Assert.assertEquals("Incorrect number of visible components", 9, visibles.size()); Assert.assertTrue("List should contain root", isInList(visibles, root, uic)); visibles = TreeUtil.collateVisibles(root); Assert.assertEquals("Incorrect number of visible components", 0, visibles.size());
@Test public void testGetContextForId() { UIContext uic = UIContextHolder.getCurrent(); Assert.assertSame("Incorrect context returned for root", uic, TreeUtil.getContextForId(root, root.getId())); Assert.assertSame("Incorrect context returned for containerChild", uic, TreeUtil. getContextForId(root, containerChild.getId())); Assert.assertSame("Incorrect context returned for simpleChild", uic, TreeUtil. getContextForId(root, simpleChild.getId())); Assert.assertSame("Incorrect context returned for repeaterChild", uic, TreeUtil. getContextForId(root, repeaterChild.getId())); Assert.assertSame("Incorrect context returned for grandChild", uic, TreeUtil. getContextForId(root, grandChild.getId())); UIContext row2Context = repeaterChild.getRowContext("2"); setActiveContext(row2Context); Assert.assertSame("Incorrect context returned for repeatedComponent row 2", row2Context, TreeUtil.getContextForId(root, repeatedComponent.getId())); }
/** * General utility method to visit every visible WComponent in the tree, taking repeaters etc. into account. * * @param node the node to traverse. * @param visitor the visitor to notify as the tree is traversed. */ public static void traverseVisible(final WComponent node, final WComponentTreeVisitor visitor) { doTraverse(node, true, visitor); }
/** * Retrieves the component with the given Id. * * @param root the root component to search from. * @param id the id to search for. * @param visibleOnly true if process visible only * @return the component with the given id, or null if not found. */ public static WComponent getComponentWithId(final WComponent root, final String id, final boolean visibleOnly) { ComponentWithContext comp = getComponentWithContextForId(root, id, visibleOnly); return comp == null ? null : comp.getComponent(); }
/** * Finds the closest context for the given component id. This handles the case where the component no longer exists * due to having been removed from the UI, or having a SubUIContext removed. * * @param id the id of the component to search for. * @return the component and context for the given id, or null if not found. */ public static UIContext findClosestContext(final String id) { UIContext uic = UIContextHolder.getCurrent(); WComponent root = uic.getUI(); UIContext closest = TreeUtil.getClosestContextForId(root, id); return closest; }
@Test public void testFindWComponent() { UIContext uic = UIContextHolder.getCurrent(); ComponentWithContext result = TreeUtil.findWComponent(root, new String[]{"WApplication"}); Assert.assertSame("Incorrect component returned for find WApplication", root, result. getComponent()); getContext()); result = TreeUtil.findWComponent(root, new String[]{"WTextArea"}); Assert.assertSame("Incorrect component returned for find WTextArea", grandChild, result. getComponent()); Assert.assertSame("Incorrect context returned for find WTextArea", uic, result.getContext()); result = TreeUtil.findWComponent(root, new String[]{"WContainer", "WTextArea"}); Assert.assertSame("Incorrect component returned for find WContainer/WTextArea", grandChild, result.getComponent()); result = TreeUtil.findWComponent(root, new String[]{"WText[1]"}); Assert.assertSame("Incorrect component returned for find WText[1]", repeatedComponent, result.getComponent()); result = TreeUtil.findWComponent(root, new String[]{"WApplication", "WText[1]"}); Assert.assertSame("Incorrect component returned for find WApplication/WText[1]", repeatedComponent, result.getComponent()); result = TreeUtil.findWComponent(root, new String[]{"WContainer", "WRepeater"}); Assert.assertNull("Should not have a result for an invalid path", result);
/** * Retrieves the first WComponent by its path in the WComponent tree. See * {@link #findWComponents(WComponent, String[])} for a description of paths. * * @param component the component to search from. * @param path the path to the WComponent. * @param visibleOnly visible only * @return the first component matching the given path, or null if not found. */ public static ComponentWithContext findWComponent(final WComponent component, final String[] path, final boolean visibleOnly) { ComponentWithContext[] components = findWComponents(component, path, visibleOnly); return components.length == 0 ? null : components[0]; }
@Test public void testGetComponentWithId() { Assert.assertSame("Incorrect component returned for root", root, TreeUtil. getComponentWithId(root, root.getId())); Assert.assertSame("Incorrect component returned for containerChild", containerChild, TreeUtil.getComponentWithId(root, containerChild.getId())); Assert.assertSame("Incorrect component returned for simpleChild", simpleChild, TreeUtil. getComponentWithId(root, simpleChild.getId())); Assert.assertSame("Incorrect component returned for repeaterChild", repeaterChild, TreeUtil. getComponentWithId(root, repeaterChild.getId())); Assert.assertSame("Incorrect component returned for grandChild", grandChild, TreeUtil. getComponentWithId(root, grandChild.getId())); UIContext row2Context = repeaterChild.getRowContext("2"); setActiveContext(row2Context); Assert.assertSame("Incorrect component returned for repeatedComponent row 2", repeatedComponent, TreeUtil.getComponentWithId(root, repeatedComponent.getId())); }
/** * Retrieves the context for the component with the given Id. * * @param root the root component to search from. * @param id the id to search for. * @param visibleOnly true if process visible only * @return the context for the component with the given id, or null if not found. */ public static ComponentWithContext getComponentWithContextForId(final WComponent root, final String id, final boolean visibleOnly) { FindComponentByIdVisitor visitor = new FindComponentByIdVisitor(id); doTraverse(root, visibleOnly, visitor); return visitor.getResult(); }
/** * Retrieves the context for the component with the given Id. * * @param root the root component to search from. * @param id the id to search for. * @param visibleOnly true if process visible only * @return the context for the component with the given id, or null if not found. */ public static UIContext getContextForId(final WComponent root, final String id, final boolean visibleOnly) { ComponentWithContext comp = getComponentWithContextForId(root, id, visibleOnly); return comp == null ? null : comp.getContext(); }
@Test public void testRefocusWDropdown() { SeleniumWComponentsWebDriver driver = getDriver(); String path = "WDropdownSubmitOnChangeExample/WDropdown[0]"; driver.findWDropdown(byWComponentPath(path)).click(); UIContext uic = getUserContextForSession(); UIContextHolder.pushContext(uic); try { // The dropdowns in the example need something to be selected to trigger the submit WComponent comp = TreeUtil.findWComponent(getUi(), path.split("/")).getComponent(); if (comp instanceof WDropdown) { WDropdown dropdown = (WDropdown) comp; driver.findElement(byWComponentPath(path, dropdown.getOptions().get(1))).click(); } } finally { UIContextHolder.popContext(); } Assert.assertEquals("Incorrect focus", driver.findWDropdown(byWComponentPath(path)).getActiveId(), driver.switchTo(true).activeElement().getAttribute("id")); } }
/** * {@inheritDoc} */ @Override public List<WebElement> findElements(final SearchContext searchContext) { List<WebElement> result = new ArrayList<>(); ComponentWithContext[] components = null; UIContextHolder.pushContext(getContext()); try { components = TreeUtil.findWComponents(getComponent(), path, visibleOnly); } finally { UIContextHolder.popContext(); } if (components.length != 0) { componentClass = components[0].getComponent().getClass(); } for (ComponentWithContext comp : components) { WComponent cmp = comp.getComponent(); UIContext cmpUic = comp.getContext(); UIContextHolder.pushContext(cmpUic); try { List<WebElement> resultForComp = findElement(searchContext, cmpUic, cmp, getValue()); result.addAll(resultForComp); } finally { UIContextHolder.popContext(); } } return result; }