public static File captureElementBitmap(WebElement element) throws Exception {

    // Get the WrapsDriver of the WebElement
    WrapsDriver wrapsDriver = (WrapsDriver) element;

    // Get the entire Screenshot from the driver of passed WebElement
    File screen =
        ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);

    // Create an instance of Buffered Image from captured screenshot
    BufferedImage img = ImageIO.read(screen);

    // Get the Width and Height of the WebElement using getSize()
    int width = element.getSize().getWidth();
    int height = element.getSize().getHeight();

    // Create a rectangle using Width and Height
    Rectangle rect = new Rectangle(width, height);

    // Get the Location of WebElement in a Point.
    // This will provide X & Y co-ordinates of the WebElement
    Point p = element.getLocation();

    // Create image by for element using its location and size.
    // This will give image data specific to the WebElement
    BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);

    // Write back the image data for element in File object
    ImageIO.write(dest, "png", screen);

    // Return the File object containing image data
    return screen;
  }
  public static Point getFramePoint(WebDriver webDriver) {
    int x = 0;
    int y = 0;

    WebElement bodyWebElement = getWebElement(webDriver, "//body");

    WrapsDriver wrapsDriver = (WrapsDriver) bodyWebElement;

    WebDriver wrappedWebDriver = wrapsDriver.getWrappedDriver();

    WebDriver.TargetLocator targetLocator = wrappedWebDriver.switchTo();

    targetLocator.window(_defaultWindowHandle);

    for (WebElement webElement : _frameWebElements) {
      Point point = webElement.getLocation();

      x += point.getX();
      y += point.getY();

      targetLocator.frame(webElement);
    }

    return new Point(x, y);
  }
  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;
    }
  }
