/** * Returns Locations of input element. * * @see Locations */ public static Locations getLocations(WebElement root) { Point topLeft = root.getLocation(); Dimension dimension = root.getSize(); Point topRight = topLeft.moveBy(dimension.getWidth(), 0); Point bottomRight = topRight.moveBy(0, dimension.getHeight()); Point bottomLeft = topLeft.moveBy(0, dimension.getHeight()); return new Locations(topLeft, topRight, bottomLeft, bottomRight); }
/** * Returns Locations of input element. * * @see Locations */ public static Locations getLocations(WebElement root) { Preconditions.checkNotNull(root, "The element cannot be null."); Point topLeft = root.getLocation(); Dimension dimension = root.getSize(); Point topRight = topLeft.moveBy(dimension.getWidth(), 0); Point bottomRight = topRight.moveBy(0, dimension.getHeight()); Point bottomLeft = topLeft.moveBy(0, dimension.getHeight()); return new Locations(topLeft, topRight, bottomLeft, bottomRight); }
private static Dimension countRandomSize(Dimension minimum, Dimension maximum) { double k = Math.random(); int width = (int) (k * (Math.abs(maximum.getWidth() - minimum.getWidth())) + Math.min(maximum.getWidth(), minimum.getWidth())); int height = (int) (k * (Math.abs(maximum.getHeight() - minimum.getHeight())) + Math.min(maximum.getHeight(), minimum.getHeight())); return new Dimension(width, height); }
public static int getElementHeight(WebDriver webDriver, String locator) { WebElement webElement = getWebElement(webDriver, locator, "1"); Dimension dimension = webElement.getSize(); return dimension.getHeight(); }
@JavascriptEnabled @Test @Ignore( value = {SAFARI, MARIONETTE}, reason = "Advanced mouse actions only implemented in rendered browsers", issues = {4136}) @NotYetImplemented(HTMLUNIT) @NoDriverAfterTest public void testCanMoveOverAndOutOfAnElement() { driver.get(pages.mouseOverPage); WebElement greenbox = driver.findElement(By.id("greenbox")); WebElement redbox = driver.findElement(By.id("redbox")); Dimension size = redbox.getSize(); new Actions(driver).moveToElement(greenbox, 1, 1).perform(); assertEquals( Colors.GREEN.getColorValue(), Color.fromString(redbox.getCssValue("background-color"))); new Actions(driver).moveToElement(redbox).perform(); assertEquals( Colors.RED.getColorValue(), Color.fromString(redbox.getCssValue("background-color"))); // IE8 (and *only* IE8) requires a move of 2 pixels. All other browsers // would be happy with 1. new Actions(driver).moveToElement(redbox, size.getWidth() + 2, size.getHeight() + 2).perform(); assertEquals( Colors.GREEN.getColorValue(), Color.fromString(redbox.getCssValue("background-color"))); }
public File takeScreenshot(WebElement element) { if (!WebDriverRunner.hasWebDriverStarted()) { log.warning("Cannot take screenshot because browser is not started"); return null; } WebDriver webdriver = getWebDriver(); if (!(webdriver instanceof TakesScreenshot)) { log.warning("Cannot take screenshot because browser does not support screenshots"); return null; } File screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE); Point p = element.getLocation(); Dimension elementSize = element.getSize(); try { BufferedImage img = ImageIO.read(screen); BufferedImage dest = img.getSubimage(p.getX(), p.getY(), elementSize.getWidth(), elementSize.getHeight()); ImageIO.write(dest, "png", screen); File screenshotOfElement = new File(generateScreenshotFileName()); FileUtils.copyFile(screen, screenshotOfElement); return screenshotOfElement; } catch (IOException e) { printOnce("takeScreenshotImage", e); return null; } }
// If this method is called inside a test the script will check if the device has a resolution // lower than 500x500 and if so will use // a different set of images when trying to find a image. These images are located in // /queryimages/low_res public void setQueryImageFolder() { Dimension size = driver.manage().window().getSize(); log("Screen size: " + size.toString()); if ((size.getHeight() <= 500) || (size.getWidth() <= 500)) { queryimageFolder = "low_res/"; } }
// TODO: experimental method public Point correctAndroidCoordinates(Point appium_coord) throws Exception { Dimension appium_dimensions = driver.manage().window().getSize(); int appium_screenWidth = appium_dimensions.getWidth(); int appium_screenHeight = appium_dimensions.getHeight(); Dimension adb_dimension = getScreenSizeADB(); int adb_screenWidth = adb_dimension.getWidth(); int adb_screenHeight = adb_dimension.getHeight(); double x_offset = appium_coord.x / appium_screenWidth; double y_offset = appium_coord.y / appium_screenHeight; log("x_offset is : " + x_offset); log("y_offset is : " + y_offset); return new Point(x_offset * adb_screenWidth, y_offset * adb_screenHeight); }
private boolean isOutsideOfWindow(WebElement tooltip) { if (!tooltip.isDisplayed()) { return true; } // The tooltip is shown, at least partially, if // its intervals of both horizontal and vertical coordinates // overlap those of the window. Point topLeft = tooltip.getLocation(); Dimension tooltipSize = tooltip.getSize(); Dimension windowSize = driver.manage().window().getSize(); int xLeft = topLeft.getX(); int yTop = topLeft.getY(); int xRight = xLeft + tooltipSize.getWidth() - 1; int yBottom = yTop + tooltipSize.getHeight() - 1; boolean overlapHorizontally = !(xRight < 0 || xLeft >= windowSize.getWidth()); boolean overlapVertically = !(yBottom < 0 || yTop >= windowSize.getHeight()); return !(overlapHorizontally && overlapVertically); }
@Override public void reserveWindowRect(String testStepNo) { if (windowSizeCheckNoSet.contains(testStepNo)) { log.debug("ウィンドウ位置、サイズを取得します"); Point winPos = seleniumDriver.manage().window().getPosition(); Dimension winSize = seleniumDriver.manage().window().getSize(); current.setWindowRect(winPos.getX(), winPos.getY(), winSize.getWidth(), winSize.getHeight()); } }
private void tapXTimes(int numberOfClicks) { Dimension dimensions = service.getDriver().manage().window().getSize(); for (int index = 0; index < numberOfClicks; index++) { service.tap(dimensions.getWidth() / 2, dimensions.getHeight() / 2); if (index == 0) { service.implicitWait(100); } } }
@Test public void maximize() { // declare varibales for windows org.openqa.selenium.Dimension defaultDim; org.openqa.selenium.Dimension maximizeDim; // Load google website on browser driver.get("http://google.com"); // Display the current screen dimensions defaultDim = driver.manage().window().getSize(); System.out.println("screenHeight before maximizing " + defaultDim.getHeight()); System.out.println("screenWidth before maximizing " + defaultDim.getWidth()); // maximize the window using webdriver method driver.manage().window().maximize(); // Display the maximized window dimensions maximizeDim = driver.manage().window().getSize(); System.out.println("screenHeight after maximizing: " + maximizeDim.getHeight()); System.out.println("screenWidth after maximizing: " + maximizeDim.getWidth()); }
public void swipe(double startX, double startY, double endX, double endY) throws Exception { TouchActions actions = new TouchActions(driver); Dimension size = driver.manage().window().getSize(); Point screen = new Point(size.getWidth(), size.getHeight()); Point swipe_start = new Point(screen.x * startX, screen.y * startY); Point swipe_end = new Point(screen.x * endX, screen.y * endY); if (platformName.equalsIgnoreCase("Android")) { androidSwipe((int) swipe_start.x, (int) swipe_start.y, (int) swipe_end.x, (int) swipe_end.y); } else { iOSSwipe((int) swipe_start.x, (int) swipe_start.y, (int) swipe_end.x, (int) swipe_end.y); } }
// Taps and holds on relative coordinates on the screen. public void tapAndHoldOnScreen(double x_offset, double y_offset, int duration) throws Exception { duration = duration * 1000; Dimension size = driver.manage().window().getSize(); Point middle = new Point(size.getWidth(), size.getHeight()); Point middleWithOffset = new Point(middle.x * x_offset, middle.y * y_offset); log("Coordinates with offset are: " + middleWithOffset.x + ", " + middleWithOffset.y); if (automationName.equalsIgnoreCase("selendroid")) { selendroidTapAtCoordinate((int) middleWithOffset.x, (int) middleWithOffset.y, duration); } else { driver.tap(1, (int) middleWithOffset.x, (int) middleWithOffset.y, duration); } }
// Performs a drag and drop from the middle of the "object" image to the middle of the screen. public void dragImageToMiddle(String object) throws Exception { Dimension size = driver.manage().window().getSize(); Point middle = new Point(size.getWidth() / 2 - 20, size.getHeight() / 2 + 20); Point[] object_coord = findImageOnScreen(object, 10); int startX = (int) object_coord[4].x; int startY = (int) object_coord[4].y; int endX = (int) middle.x; int endY = (int) middle.y; if (automationName.equalsIgnoreCase("selendroid")) { androidSwipe(startX, startY, endX, endY); } else { iOSSwipe(startX, startY, endX, endY); } }
// Taps at relative coordinates on the screen. // "x_offset" and "y_offset" set the X and Y coordinates. // "taps" sets the number of taps that are performed. // "frequency" sets the frequency of the taps. public void tapAtRelativeCoordinates(double x_offset, double y_offset, int taps, double frequency) throws Exception { Dimension size = driver.manage().window().getSize(); Point middle = new Point(size.getWidth(), size.getHeight()); Point middleWithOffset = new Point(middle.x * x_offset, middle.y * y_offset); log( "Tapping at coordinates: " + middleWithOffset.toString() + " when size of the screen is: " + size.toString()); for (int i = 0; i < taps; i++) { if (automationName.equalsIgnoreCase("selendroid")) { selendroidTapAtCoordinate((int) middleWithOffset.x, (int) middleWithOffset.y, 1); } else { driver.tap(1, (int) middleWithOffset.x, (int) middleWithOffset.y, 1); } sleep(frequency); } }
// Performs a drag and drop from the middle of the "object" image to the specified relative // coordinates of the screen. public void dragImageToCoordinates(String object, double x_offset, double y_offset) throws Exception { Dimension size = driver.manage().window().getSize(); Point screen = new Point(size.getWidth(), size.getHeight()); Point swipe_end = new Point(screen.x * x_offset, screen.y * y_offset); Point[] object_coord = findImageOnScreen(object, 10); int startX = (int) object_coord[4].x; int startY = (int) object_coord[4].y; int endX = (int) swipe_end.x; int endY = (int) swipe_end.y; if (automationName.equalsIgnoreCase("selendroid")) { androidSwipe(startX, startY, endX, endY); } else { iOSSwipe(startX, startY, endX, endY); } }
// Taps at given coordinates given in pixels public void tapAtCoordinates(int x, int y) throws Exception { if (automationName.equalsIgnoreCase("selendroid")) { selendroidTapAtCoordinate(x, y, 1); } else if (platformName.equalsIgnoreCase("Android")) { Dimension size = driver.manage().window().getSize(); if (y > size.getHeight() || x > size.getWidth()) { log("using adb tap at " + x + ", " + y); try { // run eclipse from commandline to get path variable correct and find adb Process p = Runtime.getRuntime().exec("adb -s " + udid + " shell input tap " + x + " " + y); p.waitFor(); } catch (Exception e) { log(e.toString()); } } else { driver.tap(1, x, y, 1); } } else { driver.tap(1, x, y, 1); } }
// Performs a vertical swipe starting from a relative coordinate of the device screen. // "distance" is given in pixels. A positive "distance" will perform a swipe down, a negative // "distance" will perform a swipe up. // "swipes" sets the number of swipes that are performed. // "frequency" sets the frequency of the swipes. public void swipeVertically( double x_offset, double y_offset, int distance, int swipes, double frequency) throws Exception { Dimension size = driver.manage().window().getSize(); Point middle = new Point(size.getWidth(), size.getHeight()); Point middleWithOffset = new Point(middle.x * x_offset, middle.y * y_offset); int startX = (int) middleWithOffset.x; int startY = (int) middleWithOffset.y; int endX = startX; int endY = startY + distance; for (int i = 0; i < swipes; i++) { if (platformName.equalsIgnoreCase("iOS")) { iOSSwipe(startX, startY, endX, endY); } else { androidSwipe(startX, startY, endX, endY); } sleep(frequency); } log("Finished executing swipes"); }
// Performs a horizontal swipe starting from a relative coordinate of the device screen. // "distance" is relative, a number from 0 to 1. A positive "distance" will perform a swipe to the // right, a negative "distance" will perform a swipe to the left. // "swipes" sets the number of swipes that are performed. // "frequency" sets the frequency of the swipes. public void swipeHorizontally( double x_offset, double y_offset, double distance, int swipes, double frequency) throws Exception { // positive distance for swipe right, negative for swipe left. Dimension size = driver.manage().window().getSize(); Point middle = new Point(size.getWidth(), size.getHeight()); Point middleWithOffset = new Point(middle.x * x_offset, middle.y * y_offset); double relative_distance = middle.x * distance; int startX = (int) middleWithOffset.x; int startY = (int) middleWithOffset.y; int endX = startX + (int) Math.floor(relative_distance); int endY = startY; for (int i = 0; i < swipes; i++) { if (platformName.equalsIgnoreCase("iOS")) { iOSSwipe(startX, startY, endX, endY); } else { androidSwipe(startX, startY, endX, endY); } sleep(frequency); } log("Finished executing swipes"); }
public double getTargetHeight(WebElement target) { Dimension dimension = target.getSize(); return dimension.getHeight(); }
// Taps on the center of the screen. public void tapMiddle() throws Exception { Dimension size = driver.manage().window().getSize(); Point middle = new Point(size.getWidth() / 2, size.getHeight() / 2); tapAtCoordinates((int) middle.x, (int) middle.y); }
// This method calls on the Akaze scripts to find the coordinates of a given image in another // image. // The "image" parameter is the image that you are searching for // The "scene" parameter is the image in which we are looking for "image" // "tolerance" sets the required accuracy for the image recognition algorithm. public Point[] findImage(String image, String scene, double tolerance) throws Exception { Point[] imgRect = new Point[0]; Point[] imgRectScaled; // queryImageFolder is "", unless set by setQueryImageFolder() String queryImageFile = "queryimages/" + queryimageFolder + image; log("Searching for " + queryImageFile); log("Searching in " + searchedImage); try { imgRect = imageFinder.findImage(queryImageFile, searchedImage, tolerance); } catch (Exception e) { e.printStackTrace(); } if (imgRect != null) { Dimension size = getScreenSizeADB(); if (platformName.equalsIgnoreCase("iOS")) { // for retina devices we need to recalculate coordinates double sceneHeight = imageFinder.getSceneHeight(); double sceneWidth = imageFinder.getSceneWidth(); int screenHeight = size.getHeight(); int screenWidth = size.getWidth(); // Make sure screenshot size values are "landscape" for comparison if (sceneHeight > sceneWidth) { double temp = sceneHeight; sceneHeight = sceneWidth; sceneWidth = temp; } // Make sure screen size values are "landscape" for comparison if (screenHeight > screenWidth) { int temp = screenHeight; screenHeight = screenWidth; screenWidth = temp; } if ((screenHeight < sceneHeight) && (screenWidth < sceneWidth)) { if ((screenHeight < sceneHeight / 2) && (screenWidth < sceneWidth / 2)) { imgRectScaled = new Point[] { new Point(imgRect[0].x / 3, imgRect[0].y / 3), new Point(imgRect[1].x / 3, imgRect[1].y / 3), new Point(imgRect[2].x / 3, imgRect[2].y / 3), new Point(imgRect[3].x / 3, imgRect[3].y / 3), new Point(imgRect[4].x / 3, imgRect[4].y / 3) }; log("Device with Retina display rendered at x3 => coordinates have been recalculated"); imgRect = imgRectScaled; } else { imgRectScaled = new Point[] { new Point(imgRect[0].x / 2, imgRect[0].y / 2), new Point(imgRect[1].x / 2, imgRect[1].y / 2), new Point(imgRect[2].x / 2, imgRect[2].y / 2), new Point(imgRect[3].x / 2, imgRect[3].y / 2), new Point(imgRect[4].x / 2, imgRect[4].y / 2) }; log("Device with Retina display rendered at x2 => coordinates have been recalculated"); imgRect = imgRectScaled; } } } Point center = imgRect[4]; // Check that found center coordinate isn't out of screen bounds if ((center.x >= size.width) || (center.x < 0) || (center.y >= size.height) || (center.y < 0)) { log("Screen size is (width, height): " + size.getWidth() + ", " + size.getHeight()); log("WARNING: Coordinates found do not match the screen --> image not found."); imgRect = null; } else { return imgRect; } } return null; }