List of usage examples for org.openqa.selenium WebElement getLocation
Point getLocation();
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Get the web-element's location relative to the browser client area. If the web-element is inside frames, * the frame's location will also be added.<br> * <b>Note: </b>If the 2th parameter is given false, you might get a more accurate location.<br> * @param webelement WebElement, the element to get location * @param useOnPageFirstly boolean, There are 2 ways to get element's location relative to the page: * one is {@link Coordinates#onPage()}, the other is {@link WebElement#getLocation()}. * it seems that the 2th method ({@link WebElement#getLocation()}) is more accurate. * But historically, we called {@link Coordinates#onPage()} in first place.<br> * If this parameter is true, {@link Coordinates#onPage()} will be used firstly as before.<br> * Otherwise, {@link WebElement#getLocation()} will be used directly<br> * /*from w w w . j a v a2s.co m*/ * @return Point, the element's location relative to the browser client area * @throws SeleniumPlusException */ public static Point getLocation(WebElement webelement, boolean useOnPageFirstly) throws SeleniumPlusException { String debugmsg = StringUtils.debugmsg(false); try { //1. Get the element's location relative to the frame org.openqa.selenium.Point p = null; if (useOnPageFirstly) { try { Coordinates c = ((RemoteWebElement) webelement).getCoordinates(); p = c.onPage(); } catch (UnsupportedOperationException x) { IndependantLog.debug(debugmsg + "Selenium reports coordinates.onPage() is NOT yet supported."); } catch (Throwable t) { IndependantLog.debug(debugmsg + "ignoring " + StringUtils.debugmsg(t)); } } if (p == null) p = webelement.getLocation(); IndependantLog.debug(debugmsg + "Selenium reports the WebElement 'CLIENT AREA' location as (" + p.x + "," + p.y + ")"); //2. Add the frame's location (relative to the the browser client area) if (lastFrame != null) { p.x += lastFrame.getLocation().x; p.y += lastFrame.getLocation().y; //IndependantLog.debug(debugmsg+"added lastFrame offsets, new tentative 'CLIENT AREA' location ("+p.x+","+p.y+")"); } return new Point(p.x, p.y); } catch (Exception e) { IndependantLog.error(debugmsg, e); throw new SeleniumPlusException("getLocation failed: " + StringUtils.debugmsg(e)); } }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Capture component image//from w w w.j a va 2s.c o m * @param we - WebElement object. * @param imgName - Image full path with name * @throws Exception */ public static void captureScreen(WebElement we, String imgName, String fileformat) throws SeleniumPlusException { try { Rectangle rect = new Rectangle(we.getLocation().x, we.getLocation().y, we.getSize().width, we.getSize().height); BufferedImage img = captureBrowserArea(rect); File outputfile = new File(imgName + "." + fileformat); ImageIO.write(img, fileformat, outputfile); } catch (Exception e) { throw new SeleniumPlusException("Failed to capture screenshot " + e.getMessage()); } }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Try to show the webelement on the browser's page.<br> * First, try to see if the web-element is already shown on page, if not shown on the page<br> * then it will try to move the web-element to show it on page.<br> * Finally, if the passed in parameter verify is true, it will return true only if the element * is shown on the page;<br>//ww w . j a v a 2s .com * if the passed in parameter verify is false, it will always return true.<br> * @param element WebElement, the webelement to show. * @param params optional<ul> * <b>optionals[0] verify</b> boolean, verify that the component is shown on page. The default value is false.<br> * </ul> * * @return boolean, true if succeed * * @history * <br> May 18, 2015 sbjlwa Get refreshed WebElement after moving it on the page. */ public static boolean showOnPage(WebElement element, String... optional) { String debugmsg = StringUtils.debugmsg(false); if (isShowOnPage(element)) return true; boolean verify = false; if (optional != null && optional.length > 0) { verify = StringUtilities.convertBool(optional[0]); } try { IndependantLog.debug(debugmsg + "make element visible in brower viewport."); Coordinates coordinate = ((Locatable) element).getCoordinates(); coordinate.inViewPort(); //after inViewPort(), the real element may move, but WebElement.getLocation() may still return the old value if (verify) { IndependantLog.debug(debugmsg + "refreshing element reference for verification."); element = CFComponent.refresh(element); } if (isShowOnPage(element)) return true; } catch (Exception e) { IndependantLog.warn( debugmsg + "Fail to show webelement in browser's viewport, due to " + StringUtils.debugmsg(e)); } try { IndependantLog.debug(debugmsg + "scroll browser to the top-left corner of this component."); org.openqa.selenium.Point compLoc = element.getLocation(); WDLibrary.scrollBrowserWindowTo(compLoc.x, compLoc.y, element); if (verify) { IndependantLog.debug(debugmsg + "refreshing element reference again for verification."); element = CFComponent.refresh(element); } if (isShowOnPage(element)) return true; } catch (Exception e) { IndependantLog.warn(debugmsg + "Fail to scroll browser to the top-left corner of webelement, due to " + StringUtils.debugmsg(e)); } try { IndependantLog.debug(debugmsg + "align the top this component to top of browser's viewport."); WDLibrary.alignToTop(element); if (verify) { IndependantLog.debug(debugmsg + "refreshing element reference yet again for verification."); element = CFComponent.refresh(element); } if (isShowOnPage(element)) return true; } catch (Exception e) { IndependantLog.warn(debugmsg + "Fail to align top of webelement to browser-viewport-top, due to " + StringUtils.debugmsg(e)); } if (verify) { IndependantLog.debug(debugmsg + "check if the element 'Top-Left Corner' is located in the page."); org.openqa.selenium.Point offset = new org.openqa.selenium.Point(1, 1); return isShowOnPage(element, offset); } return true; }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * To check if a certain point of web-element is shown on the page.<br> * If the point is not given or it is out of boundary of web-element, method will<br> * check if the web-element is fully shown on the page.<br> * * @param element WebElement, to check if it is shown on the page * @param offset Point, the offset from Left-Top of web-element; the point to check. can be null * @return boolean if the offest-point or the full-web-element is shown on the page. *///w w w . j a v a 2 s . c o m public static boolean isShowOnPage(WebElement element, org.openqa.selenium.Point offset) { String debugmsg = StringUtils.debugmsg(false); boolean isShowOnPage = false; try { WDLibrary.checkNotNull(element); //The location(Left-Top) is relative to the viewport org.openqa.selenium.Point elementLTLoc = element.getLocation(); Dimension elementD = element.getSize(); int browserXOffset = (int) WDLibrary.lastBrowserWindow.getPageXOffset(); int browserYOffset = (int) WDLibrary.lastBrowserWindow.getPageYOffset(); //move the location according to the page-offset, get the location relative to the page elementLTLoc = elementLTLoc.moveBy(-browserXOffset, -browserYOffset); org.openqa.selenium.Point elementBRLoc = elementLTLoc.moveBy(elementD.width, elementD.height); //Get the bounds of the browser's page int browserClientW = (int) WDLibrary.lastBrowserWindow.getClientWidth(); int browserClientH = (int) WDLibrary.lastBrowserWindow.getClientHeight(); Dimension browserPageBounds = new Dimension(browserClientW, browserClientH); IndependantLog.debug(debugmsg + " offset=" + offset + "; element dimension=" + elementD + "; "); if (offset != null && isLocationInBounds(offset, elementD)) { //check the offset point is shown browser's page elementLTLoc = elementLTLoc.moveBy(offset.x, offset.y); IndependantLog.debug(debugmsg + "check if 'element (Top Left+offset) Corner'=" + elementLTLoc + " is in 'browser window dimension'=" + browserPageBounds + "; "); isShowOnPage = isLocationInBounds(elementLTLoc, browserPageBounds); } else { //if Left-Top and Bottom-Right is in the browser, the element is fully shown IndependantLog.debug(debugmsg + "check if 'element Top Left Corner'=" + elementLTLoc + " and 'Bottom Right Corner'=" + elementBRLoc + " are both in 'browser window dimension'=" + browserPageBounds + "; "); isShowOnPage = isLocationInBounds(elementLTLoc, browserPageBounds) && isPointInBounds(elementBRLoc, browserPageBounds); } } catch (Exception e) { IndependantLog.error(debugmsg + "Fail due to " + StringUtils.debugmsg(e)); } IndependantLog.debug(debugmsg + " return " + isShowOnPage); return isShowOnPage; }
From source file:org.sonarqube.report.extendedpdf.OverviewPDFReporter.java
License:Open Source License
private void captureScreenshot(WebDriver driver, String selector) throws IOException { File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); driver.get(dashboardUrl);//www . jav a 2s .c o m System.out.println("Screenshot captured..."); List<WebElement> elements = driver.findElements(By.cssSelector(selector)); if (!elements.isEmpty()) { WebElement element = elements.get(0); BufferedImage fullImg = ImageIO.read(screenshot); Point point = element.getLocation(); int eleWidth = element.getSize().getWidth(); int eleHeight = element.getSize().getHeight(); BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); ImageIO.write(eleScreenshot, "png", screenshot); System.out.println("Cropping to get widget: " + selector.substring(1)); FileUtils.copyFile(screenshot, new File(screenshotsDir + File.separator + selector.substring(1) + ".png")); } }
From source file:org.specrunner.webdriver.impl.PrepareDefault.java
License:Open Source License
@Override public void prepare(AbstractPluginFind source, WebDriver client, WebElement... elements) { if (client instanceof JavascriptExecutor) { JavascriptExecutor executor = (JavascriptExecutor) client; if (elements != null) { for (WebElement e : elements) { if (e != null) { try { Point location = e.getLocation(); if (location != null) { executor.executeScript("arguments[0].focus()", e); executor.executeScript( "window.scrollTo(" + location.getX() + "," + location.getY() + ")"); }//from w ww. j a va2 s.c o m } catch (WebDriverException ex) { if (UtilLog.LOG.isTraceEnabled()) { UtilLog.LOG.trace(ex.getMessage(), e); } } } } } } }
From source file:org.suren.autotest.web.framework.log.TargetElementMark.java
License:Apache License
@Override public void mark(WebElement ele, File file) throws IOException { BufferedImage bufImg = ImageIO.read(file); try {//from w w w . j ava 2s.c om WebElement webEle = (WebElement) ele; Point loc = webEle.getLocation(); Dimension size = webEle.getSize(); Graphics2D g = bufImg.createGraphics(); g.setColor(Color.red); g.drawRect(loc.getX(), loc.getY(), size.getWidth(), size.getHeight()); } catch (StaleElementReferenceException se) { } }
From source file:org.suren.autotest.web.framework.selenium.action.SeleniumHover.java
License:Apache License
@Override public void hover(Element ele) { WebElement webEle = searchStrategyUtils.findStrategy(WebElement.class, ele).search(ele); if (webEle == null) { logger.warn("can not found element."); return;//w w w . j av a 2 s . com } if (!(ele instanceof FileUpload)) { Dimension size = webEle.getSize(); Point loc = webEle.getLocation(); int toolbarHeight = engine.getToolbarHeight(); int x = size.getWidth() / 2 + loc.getX(); int y = size.getHeight() / 2 + loc.getY() + toolbarHeight; try { new Robot().mouseMove(x, y); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:org.uiautomation.ios.selenium.interactions.touch.TouchDoubleTapTest.java
License:Apache License
@Test public void testCanDoubleTapOnAnImageAndAlterLocationOfElementsInScreen() { driver.get(pages.longContentPage);/*from w ww. j a va 2s . co m*/ WebElement image = driver.findElement(By.id("imagestart")); int y = image.getLocation().getY(); // The element is located at a certain point, after double tapping, // the y coordinate must change. assertTrue(y < 150); doubleTapOnElement("imagestart"); //doubleTapOnElement("imagestart"); y = image.getLocation().y; assertTrue(y > 150); }
From source file:org.uiautomation.ios.selenium.interactions.touch.TouchFlickTest.java
License:Apache License
@NeedsFreshDriver @Test//from w w w . j ava 2s . c o m public void testCanFlickHorizontallyFromWebElement() { driver.get(pages.longContentPage); WebElement toFlick = driver.findElement(By.id("imagestart")); WebElement link = driver.findElement(By.id("link1")); int originalX = link.getLocation().x; // The element is located at the right of the page, // so it is not initially visible on the screen. Action flick = getBuilder(driver).flick(toFlick, -1000, 0, FlickAction.SPEED_NORMAL).build(); flick.perform(); int newX = link.getLocation().x; // After flicking, the element should now be visible on the screen. assertTrue("Expected x < " + originalX + ", but got x = " + newX, newX < originalX); }