List of usage examples for org.openqa.selenium WebElement getLocation
Point getLocation();
From source file:org.julianharty.accessibility.automation.KeyboardHelpers.java
License:Apache License
/** * Uses the tab key to navigate around a web page. * * Helps determine which elements are reachable with the tab key and * whether the first active element can be reached again (indicating we * can 'loop' around the tabable elements on the page. * * Any iFrames are visited recursively./*from ww w .j a va 2 s.c o m*/ * * A sample call follows: * <code><br/> public void testTabbingThroughGoogleSearchResults() throws InterruptedException { <br/> FirefoxDriver driver = new FirefoxDriver(); <br/> driver.get("http://www.google.com/search?q=cheese"); <br/> int maxTabsToEnter = 300; <br/> int tabs = KeyboardHelpers.tabThroughWebPage(driver, maxTabsToEnter);<br/> assertTrue("Expected at least 50 tabs, only needed " + tabs, tabs > 50);<br/> } </code> * * @param driver a WebDriver connection with the target page loaded. * @param maxTabsToEnter the maximum tabs we should need to visit all the * tabable elements. Consider providing a 2x or 3x the expected number, * particularly for complex, unknown content. * @return the number of tabs issued if the method reached an element that * matched the first active element, else -1 if it failed to complete * within the specified number of tabs. * @throws InterruptedException if the call to sleep fails. */ public static int tabThroughWebPage(WebDriver driver, int maxTabsToEnter) throws InterruptedException { WebElement firstElement = driver.switchTo().activeElement(); WebElement currentElement = firstElement; currentSize = firstElement.getSize(); int tabsIssued = 0; int iFrame = 0; String currentTagName = "(not set)"; try { while (tabsIssued < (maxTabsToEnter)) { Point currentLocation = currentElement.getLocation(); logDetailsOfWebElement(currentElement, currentLocation, tabsIssued); currentTagName = currentElement.getTagName(); /* * for iframes switch to the iframe and call ourself recursively. We * think it always starts from _top of the iframe. That call will * tab until it 'falls out' of the active elements in the iframe. * Body will then be returned, then we need to switch back to the * parent. * * Note: use periods to separate nested frames. */ if (currentTagName.equals("iframe")) { driver.switchTo().frame(iFrame++); // A simple calculation to limit tabs in a 'broken' iFrame for now. tabThroughWebPage(driver, maxTabsToEnter - tabsIssued); // Will the following skip over the 'body' of the iFrame? driver.switchTo().defaultContent(); currentElement = driver.switchTo().activeElement(); } String currentTitle = getTitleOfCurrentElement(driver, currentElement); // Here is one of the termination conditions, if we find one of our titles. if (currentTitle.contains(TAB_KEYWORD + 0)) { logTerminationCondition( String.format("Title %s of element matches the value set", currentTitle), currentElement, tabsIssued); return tabsIssued; } setWebElementAttributesAsAppropriate(driver, currentElement, tabsIssued, currentTagName); currentElement.sendKeys(Keys.TAB); // "\t" also works tabsIssued++; // TODO 2011Aug16 Don't sleep with IE driver, it's too slow // Thread.sleep(500L); String previousTagName = currentTagName; Point previousLocation = currentLocation; Dimension previousSize = currentSize; currentElement = driver.switchTo().activeElement(); currentLocation = currentElement.getLocation(); currentSize = currentElement.getSize(); currentTagName = currentElement.getTagName(); /* Loop termination is still imprecise. Typically the first element * is the body element, however in some GWT applications it's a div * element for reasons I don't yet fathom. * TODO(jharty): improve the precision and reliability of the * matching as we learn more about how to interact with elements * on the page. */ if (GeneralHelpers.locationMatches(currentLocation, previousLocation) && GeneralHelpers.dimensionsAreEqual(currentSize, previousSize) && currentTagName.equals(previousTagName) && !currentTagName.equals("body")) { logTerminationCondition(String.format( "Bad news: Element %s seems to match %s after tabbing. Are NativeEvents working?", currentTagName, previousTagName), currentElement, tabsIssued); // Tell the user to check native events are working return -1; } if (currentElement.equals(firstElement) && tabsIssued >= 3) { logTerminationCondition("Current element matches first element", currentElement, tabsIssued); return tabsIssued; } } } catch (WebDriverException wde) { String innerHTML = (String) ((JavascriptExecutor) driver) .executeScript("return arguments[0].innerHTML;", currentElement); LOG.warning(String.format("Current Tag %s, InnerHTML for problem element is %s", currentTagName, innerHTML)); throw wde; } return -1; }
From source file:org.julianharty.accessibility.automation.LayoutAndOrdering.java
License:Apache License
/** * Tests how the reported location of an element varies as the font size grows. * //from www.j a va 2 s .c om * In theory, I would have expected the offset to either remain constant, * or to increase as the rendered font size increases. However, the * debug text from this test indicates the co-ordinates vary slightly, and * may increase or decrease after the font size is changed. */ public void testOffsetIncreasesAfterControlPlusKeyStroke() { driver.get("http://localhost:" + port + "/explicittabindex.html"); WebElement firstElement = driver.switchTo().activeElement(); WebElement currentElement = firstElement; // Go to first tabbable element on page currentElement.sendKeys(Keys.TAB); currentElement = driver.switchTo().activeElement(); Point initialElementLocation = currentElement.getLocation(); System.out.println("testOffsetIncreasesAfterControlPlusKeyStroke"); int maxChanges = 5; // First increase the rendered font size Keys modifierKey = Keys.ADD; applyModifierKeyTo(currentElement, maxChanges, modifierKey); // Now decrease the rendered font size modifierKey = Keys.SUBTRACT; applyModifierKeyTo(currentElement, maxChanges, modifierKey); }
From source file:org.julianharty.accessibility.automation.LayoutAndOrdering.java
License:Apache License
/** * A hack that applies a modifier key several times to a web browser. * /*w w w . j ava 2s. c om*/ * The aim is to increase or decrease the rendered font size and determine * how the reported location of a given web element varies. * * TODO(jharty): enable an ordered set of keys to be passed in by the * caller e.g. Keys.CONTROL, Keys.ADD * * TODO(jharty): find a way to compare the locations for a set of points. * This may become more generalised to allow a caller to pass in either a * generated set of points (e.g. a result of applying CTRL+ several times) * or a pre-determined set of points e.g. for known locations of a set of * elements on a web page. * * TODO(jharty): hmmm, needs refactoring anyway as the comparison of points * needs to reflect the direction of the change. Consider creating a * Direction enum with 2 values: INCREASE, DECREASE, which would obviate * the need to pass in a set of keystrokes and make the method tightly * focused. * * @param currentElement the element that currently has focus. * @param maxChanges the number of times the keystroke will be applied. * @param modifierKey the key to apply, currently expected to be Keys.ADD * or Keys.SUBTRACT */ private void applyModifierKeyTo(WebElement currentElement, int maxChanges, Keys modifierKey) { Point currentElementLocation = currentElement.getLocation(); for (int i = 0; i < maxChanges; i++) { currentElement.sendKeys(Keys.CONTROL, modifierKey); Point newLocation = currentElement.getLocation(); System.out.println(GeneralHelpers.printElementLocations(i, currentElementLocation, newLocation)); if (GeneralHelpers.compareElementLocationsForSaneTabOrder(currentElementLocation, newLocation, tolerance)) { fail(GeneralHelpers.printElementLocations(i, currentElementLocation, newLocation)); } currentElementLocation = newLocation; } }
From source file:org.julianharty.accessibility.automation.LayoutAndOrdering.java
License:Apache License
private void tabLayoutTest(WebDriver driver) throws InterruptedException { WebElement bodyElement = driver.findElement(By.tagName("body")); WebElement firstElement = driver.switchTo().activeElement(); WebElement currentElement = firstElement; Point preTabLocation = currentElement.getLocation(); int tabsIssued = 0; while (tabsIssued < MAX_TABS) { // currentElement.sendKeys(Keys.CONTROL, Keys.ADD); currentElement.sendKeys(Keys.TAB); // "\t" also works tabsIssued++;/*from www. j ava2 s . c om*/ Thread.sleep(50L); currentElement = driver.switchTo().activeElement(); currentElement.sendKeys(Keys.TAB); currentElement = driver.switchTo().activeElement(); Point postTabLocation = currentElement.getLocation(); System.out.println(GeneralHelpers.printElementLocations(tabsIssued, preTabLocation, postTabLocation)); if (GeneralHelpers.locationMatches(postTabLocation, preTabLocation)) { // log termination condition; // Tell the user to check native events are working throw new InterruptedException("We don't seem to have moved, abandoning this test."); } if (currentElement.equals(firstElement) || currentElement.getLocation().equals(firstElement.getLocation())) { // Declare victory :) System.out.println("!!!!!! Yay!!!"); return; } if (GeneralHelpers.compareElementLocationsForSaneTabOrder(preTabLocation, postTabLocation, tolerance)) { fail(GeneralHelpers.printElementLocations(tabsIssued, preTabLocation, postTabLocation)); } preTabLocation = postTabLocation; } }
From source file:org.julianharty.accessibility.automation.Util.java
License:Apache License
/** /*w w w . j av a 2 s. c om*/ * Determines if the given {@code WebElement} is displayed on the web page * (and also on screen). * * @param elem candidate {@code WebElement} to be tested as a decorated link * @return {@code true} if the {@code WebElement} is displayed and has * non-negative x and y location coordinates, {@code false} otherwise */ public static boolean isDisplayedOnWebPage(WebElement elem) { // Get element's location, to determine if it's located outside the // viewport. Point p = elem.getLocation(); if (elem.isDisplayed() && (p.x >= 0 || p.y >= 0)) return true; return false; }
From source file:org.mousephenotype.cda.selenium.support.TestUtils.java
License:Apache License
/** * Scrolls <code>element</code> to the top * @param driver <code>WebDriver</code> instance * @param element Element to scroll to top * @param yOffsetInPixels An <code>Integer</code> which, if not null and not 0, * first scrolls the element to the top, then further scrolls it <code> * yOffsetInPixels</code> pixels down (if negative number) or up (if * positive)./*from w w w.j a v a 2 s .co m*/ */ public void scrollToTop(WebDriver driver, WebElement element, Integer yOffsetInPixels) { Point p = element.getLocation(); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); if ((yOffsetInPixels != null) && (yOffsetInPixels != 0)) { ((JavascriptExecutor) driver) .executeScript("window.scroll(" + p.getX() + "," + (p.getY() + yOffsetInPixels) + ");"); } commonUtils.sleep(100); }
From source file:org.mousephenotype.www.testing.model.TestUtils.java
License:Apache License
/** * Scrolls <code>element</code> to the top * @param driver <code>WebDriver</code> instance * @param element Element to scroll to top * @param yOffsetInPixels An <code>Integer</code> which, if not null and not 0, * first scrolls the element to the top, then further scrolls it <code> * yOffsetInPixels</code> pixels down (if negative number) or up (if * positive).//from w w w.j av a 2s . c o m */ public static void scrollToTop(WebDriver driver, WebElement element, Integer yOffsetInPixels) { Point p = element.getLocation(); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); if ((yOffsetInPixels != null) && (yOffsetInPixels != 0)) { ((JavascriptExecutor) driver) .executeScript("window.scroll(" + p.getX() + "," + (p.getY() + yOffsetInPixels) + ");"); } sleep(100); }
From source file:org.mule.modules.selenium.SeleniumModule.java
License:Open Source License
/** * Where on the page is the top left-hand corner of the rendered element? * <p/>// w w w . j av a 2s.c om * {@sample.xml ../../../doc/mule-module-selenium.xml.sample selenium:get-location} * * @param element Element located at the payload of the message * @return A point, containing the location of the top left-hand corner of the element */ public Point getLocation(@Payload WebElement element) { return element.getLocation(); }
From source file:org.oneandone.qxwebdriver.ui.core.scroll.AbstractScrollArea.java
License:LGPL
public Boolean isChildInView(WebElement child) { Point paneLocation = contentElement.getLocation(); int paneTop = paneLocation.getY(); int paneLeft = paneLocation.getX(); Dimension paneSize = contentElement.getSize(); int paneHeight = paneSize.height; int paneBottom = paneTop + paneHeight; int paneWidth = paneSize.width; int paneRight = paneLeft + paneWidth; Point childLocation = child.getLocation(); int childTop = childLocation.getY(); int childLeft = childLocation.getX(); if (childTop >= paneTop && childTop < paneBottom && childLeft >= paneLeft && childLeft < paneRight) { return true; }//from www. jav a 2s.c o m return false; }
From source file:org.oneandone.qxwebdriver.ui.mobile.core.WidgetImpl.java
License:LGPL
protected static Point getCenter(WebElement element) { Dimension size = element.getSize(); int halfWidth = size.getWidth() / 2; int halfHeight = size.getHeight() / 2; Point loc = element.getLocation(); int posX = loc.getX() + halfWidth; int posY = loc.getY() + halfHeight; Point point = new Point(posX, posY); return point; }