下面列出了怎么用org.openqa.selenium.interactions.Locatable的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
}
@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);
}
@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);
}
@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;
}
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);
}
/**
/**
* 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
}
}
@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());
}
@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);
}
@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);
}
@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);
}
@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);
}
@Override
public WebElement apply(final WebElement element) {
return (WebElement) newProxyInstance(
getClass().getClassLoader(),
new Class[]{WebElement.class, WrapsElement.class, Locatable.class, HasIdentity.class},
invocationHandlerFor(element)
);
}
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;
}
/**
* @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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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.");
}
}
@Override
public Void call() {
TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();
WebElement element = getKnownElements().get(elementId);
Coordinates elementLocation = ((Locatable) element).getCoordinates();
touchScreen.singleTap(elementLocation);
return null;
}
@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;
}
@Override
public Void call() {
TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();
WebElement element = getKnownElements().get(elementId);
Coordinates elementLocation = ((Locatable) element).getCoordinates();
touchScreen.doubleTap(elementLocation);
return null;
}
@Override
public Void call() {
TouchScreen touchScreen = ((HasTouchScreen) getDriver()).getTouch();
WebElement element = getKnownElements().get(elementId);
Coordinates elementLocation = ((Locatable) element).getCoordinates();
touchScreen.longPress(elementLocation);
return null;
}
@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;
}
@Override
public Coordinates getCoordinates()
{
return ((Locatable) wrappedElement).getCoordinates();
}
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);
}
/**
* @param webElement web element
* @param locatable locatable
*/
public BobcatWebElementContext(WebElement webElement, Locatable locatable) {
this.webElement = webElement;
this.locatable = locatable;
}
/**
* @return locatable
*/
public Locatable getLocatable() {
return locatable;
}
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);
}
public DoubleTapAction(TouchScreen touchScreen, Locatable locationProvider) {
super(touchScreen, locationProvider);
}
public ScrollAction(TouchScreen touchScreen, Locatable locationProvider, int x, int y) {
super(touchScreen, locationProvider);
xOffset = x;
yOffset = y;
}