Java Code Examples for org.openqa.selenium.WebElement#getLocation()

The following examples show how to use org.openqa.selenium.WebElement#getLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TargetElementMark.java    From phoenix.webui.framework with Apache License 2.0 7 votes vote down vote up
@Override
public void mark(WebElement ele, File file) throws IOException
{
	BufferedImage bufImg = ImageIO.read(file);
	
	try
	{
		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)
	{
	}
}
 
Example 2
Source File: TouchScrollTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanScrollVerticallyFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link3"));
  int y = link.getLocation().y;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(4200);

  WebElement toScroll = driver.findElement(By.id("imagestart"));
  Action scroll = getBuilder(driver).scroll(toScroll, 0, -800).build();
  scroll.perform();

  y = link.getLocation().y;
  // After scrolling, the location of the element should change accordingly.
  assertThat(y).isLessThan(3500);
}
 
Example 3
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickHorizontallyFastFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement toFlick = driver.findElement(By.id("imagestart"));
  WebElement link = driver.findElement(By.id("link2"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(3500);

  Action flick = getBuilder(driver).flick(toFlick, -400, 0, FlickAction.SPEED_FAST).build();
  flick.perform();
  x = link.getLocation().x;
  // After flicking, the element should now be visible on the screen.
  assertThat(x).isLessThan(3500);
}
 
Example 4
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickVertically() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link3"));
  int y = link.getLocation().y;
  // The element is located at the bottom of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(4200);

  Action flick = getBuilder(driver).flick(0, 750).build();
  flick.perform();
  y = link.getLocation().y;

  // After flicking, the element should now be visible on the screen.
  assertThat(y).isLessThan(4200);
}
 
Example 5
Source File: TouchScrollTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanScrollHorizontallyFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link1"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(1500);

  WebElement toScroll = driver.findElement(By.id("imagestart"));
  Action scroll = getBuilder(driver).scroll(toScroll, -1000, 0).build();
  scroll.perform();

  x = link.getLocation().x;
  // After scrolling, the location of the element should change accordingly.
  assertThat(x).isLessThan(1500);
}
 