Beispiel #4
0
  @Test
  public void testDrapAndDrop() throws PageException {
    try {
      // open w3 schools tutorial
      driverObj.get(prop.getProperty("w3tutorials_url"));
      // driverObj.get("file:///C:/Users/P8-03GPQ0/Desktop/Drag_Drop.html");
      Thread.sleep(2000);

      // find draggable element
      // ITafElement draggable = webpage.findObjectByxPath(".//*[@id='drag1']");
      ITafElement draggable =
          webpage.findObjectById(prop.getProperty("w3tutorials_draggable_text"));

      Point point1 = draggable.getCoordinates();

      // find the drop area
      ITafElement dropArea = webpage.findObjectById(prop.getProperty("w3tutorials_drop_area"));

      // drag and drop the text
      draggable.dragAndDrop(dropArea);

      Point point2 = draggable.getCoordinates();

      Assert.assertNotEquals(point1.getY(), point2.getY());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Beispiel #5
0
 private Point getIECoreAbsoluteLocation() {
   final Point elementPoint = element.getLocation();
   final String posStr = getIEPosString();
   return new Point(
       elementPoint.getX() + Integer.parseInt(posStr.split("###")[0]),
       elementPoint.getY() + Integer.parseInt(posStr.split("###")[1]));
 }
Beispiel #6
0
 /**
  * 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);
 }
  public static int getElementPositionTop(WebDriver webDriver, String locator) {

    WebElement webElement = getWebElement(webDriver, locator, "1");

    Point point = webElement.getLocation();

    return point.getY();
  }
Beispiel #8
0
 /**
  * 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);
 }
 @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());
   }
 }
Beispiel #10
0
  @Test
  public void testZoomOut() throws PageException {
    // find signin element
    ITafElement element = webpage.findObjectById(prop.getProperty("amazon_signin_link"));

    // get coordinates of signin element
    Point point1 = element.getCoordinates();
    CommonUtil.sop(point1.getX() + ", " + point1.getY());

    // Zoom out the web page
    webpage.zoomOut(2);

    // find the signin element
    element = webpage.findObjectById(prop.getProperty("amazon_signin_link"));

    // get its cordinates
    Point point2 = element.getCoordinates();
    CommonUtil.sop(point2.getX() + ", " + point2.getY());

    // Assert on element location
    Assert.assertNotEquals(point1.getX(), point2.getX());

    // zoom it back to 100%
    webpage.zoomTo100();
  }
Beispiel #11
0
 public void takeScreenShotOfElementOnFailureSecond() throws IOException {
   // Get entire page screenshot
   File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
   BufferedImage fullImg = ImageIO.read(screenshot);
   // Get the location of element on the page
   Point point = element.getLocation();
   // Get width and height of the element
   int eleWidth = element.getSize().getWidth();
   int eleHeight = element.getSize().getHeight();
   // Crop the entire page screenshot to get only element screenshot
   BufferedImage eleScreenshot =
       fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
   ImageIO.write(eleScreenshot, "png", screenshot);
   // Copy the element screenshot to disk
   FileUtils.copyFile(
       screenshot, new File("/home/dima/Downloads/-autotest-data/Element_Screenshots/"));
 }
 public static void ScreenCapture() throws IOException {
   File file = new File("C:\\Selenium\\jenkindemo\\src\\objectRepositry\\Products_PageObjects");
   FileInputStream input = new FileInputStream(file);
   Properties prop = new Properties();
   prop.load(input);
   WebElement element = dr.findElement(By.xpath(prop.getProperty("ScreenElement")));
   File scrFile = ((TakesScreenshot) dr).getScreenshotAs(OutputType.FILE);
   BufferedImage fullImg = ImageIO.read(scrFile);
   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", scrFile);
   String filename = "Screenshot" + System.currentTimeMillis();
   FileUtils.copyFile(scrFile, new File("c:\\sel_screen\\" + filename + ".png"));
 }
Beispiel #13
0
 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);
 }
Beispiel #14
0
  public void shoot(WebElement element) throws IOException {
    //        try {
    //            driver = new Augmenter().augment(driver);
    //        } catch (Exception ignored) {
    //        }
    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    Point p = element.getLocation();
    int width = element.getSize().getWidth();
    int height = element.getSize().getHeight();
    Rectangle rect = new Rectangle(width, height);
    BufferedImage img = null;
    img = ImageIO.read(screen);
    BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
    ImageIO.write(dest, "png", screen);

    FileUtils.copyFile(screen, new File(screenshotFolder + System.nanoTime() + ".png"));
  }
  @JavascriptEnabled
  @Test
  @Ignore(
      value = {SAFARI, MARIONETTE},
      reason = "Advanced mouse actions only implemented in rendered browsers",
      issues = {4136})
  @NotYetImplemented(HTMLUNIT)
  // @NoDriverAfterTest
  public void testMoveMouseByOffsetOverAndOutOfAnElement() {
    driver.get(pages.mouseOverPage);

    WebElement greenbox = driver.findElement(By.id("greenbox"));
    WebElement redbox = driver.findElement(By.id("redbox"));
    Dimension size = redbox.getSize();
    Point greenboxPosition = greenbox.getLocation();
    Point redboxPosition = redbox.getLocation();
    int shiftX = redboxPosition.getX() - greenboxPosition.getX();
    int shiftY = redboxPosition.getY() - greenboxPosition.getY();

    new Actions(driver).moveToElement(greenbox, 2, 2).perform();

    assertEquals(
        Colors.GREEN.getColorValue(), Color.fromString(redbox.getCssValue("background-color")));

    new Actions(driver).moveToElement(greenbox, 2, 2).moveByOffset(shiftX, shiftY).perform();
    assertEquals(
        Colors.RED.getColorValue(), Color.fromString(redbox.getCssValue("background-color")));

    new Actions(driver)
        .moveToElement(greenbox, 2, 2)
        .moveByOffset(shiftX, shiftY)
        .moveByOffset(-shiftX, -shiftY)
        .perform();
    assertEquals(
        Colors.GREEN.getColorValue(), Color.fromString(redbox.getCssValue("background-color")));
  }
  public static int getFramePositionLeft(WebDriver webDriver) {
    Point point = getFramePoint(webDriver);

    return point.getX();
  }
  private double getDistance(Point point1, Point point2) {
    java.awt.Point point3 = new java.awt.Point(point1.getX(), point1.getY());
    java.awt.Point point4 = new java.awt.Point(point2.getX(), point2.getY());

    return point3.distance(point4);
  }
  public static int getWindowPositionTop(WebDriver webDriver) {
    Point point = getWindowPoint(webDriver);

    return point.getY();
  }
 @Override
 protected Number handleSeleneseCommand(WebDriver driver, String locator, String value) {
   Point location = finder.findElement(driver, locator).getLocation();
   return location.getX();
 }