下面列出了怎么用org.openqa.selenium.internal.Locatable的API类实例代码及写法,或者点击链接到github查看源代码。
public static Function<WebDriver, Boolean> elementHasStoppedMoving(final WebElement element) {
return new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
Point initialLocation = ((Locatable) element).getCoordinates().inViewPort();
try {
Thread.sleep(50);
} catch (InterruptedException ignored) {
//ignored
}
Point finalLocation = ((Locatable) element).getCoordinates().inViewPort();
return initialLocation.equals(finalLocation);
}
};
}
@Override
public Coordinates getCoordinates()
{
if ( webElement instanceof Locatable )
{
if ( cachedCoordinates == null )
cachedCoordinates = ( (Locatable) webElement ).getCoordinates();
return cachedCoordinates;
}
else
return null;
}
@Override
public Coordinates getCoordinates()
{
try
{
return ((Locatable)delegate).getCoordinates();
}
catch (StaleElementReferenceException e)
{
reLocateElement();
return getCoordinates();
}
}
/**
* Forces a mouseOver event on the WebElement.
*/
public void mouseOver() {
TestLogging.log("MouseOver " + this.toString());
findElement();
// build and perform the mouseOver with Advanced User Interactions API
// Actions builder = new Actions(driver);
// builder.moveToElement(element).build().perform();
final Locatable hoverItem = (Locatable) element;
final Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());
}
/***
* Determine if the element is within the bounds of the window
*
* @return true or false
* @throws WidgetException
*/
public boolean isWithinBoundsOfWindow() throws WidgetException {
JavascriptExecutor js = ((JavascriptExecutor) getGUIDriver().getWrappedDriver());
// execute javascript to get scroll values
Object top = js.executeScript("return document.body.scrollTop;");
Object left = js.executeScript("return document.body.scrollLeft;");
int scrollTop;
int scrollLeft;
try {
scrollTop = Integer.parseInt(top+"");
scrollLeft = Integer.parseInt(left+"");
}
catch(NumberFormatException e) {
throw new WidgetException("There was an error parsing the scroll values from the page", getByLocator());
}
// calculate bounds
Dimension dim = getGUIDriver().getWrappedDriver().manage().window().getSize();
int windowWidth = dim.getWidth();
int windowHeight = dim.getHeight();
int x = ((Locatable)getWebElement()).getCoordinates().onPage().getX();
int y = ((Locatable)getWebElement()).getCoordinates().onPage().getY();
int relX = x - scrollLeft;
int relY = y - scrollTop;
return relX + findElement().getSize().getWidth() <= windowWidth &&
relY + findElement().getSize().getHeight() <= windowHeight &&
relX >= 0 && relY >= 0;
}
/***
* Scroll to this element
*/
@Override
public void scrollTo() throws WidgetException {
WebElement we = findElement();
Locatable l = ((Locatable)we);
l.getCoordinates().inViewPort();
}
@Override
public Actions sendKeys(WebElement element, CharSequence... keysToSend) {
action.addAction(
new SendKeysAction(keyboard, mouse, (Locatable) element, modifyCharSequence(keysToSend)));
return this;
}
/***
* You can use this method to take your control pictures. Note that the file should be a png.
*
* @param element
* @param toSaveAs
* @throws IOException
* @throws WidgetException
*/
public static void takeScreenshotOfElement(IElement element, File toSaveAs) throws IOException, WidgetException {
for(int i = 0; i < 10; i++) { //Loop up to 10x to ensure a clean screenshot was taken
log.info("Taking screen shot of locator " + element.getByLocator() + " ... attempt #" + (i+1));
//Scroll to element
element.scrollTo();
//Take picture of the page
WebDriver wd = SessionManager.getInstance().getCurrentSession().getWrappedDriver();
File screenshot;
boolean isRemote = false;
if(!(wd instanceof RemoteWebDriver)) {
screenshot = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE);
}
else {
Augmenter augmenter = new Augmenter();
screenshot = ((TakesScreenshot) augmenter.augment(wd)).getScreenshotAs(OutputType.FILE);
isRemote = true;
}
BufferedImage fullImage = ImageIO.read(screenshot);
WebElement ele = element.getWebElement();
//Parse out the picture of the element
Point point = ele.getLocation();
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
int x;
int y;
if(isRemote) {
x = ((Locatable)ele).getCoordinates().inViewPort().getX();
y = ((Locatable)ele).getCoordinates().inViewPort().getY();
}
else {
x = point.getX();
y = point.getY();
}
log.debug("Screenshot coordinates x: "+x+", y: "+y);
BufferedImage eleScreenshot = fullImage.getSubimage(x, y, eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
//Ensure clean snapshot (sometimes WebDriver takes bad pictures and they turn out all black)
if(!isBlack(ImageIO.read(screenshot))) {
FileUtils.copyFile(screenshot, toSaveAs);
break;
}
}
}