Example 6
Source File: SeleniumUtils.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
public static void taskScreenShot(WebDriver driver,WebElement element,String pathName) {

        //指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        //利用IOUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。

        try {
            //获取元素在所处frame中位置对象
            Point p = element.getLocation();
            //获取元素的宽与高
            int width = element.getSize().getWidth();
            int height = element.getSize().getHeight();
            //矩形图像对象
            Rectangle rect = new Rectangle(width, height);
            BufferedImage img = ImageIO.read(srcFile);
            BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height);
            ImageIO.write(dest, "png", srcFile);
            IOUtils.copyFile(srcFile, new File(pathName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
Example 7
Source File: ReadElementAspect.java    From opentest with MIT License 6 votes vote down vote up
@Override
public void run() {
    super.run();
    
    By locator = this.readLocatorArgument("locator");

    WebElement element = this.getElement(locator);
    Point point = element.getLocation();

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

    Dimension size = element.getSize();
    int width = size.width;
    int height = size.height;

    this.writeOutput("x", x);
    this.writeOutput("y", y);
    this.writeOutput("width", width);
    this.writeOutput("height", height);
}
 
Example 8
Source File: DragAndDropTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NoDriverAfterTest
// We can't reliably resize the window back afterwards, cross-browser, so have to kill the
// window, otherwise we are stuck with a small window for the rest of the tests.
// TODO(dawagner): Remove @NoDriverAfterTest when we can reliably do window resizing
@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(EDGE)
public void testShouldAllowUsersToDragAndDropToElementsOffTheCurrentViewPort() {
  driver.get(pages.dragAndDropPage);

  JavascriptExecutor js = (JavascriptExecutor) driver;
  js.executeScript("window.resizeTo(300, 300);");

  driver.get(pages.dragAndDropPage);
  WebElement img = driver.findElement(By.id("test3"));
  Point expectedLocation = img.getLocation();
  drag(img, expectedLocation, 100, 100);
  assertThat(img.getLocation()).isEqualTo(expectedLocation);
}
 
Example 9
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickVerticallyFast() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link4"));
  int y = link.getLocation().y;
  // The element is located at the bottom of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(8700);

  Action flick = getBuilder(driver).flick(0, 1500).build();
  flick.perform();
  y = link.getLocation().y;
  // After flicking, the element should now be visible on the screen.
  assertThat(y).isLessThan(4000);
}
 
Example 10
Source File: AppiumIosTest.java    From candybean with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testLocation() throws Exception {
    WebElement button = driver.findElement(By.xpath("//button[1]"));

    Point location = button.getLocation();

    assertEquals(location.getX(), 94);
    assertEquals(location.getY(), 122);
}
 
Example 11
Source File: CombinedInputActionsTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
@NotYetImplemented(HTMLUNIT)
public void testClickAfterMoveToAnElementWithAnOffsetShouldUseLastMousePosition() {
  driver.get(pages.clickEventPage);

  WebElement element = driver.findElement(By.id("eventish"));
  Dimension size = element.getSize();
  Point location = element.getLocation();

  new Actions(driver)
      .moveToElement(element, 20 - size.getWidth() / 2, 10 - size.getHeight() / 2)
      .click()
      .perform();

  wait.until(presenceOfElementLocated(By.id("pageX")));

  int x;
  int y;
  if (isInternetExplorer(driver) && getIEVersion(driver) < 10) {
    x = Integer.parseInt(driver.findElement(By.id("clientX")).getText());
    y = Integer.parseInt(driver.findElement(By.id("clientY")).getText());
  } else {
    x = Integer.parseInt(driver.findElement(By.id("pageX")).getText());
    y = Integer.parseInt(driver.findElement(By.id("pageY")).getText());
  }

  assertThat(fuzzyPositionMatching(location.getX() + 20, location.getY() + 10, x, y)).isTrue();
}
 
Example 12
Source File: Coordinates.java    From selenium-shutterbug with MIT License 5 votes vote down vote up
public Coordinates(WebElement element, Double devicePixelRatio) {
    Point point = element.getLocation();
    Dimension size = element.getSize();
    this.width = (int)(size.getWidth()*devicePixelRatio);
    this.height = (int)(size.getHeight()*devicePixelRatio);
    this.x = (int)(point.getX()*devicePixelRatio);
    this.y = (int)(point.getY()*devicePixelRatio);
}
 
Example 13
Source File: MouseMovementUtilsImpl.java    From IridiumApplicationTesting with MIT License 5 votes vote down vote up
@Override
public void mouseGlide(
	@NotNull final WebDriver driver,
	@NotNull final JavascriptExecutor javascriptExecutor,
	@NotNull final WebElement element,
	final int time,
	final int steps) {

	checkNotNull(element);

	final boolean moveMouseCursor =
		systemPropertyUtils.getPropertyAsBoolean(
			Constants.MOVE_CURSOR_TO_ELEMENT, false);

	final int verticalOffset =
		systemPropertyUtils.getPropertyAsInt(
			Constants.MOUSE_MOVE_VERTICAL_OFFSET, 0);

	if (moveMouseCursor) {

		final float zoom = systemPropertyUtils.getPropertyAsFloat(
			Constants.SCREEN_ZOOM_FACTOR, 1.0f);

		final org.openqa.selenium.Point viewPoint = element.getLocation();
		final Long height = (Long) javascriptExecutor.executeScript(
			"return arguments[0].clientHeight;", element);
		final Long width = (Long) javascriptExecutor.executeScript(
			"return arguments[0].clientWidth;", element);
		mouseGlide(
			(int) ((viewPoint.x + width / 2) * zoom),
			(int) ((viewPoint.y + verticalOffset + height / 2) * zoom),
			Constants.MOUSE_MOVE_TIME,
			Constants.MOUSE_MOVE_STEPS);

		new Actions(driver).moveToElement(element).perform();
	}
}
 
Example 14
Source File: ScreenServiceImpl.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void saveScreenshot(String screenName, WebElement element) throws IOException {
    log.debug("saveScreenshot with the screen named [{}] and element [{}]", screenName, element.getTagName());

    final byte[] screenshot = ((TakesScreenshot) Context.getDriver()).getScreenshotAs(OutputType.BYTES);
    FileUtils.forceMkdir(new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER));

    InputStream in = new ByteArrayInputStream(screenshot);
    BufferedImage fullImg = ImageIO.read(in);

    // 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
    try {
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
        ImageIO.write(convertType(eleScreenshot, BufferedImage.TYPE_3BYTE_BGR), "jpg",
                new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + screenName + ".jpg"));
    } catch (RasterFormatException e) {
        // if image protrudes from the screen, the whole image is saved.
        log.warn("image protrudes from the screen, the whole image is saved.");
        scrollIntoView(element);
        ImageIO.write(convertType(fullImg, BufferedImage.TYPE_3BYTE_BGR), "jpg",
                new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER + File.separator + screenName + ".jpg"));
    }

}
 
