org.openqa.selenium.interactions.Locatable Java Examples

The following examples show how to use org.openqa.selenium.interactions.Locatable. 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: ExtendedWebElement.java    From carina with Apache License 2.0 6 votes vote down vote up
/**
/**
 * Scroll to element (applied only for desktop).
 * Useful for desktop with React 
 */
public void scrollTo() {
    if (Configuration.getDriverType().equals(SpecialKeywords.MOBILE)) {
        LOGGER.debug("scrollTo javascript is unsupported for mobile devices!");
        return;
    }
    try {
        Locatable locatableElement = (Locatable) findElement(EXPLICIT_TIMEOUT);
        // [VD] onScreen should be updated onto onPage as only 2nd one
        // returns real coordinates without scrolling... read below material
        // for details
        // https://groups.google.com/d/msg/selenium-developers/nJR5VnL-3Qs/uqUkXFw4FSwJ

        // [CB] onPage -> inViewPort
        // https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/remote/RemoteWebElement.java?r=abc64b1df10d5f5d72d11fba37fabf5e85644081
        int y = locatableElement.getCoordinates().inViewPort().getY();
        int offset = R.CONFIG.getInt("scroll_to_element_y_offset");
        ((JavascriptExecutor) getDriver()).executeScript("window.scrollBy(0," + (y - offset) + ");");
    } catch (Exception e) {
    	//do nothing
    }
}
 
Example #2
Source File: KnownElements.java    From selenium with Apache License 2.0 6 votes vote down vote up
private WebElement proxyElement(final WebElement element, final String id) {
  InvocationHandler handler = (object, method, objects) -> {
    if ("getId".equals(method.getName())) {
      return id;
    } else if ("getWrappedElement".equals(method.getName())) {
      return element;
    } else {
      try {
      return method.invoke(element, objects);
      } catch (InvocationTargetException e) {
        throw e.getTargetException();
      }
    }
  };

  Class<?>[] proxyThese;
  if (element instanceof Locatable) {
    proxyThese = new Class[] {WebElement.class, ProxiedElement.class, Locatable.class};
  } else {
    proxyThese = new Class[] {WebElement.class, ProxiedElement.class};
  }

  return (WebElement) Proxy.newProxyInstance(element.getClass().getClassLoader(),
      proxyThese,
      handler);
}
 
Example #3
Source File: MouseMoveToLocation.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Override
public Void call() {
  Mouse mouse = ((HasInputDevices) getDriver()).getMouse();

  Coordinates elementLocation = null;
  if (elementProvided) {
    WebElement element = getKnownElements().get(elementId);
    elementLocation = ((Locatable) element).getCoordinates();
  }

  if (offsetsProvided) {
    mouse.mouseMove(elementLocation, xOffset, yOffset);
  } else {
    mouse.mouseMove(elementLocation);
  }
  return null;
}
 
Example #4
Source File: JavascriptEnabledDriverTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldBeAbleToGetTheLocationOfAnElement() {
  assumeTrue(driver instanceof JavascriptExecutor);
  assumeTrue(((HasCapabilities) driver).getCapabilities().is(SUPPORTS_JAVASCRIPT));

  driver.get(pages.javascriptPage);

  ((JavascriptExecutor) driver).executeScript("window.focus();");
  WebElement element = driver.findElement(By.id("keyUp"));

  assumeTrue(element instanceof Locatable);

  Point point = ((Locatable) element).getCoordinates().inViewPort();

  assertThat(point.getX()).as("X coordinate").isGreaterThan(1);
  // Element's Y coordinates can be 0, as the element is scrolled right to the top of the window.
  assertThat(point.getY()).as("Y coordinate").isGreaterThanOrEqualTo(0);
}
 
Example #5
Source File: TouchDoubleTapTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void testCanDoubleTapOnAnImageAndAlterLocationOfElementsInScreen() {
  driver.get(pages.longContentPage);

  WebElement image = driver.findElement(By.id("imagestart"));
  int y = ((Locatable) image).getCoordinates().inViewPort().y;
  // The element is located at a certain point, after double tapping,
  // the y coordinate must change.
  assertThat(y).isGreaterThan(100);

  Action doubleTap = getBuilder(driver).doubleTap(image).build();
  doubleTap.perform();

  y = ((Locatable) image).getCoordinates().inViewPort().y;
  assertThat(y).isLessThan(50);
}
 
Example #6
Source File: GuiceAwareFieldDecorator.java    From bobcat with Apache License 2.0 6 votes vote down vote up
/**
 * This method decorates the field with the generated value. It should not be used directly.
 * Selenium's
 * PageFactory calls this method. If the field has Inject annotation, Selenium will ignore it.
 * Otherwise
 * Selenium will try to generate the value for this field.
 *
 * @param loader ClassLoader
 * @param field  Field to be initialized
 * @return decorated field Object
 */
