List of usage examples for org.openqa.selenium WebElement getTagName
String getTagName();
From source file:de.betterform.conformance.WebDriverTestFunctions.java
License:BSD License
private boolean hasGrouplayout(WebElement group, String apperance) { boolean validLayout = true; if (WebDriverTestInterface.appearanceFull.equals(apperance)) { if (WebDriverTestInterface.spanTagName.equals(group.getTagName())) { Iterator<WebElement> children = group .findElements(By.className(WebDriverTestInterface.xfControlClass)).iterator(); while (children.hasNext()) { WebElement child = children.next(); if (child.getAttribute("widgetid") != null) { WebElement widget = child .findElement(By.id("widget_" + child.getAttribute("widgetid") + "-value")); validLayout &= "200px".equals(widget.getCssValue("left")); validLayout &= "left".equals(widget.getCssValue("text-align")); }/* w w w .j a v a 2 s.co m*/ validLayout &= WebDriverTestInterface.spanTagName.equals(child.getTagName()); } return validLayout; } } else if (WebDriverTestInterface.appearanceCompact.equals(apperance) || WebDriverTestInterface.appearanceDefault.equals(apperance)) { if (WebDriverTestInterface.spanTagName.equals(group.getTagName())) { Iterator<WebElement> children = group .findElements(By.className(WebDriverTestInterface.xfControlClass)).iterator(); while (children.hasNext()) { WebElement child = children.next(); if (child.getAttribute("widgetid") != null) { WebElement widget = child .findElement(By.id("widget_" + child.getAttribute("widgetid") + "-value")); validLayout &= "start".equals(widget.getCssValue("text-align")); } validLayout &= WebDriverTestInterface.spanTagName.equals(child.getTagName()); } return validLayout; } } else if (WebDriverTestInterface.appearanceHorizontalTable.equals(apperance)) { if (WebDriverTestInterface.tableTagName.equals(group.getTagName())) { final List<WebElement> children = group .findElements(By.tagName(WebDriverTestInterface.tableRowTagName)); if (children.size() == 3) { WebElement labelRow = children.get(1); WebElement valueRow = children.get(2); final List<WebElement> labels = labelRow .findElements(By.tagName(WebDriverTestInterface.tableColTagName)); final List<WebElement> values = valueRow .findElements(By.tagName(WebDriverTestInterface.tableColTagName)); if (labels.size() == values.size()) { for (int i = 0; i < labels.size(); i++) { validLayout &= hasHTMlClass(labels.get(i), "bfHorizontalTableLabel)"); List<WebElement> htmlLabels = labels.get(i) .findElements(By.tagName(WebDriverTestInterface.labelTagName)); validLayout &= (htmlLabels.size() == 1); validLayout &= (hasHTMlClass(htmlLabels.get(0), "bfTableLabel")); //TODO: !!!! } for (int i = 0; i < labels.size(); i++) { validLayout &= hasHTMlClass(values.get(i), "bfHorizontalTableValue)"); //TODO: !!!! } } } } } return false; }
From source file:de.learnlib.alex.data.entities.actions.web.SelectActionTest.java
License:Apache License
@Test public void shouldReturnOkIfValueWasSelectedByValue() { WebElement selectElement = mock(WebElement.class); given(webSiteConnector.getElement(node)).willReturn(selectElement); given(selectElement.getTagName()).willReturn("select"); WebElement itemElement = mock(WebElement.class); List<WebElement> itemElements = new ArrayList<>(); itemElements.add(itemElement);//from w ww . j ava 2 s . c om given(selectElement.findElements(By.xpath(".//option[@value = \"Lorem Ipsum\"]"))).willReturn(itemElements); s.setSelectBy(SelectAction.SelectByType.VALUE); ExecuteResult result = s.executeAction(connectors); assertTrue(result.isSuccess()); verify(itemElement).click(); }
From source file:de.learnlib.alex.data.entities.actions.web.SelectActionTest.java
License:Apache License
@Test public void shouldReturnOkIfValueWasSelectedByText() { WebElement selectElement = mock(WebElement.class); given(webSiteConnector.getElement(node)).willReturn(selectElement); given(selectElement.getTagName()).willReturn("select"); WebElement itemElement = mock(WebElement.class); List<WebElement> itemElements = new ArrayList<>(); itemElements.add(itemElement);/*w w w .ja va2s.c o m*/ given(selectElement.findElements(By.xpath(".//option[normalize-space(.) = \"Lorem Ipsum\"]"))) .willReturn(itemElements); s.setSelectBy(SelectAction.SelectByType.TEXT); ExecuteResult result = s.executeAction(connectors); assertTrue(result.isSuccess()); verify(itemElement).click(); }
From source file:de.learnlib.alex.data.entities.actions.web.SelectActionTest.java
License:Apache License
@Test public void shouldReturnOkIfValueWasSelectedByIndex() { WebElement selectElement = mock(WebElement.class); given(webSiteConnector.getElement(node)).willReturn(selectElement); given(selectElement.getTagName()).willReturn("select"); List<WebElement> itemElements = new ArrayList<>(); given(selectElement.findElements(By.tagName("option"))).willReturn(itemElements); WebElement itemElement = mock(WebElement.class); itemElements.add(itemElement);/*from w w w . ja va 2 s . c o m*/ given(itemElement.getAttribute("index")).willReturn("0"); s.setValue("0"); s.setSelectBy(SelectAction.SelectByType.INDEX); ExecuteResult result = s.executeAction(connectors); assertTrue(result.isSuccess()); verify(itemElement).click(); }
From source file:de.learnlib.alex.data.entities.actions.web.SelectActionTest.java
License:Apache License
@Test public void shouldReturnFailedIfValueIsNotAnIndexNumber() { WebElement selectElement = mock(WebElement.class); given(webSiteConnector.getElement(node)).willReturn(selectElement); given(selectElement.getTagName()).willReturn("select"); s.setValue("definite not a number"); s.setSelectBy(SelectAction.SelectByType.INDEX); ExecuteResult result = s.executeAction(connectors); assertFalse(result.isSuccess());/*from w w w . j a v a 2s . c om*/ }
From source file:de.learnlib.alex.data.entities.actions.web.UploadFileAction.java
License:Apache License
@Override protected ExecuteResult execute(ConnectorManager connector) { final FileStoreConnector fileStore = connector.getConnector(FileStoreConnector.class); final WebSiteConnector webSiteConnector = connector.getConnector(WebSiteConnector.class); final WebElementLocator nodeWithVariables = new WebElementLocator(insertVariableValues(node.getSelector()), node.getType());/*from w w w . j av a2 s .c o m*/ try { final String path = fileStore.getAbsoluteFileLocation(symbol.getProjectId(), fileName); final WebElement el = webSiteConnector.getElement(nodeWithVariables); if (el.getTagName().equals("input") && el.getAttribute("type").equals("file")) { el.sendKeys(path); return getSuccessOutput(); } else { throw new NoSuchElementException("The element is not an input file element."); } } catch (IllegalStateException e) { LOGGER.info(LoggerMarkers.LEARNER, "The file '{}' could not be found in ALEX.", this.fileName); return getFailedOutput(); } catch (NoSuchElementException e) { LOGGER.info(LoggerMarkers.LEARNER, "The element could not be found or is not an input element."); return getFailedOutput(); } catch (Exception e) { LOGGER.info(LoggerMarkers.LEARNER, "The file could not be uploaded for an unknown reason", e); return getFailedOutput(); } }
From source file:edu.uga.cs.clickminer.BrowserEngine.java
License:Open Source License
/** * Activates a clickable a html element from the list of search results and * generates an InteractionRecord object of the interaction. Activating the * element depends on the element's type. Form elements have their fields * filled from the content of the request and submitted. An element besides * an object or an embed whose target attribute is a frame within the * current window has a click event sent to it. In all other cases the url * of the request is submitted to the address bar of a new window. * /*from w w w. j a v a 2 s. co m*/ * @param req * the request * @return true, if successful an element was found and activated, false * otherwise */ private InteractionRecord activateTargetElementFromResults(List<ElementSearchResult> searchResults, PageEntry pageentry, MitmHttpRequest req) { // If the element contains the url in the request then it must be // clickable or submittable // get the names of the frames in the current frames wdriver.switchTo().defaultContent(); List<String> fnames = FrameUtils.getAllFrameNames(wdriver); fnames.add("_self"); fnames.add("_parent"); ElementSearchResult selectedResult = searchResults.get(0); if (!selectedResult.matchesInDefaultFrame()) { FrameUtils.traverseFramePath(wdriver, selectedResult.getFramePath()); } WebElement selelem = null; for (WebElement elem : selectedResult.getMatchingElements()) { if (elem.isDisplayed() && elem.isEnabled()) { selelem = elem; break; } } if (selelem == null) { selelem = selectedResult.getMatchingElements().get(0); } FrameEntry selectedFrame = pageentry.getMatchingFrameEntry(selectedResult.getFramePath().getUrls()); ElementEntry selectedElement = selectedFrame.getMatchingElementEntry(selelem); if (log.isInfoEnabled()) { log.info("Activating element tag: " + selelem.getTagName() + " locator: " + selectedElement.getLocatorString()); } Set<String> oldWinHandles = wdriver.getWindowHandles(); String seltagname = selelem.getTagName().toLowerCase(); // TODO must take the target of the base tag into account. String seltagtarget = selelem.getAttribute("target"); String newWinHandle = null; if (log.isInfoEnabled()) { log.info("Element target: " + seltagtarget); } if (seltagname.equals("form")) { submitFormElement(selelem, req.getContent()); } else if ((fnames.contains(seltagtarget) || (seltagtarget == null && !selectedResult.matchesInDefaultFrame())) && !objectHTMLElements.contains(seltagname) && selelem.isDisplayed() && selelem.isEnabled()) { // The frame is supposed to alter the current page in someway // which could alter the presence of elements within the DOM // and search order of the windows. try { selelem.click(); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Error clicking on element. " + "Opening url in new window.", e); } newWinHandle = this.openUrlInNewWindow(req.getUrl()); } } else { // open all non-form elements in a new window // we do this to deal with force opening tabs // and caching newWinHandle = this.openUrlInNewWindow(req.getUrl()); } // TODO might want to change this from a sleep try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } Set<String> newWinHandles = wdriver.getWindowHandles(); InteractionRecord irecord = null; if (newWinHandles.size() != oldWinHandles.size()) { if (newWinHandle == null) { newWinHandles.removeAll(oldWinHandles); newWinHandle = newWinHandles.iterator().next(); } PageEntry destpage = PageEntry.getPageEntry(wdriver, windowIMap, newWinHandle); irecord = new InteractionRecord(ResultLocation.NEW_WINDOW, req, destpage); } else { PageEntry destpage = PageEntry.getPageEntry(wdriver, windowIMap, pageentry.getWindow().getWindowID()); irecord = new InteractionRecord(ResultLocation.CURRENT_WINDOW, req, destpage); } selectedElement.setSelected(true); irecord.addPageEntry(pageentry); String windowhandle = wdriver.getWindowHandle(); // update the ts for the window we are interacting with windowIMap.updateWindow(windowhandle); framePathIndex.updateIndex(windowhandle, windowIMap.getWindowTimestamp(windowhandle)); // update the ts for any new windows opened Set<String> newWindows = windowIMap.updateNewWindows(windowhandle); for (String window : newWindows) { framePathIndex.updateIndex(window, windowIMap.getWindowTimestamp(window)); } //Dismiss any alerts. try { Alert alert = wdriver.switchTo().alert(); alert.dismiss(); } catch (NoAlertPresentException e) { if (log.isDebugEnabled()) { log.debug("No modal dialogs, skipping"); } } catch (Exception e1) { if (log.isErrorEnabled()) { log.error("Could not dismiss dialog", e1); } } // After the click, must always switch back to the default content. wdriver.switchTo().window(windowhandle); wdriver.switchTo().defaultContent(); return irecord; }
From source file:edu.uga.cs.clickminer.BrowserEngine.java
License:Open Source License
/** * Returns a new list containing only the clickable elements of the source * list. Param elements are substituted with their parent object elements. * Embed elements are filtered unless the match is made on the flashvars * attribute. Form elements are filtered unless the match is made on the * target attribute or any of the possible form event attributes. Base, bdo, br, * frame, frameset, head, html, iframe, meta, param, script, style and title * are filtered out as not clickable. A elements are filtered unless the match * is match on the href attribute or any of the possible keyboard and mouse event * attributes. All other elements are filtered unless the match is made on a * keyboard and mouse event attribute.//from ww w. j a va 2s . com * * See <a * href="http://www.w3schools.com/TAgs/ref_eventattributes.asp">Source * list.</a> * * @param elements * the elements to filter * @param matchval * the value used in the search for matching elements * @return the filtered list */ public List<WebElement> filterNonClickableElements(List<WebElement> elements, String matchval) { List<WebElement> retval = new ArrayList<WebElement>(); //outer: for (WebElement elem : elements) { String tagName = elem.getTagName().toLowerCase(); if (tagName.equals("param")) { WebElement parent = elem.findElement(By.xpath("..")); if (!retval.contains(parent)) { retval.add(parent); } continue; } else if (tagName.equals("embed")) { String attrval = elem.getAttribute("flashvars"); if (attrval == null) { attrval = elem.getAttribute("FlashVars"); } if (!(attrval != null && attrval.contains(matchval))) { continue; } } else if (tagName.equals("form")) { String attrval = elem.getAttribute("action"); if (!(attrval != null && attrval.contains(matchval)) && !this.matchFormEventAttribute(elem, matchval)) { continue; } } else if (nonClickableHTMLElements.contains(tagName)) { if (log.isInfoEnabled()) { log.info("Filtering element due to tag name " + tagName); } continue; } else if (tagName.equals("a")) { String attrval = elem.getAttribute("href"); if (!(attrval != null && attrval.contains(matchval)) && !this.matchMouseKBEventAttribute(elem, matchval)) { if (log.isInfoEnabled()) { log.info("Filtering element with a tag"); } continue; } } else { if (!this.matchMouseKBEventAttribute(elem, matchval)) { if (log.isInfoEnabled()) { log.info("Filtering element with tag name " + tagName); } continue; } } retval.add(elem); } return retval; }
From source file:edu.uga.cs.clickminer.BrowserEngine.java
License:Open Source License
/** * <p>containsClickableElements.</p> *//* www.jav a2s . c o m*/ public boolean containsClickableElements() { boolean retval = false; String tag = null; List<FramePath> allpaths = FrameUtils.findFramePaths(wdriver, null); outer: for (FramePath path : allpaths) { FrameUtils.traverseFramePath(wdriver, path); List<WebElement> elements = wdriver.findElements(By.xpath("//*")); for (WebElement elem : elements) { try { tag = elem.getTagName(); if (!nonClickableHTMLElements.contains(tag)) { if (eventClickableOnlyHTMLElements.contains(tag) && !containsMouseKBEventAttribute(wdriver, elem)) { continue; } retval = true; break outer; } } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Error processing element. Skipping.", e); } } } } wdriver.switchTo().defaultContent(); if (log.isInfoEnabled()) { if (retval) { log.info("Page with url " + wdriver.getCurrentUrl() + " and title " + wdriver.getTitle() + " contains a clickable '" + tag + "' element"); } else { log.info("Page with url " + wdriver.getCurrentUrl() + " and title " + wdriver.getTitle() + " contains a no clickable elements."); } } return retval; }
From source file:edu.uga.cs.clickminer.BrowserEngine.java
License:Open Source License
/** * <p>submitFormElement.</p> * * @param form a {@link org.openqa.selenium.WebElement} object. * @param formvalues a {@link java.lang.String} object. *//*w ww . ja v a2s . com*/ public void submitFormElement(WebElement form, String formvalues) { // TODO must be able to handle multipart form data if (log.isInfoEnabled()) { log.info("Filling out tag: " + form.getTagName() + " " + "id: " + form.getAttribute("id") + " with values:\n" + formvalues); } // parse the request body Map<String, String> formdict = new HashMap<String, String>(); String[] parts = formvalues.split("&"); for (String part : parts) { String[] nameandval = part.split("="); // we decode the values because we want to enter exactly what was // supplied by the user try { formdict.put(nameandval[0], URLDecoder.decode(nameandval[1], "UTF-8")); } catch (UnsupportedEncodingException e) { if (log.isErrorEnabled()) { log.error("Unable to decode form value " + nameandval[1] + " . Skipping value.", e); } } } // fill out the form for (String key : formdict.keySet()) { WebElement formfield = null; try { formfield = form.findElement(By.id(key)); if (log.isDebugEnabled()) { log.debug("Form field " + key + " found by id"); } } catch (NoSuchElementException e) { try { formfield = form.findElement(By.name(key)); if (log.isDebugEnabled()) { log.debug("Form field " + key + " found by name"); } } catch (NoSuchElementException q) { } } if (formfield != null) { if (log.isInfoEnabled()) { log.info("Setting form field " + key + " to value " + formdict.get(key)); } formfield.sendKeys(formdict.get(key)); } else { if (log.isInfoEnabled()) { log.info("Form field " + key + " was not found"); } } } form.submit(); }