/** * Creates a ByWComponentPath which searches for a path to a component. * * @param componentWithContext the component to search for. * @param path the path to traverse. */ public ByWComponentPath(final ComponentWithContext componentWithContext, final String path) { this(componentWithContext.getComponent(), componentWithContext.getContext(), path); }
/** * Creates a ByWComponent which searches for a component instance in the given context. If searching for a repeated * component, the <code>context</code> should be the row context for the row you want to return. * * @param componentWithContext the component to search for. */ public ByWComponent(final ComponentWithContext componentWithContext) { this(componentWithContext.getComponent(), componentWithContext.getContext(), null); }
/** * Creates a ByWComponentPath which searches for a path to a component. * * @param componentWithContext the component to search for. * @param path the path to traverse. * @param visibleOnly only look for components that are visible */ public ByWComponentPath(final ComponentWithContext componentWithContext, final String path, final boolean visibleOnly) { this(componentWithContext.getComponent(), componentWithContext.getContext(), path, visibleOnly); }
/** * 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(); }
/** * Indicates whether the list contains the given component and context. * * @param components the list of components to check. * @param component the component to search for. * @param uic the component context to search for. * @return true if the component is in the list with the given context, false otherwise. */ private boolean isInList(final List<ComponentWithContext> components, final WComponent component, final UIContext uic) { for (ComponentWithContext comp : components) { if (comp.getComponent() == component && comp.getContext() == uic) { return true; } } return false; }
/** * {@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; }
/** * Override getStep to retrieve the step from a WWindow if the targeted component is a WWindow or a descendant * of one. * * @return the step count. */ @Override public int getStep() { // WWindows require a special case, as they keep track of their own separate step. // We must check if the request is an AJAX or content target inside a WWindow, // and if so return that WWindow's step. Otherwise, we return the step on the // environment. UIContextHolder.pushContext(window.getContext()); try { WWindow targetWindow = (WWindow) window.getComponent(); return targetWindow.getStep(); } finally { UIContextHolder.popContext(); } }
/** * Override setStep to store the step on a WWindow if the targeted component is a WWindow or a descendant of * one. * * @param step the step count to set. */ @Override public void setStep(final int step) { // WWindows require a special case, as they keep track of their own separate step. // We must check if the request is an AJAX or content target inside a WWindow, // and if so set that WWindow's step. Otherwise, we set the step on the // environment. UIContextHolder.pushContext(window.getContext()); try { WWindow targetWindow = (WWindow) window.getComponent(); targetWindow.setStep(step); } finally { UIContextHolder.popContext(); } } }
/** * Retrieves the application title for the given context. This mess is required due to WWindow essentially existing * as a separate UI root. If WWindow is eventually removed, then we can just retrieve the title from the root * WApplication. * * @param uic the context to check. * @return the current application title. */ private String getApplicationTitle(final UIContext uic) { WComponent root = uic.getUI(); String title = root instanceof WApplication ? ((WApplication) root).getTitle() : null; Map<String, String> params = uic.getEnvironment().getHiddenParameters(); String target = params.get(WWindow.WWINDOW_REQUEST_PARAM_KEY); if (target != null) { ComponentWithContext targetComp = WebUtilities.getComponentById(target, true); if (targetComp != null && targetComp.getComponent() instanceof WWindow) { try { UIContextHolder.pushContext(targetComp.getContext()); title = ((WWindow) targetComp.getComponent()).getTitle(); } finally { UIContextHolder.popContext(); } } } return title == null ? "" : title; }
/** * Check if this ID is focusable. * <p> * Considered focusable if the component and all its ancestors are visible and not hidden. * </p> * * @param root the root component to search from. * @param id the id to search for. * @return the component with context if it is focusable, otherwise null */ public static boolean isIdFocusable(final WComponent root, final String id) { FindComponentByIdVisitor visitor = new FindComponentByIdVisitor(id) { @Override public VisitorResult visit(final WComponent comp) { VisitorResult result = super.visit(comp); // If hidden then abort branch if (result == VisitorResult.CONTINUE && comp.isHidden()) { return VisitorResult.ABORT_BRANCH; } return result; } }; // Only traverse visible doTraverse(root, true, visitor); // Check if matching component is hidden ComponentWithContext result = visitor.getResult(); return result == null ? false : !result.getComponent().isHidden(); }
WComponent trigger = triggerWithContext.getComponent();
/** * {@inheritDoc} */ @Override public void serviceRequest(final Request request) { // Check if this is a targeted request targetId = request.getParameter(Environment.TARGET_ID); if (targetId == null) { throw new SystemException("No target id request parameter"); } ComponentWithContext target = WebUtilities.getComponentById(targetId, true); if (target == null) { throw new SystemException("No target component found for id " + targetId); } // go straight to the target component. attachUI(target.getComponent()); UIContextHolder.pushContext(target.getContext()); try { super.serviceRequest(request); } finally { UIContextHolder.popContext(); } }
xml.appendClose(); target.getComponent().paint(renderContext);
ComponentWithContext target = WebUtilities.getComponentById(targetId, true); if (target != null) { writeDebugInfo(target.getComponent(), xml);
@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")); } }
@Test public void testAutoReFocusWDropdowns() { SeleniumWComponentsWebDriver driver = getDriver(); String[] paths = { ROOT_PATH + "[0]/AutoReFocusExample/WDropdown", ROOT_PATH + "[1]/AutoReFocusExample/WDropdown" }; for (String path : paths) { driver.findWDropdown(byWComponentPath(path)).getInputField().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("/"), false).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")); } }
/** * submit on change test. */ @Test public void testExampleSubmitOnChange() { WDropdownOptionsExample example = (WDropdownOptionsExample) getUi(); // Launch the web browser to the LDE SeleniumWComponentsWebDriver driver = getDriver(); WDropdown.DropdownType type = WDropdown.DropdownType.NATIVE; configureDropDown(driver, type, 4); UIContext uic = getUserContextForSession(); UIContextHolder.pushContext(uic); try { WDropdown dropdown = (WDropdown) TreeUtil.findWComponent(example, new String[]{"WDropdown"}).getComponent(); List<?> options = dropdown.getOptions(); for (Object option : options) { driver.findElement(byWComponent(dropdown, option)).click(); Assert.assertEquals("Incorrect option selected", option, dropdown.getSelected()); Assert.assertEquals("Incorrect text field text", option, driver.findElement(byWComponentPath("WDropdownOptionsExample/WPanel[1]")).getText()); } } finally { UIContextHolder.popContext(); } }
@Test public void testExample() { WDropdownSpecialCharHandlingExample example = (WDropdownSpecialCharHandlingExample) getUi(); // Launch the web browser to the LDE SeleniumWComponentsWebDriver driver = getDriver(); UIContext uic = getUserContextForSession(); UIContextHolder.pushContext(uic); try { WDropdown dropdown = (WDropdown) TreeUtil.findWComponent(example, new String[]{"WDropdown"}). getComponent(); List<?> options = dropdown.getOptions(); for (Object option : options) { driver.findElement(byWComponent(dropdown, option)).click(); driver.findElement(byWComponentPath("WButton")).click(); Assert.assertEquals("Incorrect option selected", option, dropdown.getSelected()); Assert.assertEquals("Incorrect text field text", (option == null ? "" : option), driver.findWTextField(byWComponentPath("WTextField")).getValue()); } } finally { UIContextHolder.popContext(); } } }
try { WDropdown dropdown = (WDropdown) TreeUtil.findWComponent(example, new String[]{"WDropdown"}) .getComponent(); List<?> options = dropdown.getOptions();
@Test public void testDropdown() { WDropdownSpaceHandlingExample example = (WDropdownSpaceHandlingExample) getUi(); // Launch the web browser to the LDE SeleniumWComponentsWebDriver driver = getDriver(); UIContext uic = getUserContextForSession(); UIContextHolder.pushContext(uic); try { WDropdown dropdown = (WDropdown) TreeUtil.findWComponent(example, new String[]{"WDropdown"}). getComponent(); List<?> options = dropdown.getOptions(); for (Object option : options) { driver.findElement(byWComponent(dropdown, option)).click(); driver.findWButtonByText("Submit").click(); Assert.assertEquals("Incorrect option selected", option, dropdown.getSelected()); } } finally { UIContextHolder.popContext(); } } }