@Override
public Object decorate(ClassLoader loader, Field field) {
  if (field.isAnnotationPresent(Inject.class)) {
    return null;
  } else {
    Object decoratedField = super.decorate(loader, field);
    if (decoratedField instanceof WebElement) {
      WebElement element = (WebElement) decoratedField;
      Locatable locatable = (Locatable) decoratedField;
      BobcatWebElementContext context =
          new BobcatWebElementContext(element, locatable);
      return bobcatWebElementFactory.create(context);
    }
    return decoratedField;
  }
}
 
Example #7
Source File: TouchActions.java    From selenium with Apache License 2.0 5 votes vote down vote up
/**
 * Allows the execution of long press gestures.
 *
 * @param onElement The {@link WebElement} to long press
 * @return self
 */

public TouchActions longPress(WebElement onElement) {
  if (touchScreen != null) {
    action.addAction(new LongPressAction(touchScreen, (Locatable) onElement));
  }
  return this;
}
 
Example #8
Source File: Scroll.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() {
  TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();

  if (elementId != null) {
    WebElement element = getKnownElements().get(elementId);
    Coordinates elementLocation = ((Locatable) element).getCoordinates();
    touchScreen.scroll(elementLocation, xOffset, yOffset);
  } else {
    touchScreen.scroll(xOffset, yOffset);
  }
  return null;
}
 
Example #9
Source File: LongPressOnElement.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() {
  TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();
  WebElement element = getKnownElements().get(elementId);
  Coordinates elementLocation = ((Locatable) element).getCoordinates();
  touchScreen.longPress(elementLocation);

  return null;
}
 
Example #10
Source File: DoubleTapOnElement.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() {
  TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();
  WebElement element = getKnownElements().get(elementId);
  Coordinates elementLocation = ((Locatable) element).getCoordinates();

  touchScreen.doubleTap(elementLocation);

  return null;
}
 
Example #11
Source File: Flick.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() {
  TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();

  if (elementId != null) {
    WebElement element = getKnownElements().get(elementId);
    Coordinates elementLocation = ((Locatable) element).getCoordinates();
    touchScreen.flick(elementLocation, xOffset, yOffset, speed);
  } else {
    touchScreen.flick(xSpeed, ySpeed);
  }

  return null;
}
 
Example #12
Source File: SingleTapOnElement.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() {
  TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();
  WebElement element = getKnownElements().get(elementId);
  Coordinates elementLocation = ((Locatable) element).getCoordinates();

  touchScreen.singleTap(elementLocation);

  return null;
}
 
Example #13
Source File: SingleKeyAction.java    From selenium with Apache License 2.0 5 votes vote down vote up
protected SingleKeyAction(Keyboard keyboard, Mouse mouse, Locatable locationProvider, Keys key) {
  super(keyboard, mouse, locationProvider);
  this.key = key;
  boolean isModifier = false;
  for (Keys modifier : MODIFIER_KEYS) {
    isModifier = isModifier || modifier.equals(key);
  }

  if (!isModifier) {
    throw new IllegalArgumentException("Key Down / Up events only make sense for modifier keys.");
  }
}
 
Example #14
Source File: TouchActions.java    From selenium with Apache License 2.0 5 votes vote down vote up
/**
 * Allows the execution of double tap on the screen, analogous to double click using a Mouse.
 *
 * @param onElement The {@link WebElement} to double tap
 * @return self
 */

public TouchActions doubleTap(WebElement onElement) {
  if (touchScreen != null) {
    action.addAction(new DoubleTapAction(touchScreen, (Locatable) onElement));
  }
  return this;
}
 
Example #15
Source File: TouchActions.java    From selenium with Apache License 2.0 5 votes vote down vote up
/**
 * Allows the execution of single tap on the screen, analogous to click using a Mouse.
 *
 * @param onElement the {@link WebElement} on the screen.
 * @return self
 */
public TouchActions singleTap(WebElement onElement) {
  if (touchScreen != null) {
    action.addAction(new SingleTapAction(touchScreen, (Locatable) onElement));
  }
  tick(touchPointer.createPointerDown(0));
  tick(touchPointer.createPointerUp(0));
  return this;
}
 
Example #16
Source File: DelegatingWebElementTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetCoordinates()
{
    WebElement locatableWebElement = Mockito.mock(WebElement.class,
            withSettings().extraInterfaces(Locatable.class));
    Coordinates coordinates = Mockito.mock(Coordinates.class);
    when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
    DelegatingWebElement locatableDelegatingWebElement = new DelegatingWebElement(locatableWebElement);
    assertEquals(coordinates, locatableDelegatingWebElement.getCoordinates());
}
 
Example #17
Source File: MouseActionsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void clickInvisibleElement()
{
    when(webDriverProvider.get()).thenReturn(webDriver);
    when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
    Coordinates coordinates = mock(Coordinates.class);
    when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
    mouseActions.moveToAndClick(locatableWebElement);
    verify(mouse).mouseMove(coordinates);
    verify(mouse).click(null);
}
 