Example 15
Source File: BasicMouseInterfaceTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore(value = MARIONETTE, issue = "https://github.com/mozilla/geckodriver/issues/789")
@NotYetImplemented(HTMLUNIT)
@NotYetImplemented(SAFARI)
@NotYetImplemented(EDGE)
public void testMoveMouseByOffsetOverAndOutOfAnElement() {
  driver.get(pages.mouseOverPage);

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

  Dimension greenBoxSize = greenbox.getSize();
  int xOffset = 2 - greenBoxSize.getWidth() / 2;
  int yOffset = 2 - greenBoxSize.getHeight() / 2;

  new Actions(driver).moveToElement(greenbox, xOffset, yOffset).perform();

  shortWait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));

  new Actions(driver).moveToElement(greenbox, xOffset, yOffset)
    .moveByOffset(shiftX, shiftY).perform();
  shortWait.until(attributeToBe(redbox, "background-color", Colors.RED.getColorValue().asRgba()));

  new Actions(driver).moveToElement(greenbox, xOffset, yOffset)
    .moveByOffset(shiftX, shiftY)
    .moveByOffset(-shiftX, -shiftY).perform();

  shortWait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));
}
 
Example 16
Source File: AppiumAndroidTest.java    From candybean with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testLocation() throws Exception {
    WebElement button = driver.findElement(By.xpath("//button[1]"));

    Point location = button.getLocation();

    assertEquals(location.getX(), 157);
    assertEquals(location.getY(), 182);
}
 
Example 17
Source File: RectangleBasedImageAnnotation.java    From webtau with Apache License 2.0 5 votes vote down vote up
@Override
public void addAnnotationData(Map<String, Object> data, WebElement webElement) {
    Point location = webElement.getLocation();
    Dimension size = webElement.getSize();

    data.put("x", location.getX());
    data.put("y", location.getY());
    data.put("width", size.getWidth());
    data.put("height", size.getHeight());
}
 
Example 18
Source File: SmartWebElement.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Override
public Point getLocation() {
    WebElement e = getElement();
    return e.getLocation();
}
 
Example 19
Source File: SeleniumElement.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Point _getAt()
{
  WebElement webElement = getElement();
  return webElement.getLocation();
}
 
Example 20
Source File: AbstractElementExtractor.java    From carina with Apache License 2.0 2 votes vote down vote up
/**
 * Method to generate rectangle for the element since current version of
 * appium driver throws unimplemented exception
 * 
 * @param element WebElement
 * @return Rectangle
 */
public Rectangle getRect(WebElement element) {
    return new Rectangle(element.getLocation(), element.getSize());
}