Example #18
Source File: MouseActionsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testContextClick()
{
    when(webDriverProvider.get()).thenReturn(webDriver);
    when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
    Coordinates coordinates = mock(Coordinates.class);
    when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
    mouseActions.contextClick(locatableWebElement);
    verify(mouse).contextClick(coordinates);
}
 
Example #19
Source File: MouseActionsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testMoveToElement()
{
    when(webDriverProvider.get()).thenReturn(webDriver);
    when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
    Coordinates coordinates = mock(Coordinates.class);
    when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
    mouseActions.moveToElement(locatableWebElement);
    verify(mouse).mouseMove(coordinates);
}
 
Example #20
Source File: DroppableWebElement.java    From bobcat with Apache License 2.0 5 votes vote down vote up
/**
 * @return Point in the middle of the drop area.
 */
@Override
public Point getCurrentLocation() {
  Point inViewPort = null;
  switcher.switchTo(getFramePath());
  try {
    Dimension size = dropArea.getSize();
    inViewPort = ((Locatable) dropArea).getCoordinates().inViewPort()
        .moveBy(size.getWidth() / 2, size.getHeight() / 2);
  } finally {
    switcher.switchBack();
  }
  return inViewPort;
}
 
Example #21
Source File: MouseActionsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testMoveToElementMoveTargetOutOfBoundsException()
{
    when(webDriverProvider.get()).thenReturn(webDriver);
    when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
    MoveTargetOutOfBoundsException boundsException = new MoveTargetOutOfBoundsException(
            COULD_NOT_MOVE_TO_ERROR_MESSAGE);
    Coordinates coordinates = mock(Coordinates.class);
    when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
    doThrow(boundsException).when(mouse).mouseMove(coordinates);
    mouseActions.moveToElement(locatableWebElement);
    verify(softAssert).recordFailedAssertion(COULD_NOT_MOVE_TO_ERROR_MESSAGE + boundsException);
}
 
Example #22
Source File: FrameAwareWebElementTransformer.java    From teasy with MIT License 5 votes vote down vote up
@Override
public WebElement apply(final WebElement element) {
    return (WebElement) newProxyInstance(
            getClass().getClassLoader(),
            new Class[]{WebElement.class, WrapsElement.class, Locatable.class, HasIdentity.class},
            invocationHandlerFor(element)
    );
}
 
Example #23
Source File: DraggableWebElement.java    From bobcat with Apache License 2.0 5 votes vote down vote up
private Point getCurrentLocation() {
  Point inViewPort = null;
  switcher.switchTo(getFramePath());
  try {
    Dimension size = webElement.getSize();
    inViewPort = ((Locatable) webElement).getCoordinates().inViewPort()
        .moveBy(size.getWidth() / 2, size.getHeight() / 2);
  } finally {
    switcher.switchBack();
  }
  return inViewPort;
}
 
Example #24
Source File: LongPressAction.java    From selenium with Apache License 2.0 4 votes vote down vote up
public LongPressAction(TouchScreen touchScreen, Locatable locationProvider) {
  super(touchScreen, locationProvider);
}
 
Example #25
Source File: GetElementLocationInView.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Override
public Point call() {
  Locatable element = (Locatable) getElement();
  return element.getCoordinates().inViewPort();
}
 
Example #26
Source File: ProxyFactory.java    From healenium-web with Apache License 2.0 4 votes vote down vote up
public static <T extends WebElement> T createWebElementProxy(ClassLoader loader, InvocationHandler handler) {
    Class<?>[] interfaces = new Class[]{WebElement.class, WrapsElement.class, Locatable.class};
    return (T) Proxy.newProxyInstance(loader, interfaces, handler);
}
 
Example #27
Source File: BobcatWebElementContext.java    From bobcat with Apache License 2.0 4 votes vote down vote up
/**
 * @param webElement web element
 * @param locatable  locatable
 */
public BobcatWebElementContext(WebElement webElement, Locatable locatable) {
  this.webElement = webElement;
  this.locatable = locatable;
}
 
Example #28
Source File: BobcatWebElementContext.java    From bobcat with Apache License 2.0 4 votes vote down vote up
/**
 * @return locatable
 */
public Locatable getLocatable() {
  return locatable;
}
 
Example #29
Source File: CurrentWebElementProvider.java    From bobcat with Apache License 2.0 4 votes vote down vote up
private WebElement getWebElementFromFactory(ElementLocatorFactory factory) {
  InvocationHandler handler = new LocatingElementHandler(
      ((ParentElementLocatorProvider) factory).getCurrentScope());
  return (WebElement) Proxy.newProxyInstance(WebElement.class.getClassLoader(),
      new Class[] {WebElement.class, WrapsElement.class, Locatable.class}, handler);
}
 
Example #30
Source File: PositionAndSizeTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
private Point getLocationOnPage(By locator) {
  WebElement element = driver.findElement(locator);
  return ((Locatable) element).getCoordinates().onPage();
}