下面列出了org.openqa.selenium.Point#getY ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
@Ignore(value = IE, reason = "IE fails this test if requireWindowFocus=true")
@NotYetImplemented(SAFARI)
@Ignore(MARIONETTE)
@NotYetImplemented(EDGE)
public void canDragAnElementNotVisibleInTheCurrentViewportDueToAParentOverflow() {
driver.get(pages.dragDropOverflow);
WebElement toDrag = driver.findElement(By.id("time-marker"));
WebElement dragTo = driver.findElement(By.id("11am"));
Point srcLocation = toDrag.getLocation();
Point targetLocation = dragTo.getLocation();
int yOffset = targetLocation.getY() - srcLocation.getY();
assertThat(yOffset).isNotEqualTo(0);
new Actions(driver).dragAndDropBy(toDrag, 0, yOffset).perform();
assertThat(toDrag.getLocation()).isEqualTo(dragTo.getLocation());
}
@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);
}
@Override
public void run() {
super.run();
By locator = readLocatorArgument("locator");
this.swipeAndCheckElementVisible(locator, this.getSwipeOptions());
MobileElement 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;
Logger.trace(String.format("The element aspect is (%s,%s,%s,%s)",
x, y, width, height));
this.writeOutput("x", x);
this.writeOutput("y", y);
this.writeOutput("width", width);
this.writeOutput("height", height);
}
/**
* Performs the lo touch gesture according to the point coordinates.
*
* @param locator
* write in format of x,y. can be in pixels or
* percentage(recommended) for example 50%,50%.
*/
@Then("^I tap on \"(.*?)\" for \"(\\d*\\.?\\d*)\" seconds$")
public static void tapElement(String locator, int seconds) {
QAFExtendedWebElement myElement = new QAFExtendedWebElement(locator);
Point location = myElement.getLocation();
Dimension size = myElement.getSize();
// determine location to click and convert to an appropriate string
int xToClick = location.getX() + (size.getWidth() / 2);
int yToClick = location.getY() + (size.getHeight() / 2);
String clickLocation = xToClick + "," + yToClick;
DeviceUtils.longTouch(clickLocation, seconds);
}
@Step
@When("установить пикер в первое значение")
public void setFirstPickerWheelValue() {
WebElement webElement = driver.findElement(By.xpath(pickerWheelXPath));
Point center = ((IOSElement) webElement).getCenter();
Dimension size = webElement.getSize();
int height = size.getHeight();
Point target = new Point(center.getX(), center.getY() - (int) (height * stepToLast));
TouchAction touchAction = new TouchAction(driver);
touchAction.press(point(target.getX(), target.getY())).release();
touchAction.perform();
}
@Step
@When("установить пикер в последнее значение")
public void setLastPickerWheelValue() {
WebElement webElement = driver.findElement(By.xpath(pickerWheelXPath));
Point center = ((IOSElement) webElement).getCenter();
Dimension size = webElement.getSize();
int height = size.getHeight();
Point target = new Point(center.getX(), center.getY() + (int) (height * stepToLast));
TouchAction touchAction = new TouchAction(driver);
touchAction.press(point(target.getX(), target.getY())).release();
touchAction.perform();
}
@Override
public void hover(Element ele)
{
WebElement webEle = searchStrategyUtils.findStrategy(WebElement.class, ele).search(ele);
if(webEle == null)
{
logger.warn("can not found element.");
return;
}
if(!(ele instanceof FileUpload))
{
Dimension size = webEle.getSize();
Point loc = webEle.getLocation();
int toolbarHeight = engine.getToolbarHeight();
int x = size.getWidth() / 2 + loc.getX();
int y = size.getHeight() / 2 + loc.getY() + toolbarHeight;
try
{
new Robot().mouseMove(x, y);
}
catch (AWTException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Test
public void bounceThatWindow(){
WebDriver driver = new FirefoxDriver();
driver.get("file://" + System.getProperty("user.dir") + "/jsrunner.html");
driver.manage().window().maximize();
Dimension fullScreenSize = driver.manage().window().getSize();
int changeWidth = 200; int changeHeight = 210;
int xDir = 8; int yDir = 8; int xDirIncrement = xDir; int yDirIncrement = yDir;
driver.manage().window().setSize(new Dimension(changeWidth,changeHeight));
Point position = driver.manage().window().getPosition();
String banner = "***BANG****........ AND THEY ARE OFF........ Automation can be fun. " +
" EvilTester.com present a javascript and browser" +
" animation using Selenium 2 WebDriver tribute to the" +
" sporting event that cannot be named lest we be sued";
int bannerStart = 0;
for(int bounceIterations = 0; bounceIterations < 1000; bounceIterations ++){
position = position.moveBy(xDir,yDir);
driver.manage().window().setPosition(position);
if(position.getX()>(fullScreenSize.getWidth() - changeWidth)){ xDir = -1 * xDirIncrement; }
if(position.getX()<0){ xDir = xDirIncrement; }
if(position.getY()>(fullScreenSize.getHeight() - changeHeight)){ yDir = -1 * yDirIncrement; }
if(position.getY()<0){ yDir = yDirIncrement; }
//try {Thread.sleep(20);} catch (InterruptedException e) {}
String displayBanner = banner.substring(bannerStart,bannerStart+30);
((JavascriptExecutor)driver).executeScript("document.title='" + displayBanner + "';");
bannerStart++;
if(bannerStart > banner.length()-35){banner += banner;}
}
driver.quit();
}
@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()));
}
/**
* Gets the actual point.
*
* @param percentagePoint the percentage point
* @param screenDimension the screen dimension
* @return the actual point
*/
protected Point getActualPoint( Point percentagePoint, Dimension screenDimension )
{
if ( webElement != null )
{
if ( webElement.getLocation() != null && webElement.getSize() != null && webElement.getSize().getWidth() > 0 && webElement.getSize().getHeight() > 0 )
{
int x = percentagePoint.getX() * webElement.getSize().getWidth() + webElement.getLocation().getX();
int y = percentagePoint.getY() * webElement.getSize().getHeight() + webElement.getLocation().getY();
return new Point( x, y );
}
}
return new Point( (int) ( (percentagePoint.getX() / 100.0 ) * (double)screenDimension.getWidth() ), (int) ( (percentagePoint.getY() / 100.0 ) * (double)screenDimension.getHeight() ) );
}
@Override
public int getLocationY() throws WidgetException {
try {
Point p = findElement().getLocation();
return p.getY();
} catch (Exception e) {
throw new WidgetException("Error while fetching Y location", locator, e);
}
}
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);
}
public static String objectOperation(WebDriver wd, WebElement we, String operation, String operationValue, String property, String propertyValue) {
String result = "";
// ����WebElement�������
switch (operation) {
case "click":
we.click();
result = "click�������...������λ����:" + property + "; ��λ����ֵ:" + propertyValue + "��";
LogUtil.APP.info("click�������...������λ����:{}; ��λ����ֵ:{}��",property,propertyValue);
break;
case "sendkeys":
we.sendKeys(operationValue);
result = "sendKeys��������...������λ����:" + property + "; ��λ����ֵ:" + propertyValue + "; ����ֵ:" + operationValue + "��";
LogUtil.APP.info("sendKeys��������...������λ����:{}; ��λ����ֵ:{}; ����ֵ:{}��",property,propertyValue,operationValue);
break;
case "clear":
we.clear();
result = "clear��������...������λ����:" + property + "; ��λ����ֵ:" + propertyValue + "��";
LogUtil.APP.info("clear��������...������λ����:{}; ��λ����ֵ:{}��",property,propertyValue);
break; // ��������
case "gotoframe":
wd.switchTo().frame(we);
result = "gotoframe�л�Frame...������λ����:" + property + "; ��λ����ֵ:" + propertyValue + "��";
LogUtil.APP.info("gotoframe�л�Frame...������λ����:{}; ��λ����ֵ:{}��",property,propertyValue);
break;
case "isenabled":
result = "��ȡ����ֵ�ǡ�"+we.isEnabled()+"��";
LogUtil.APP.info("��ȡ����ֵ�ǡ�{}��",we.isEnabled());
break;
case "isdisplayed":
result = "��ȡ����ֵ�ǡ�" + we.isDisplayed() + "��";
LogUtil.APP.info("��ȡ����ֵ�ǡ�{}��", we.isDisplayed());
break;
case "exjsob":
JavascriptExecutor jse = (JavascriptExecutor) wd;
Object obj = jse.executeScript(operationValue, we);
if (null != obj) {
String tmp = obj.toString();
result = (100 < tmp.length()) ? tmp.substring(0, 100) + "..." : tmp;
result = "��ȡ����ֵ�ǡ�" + result + "��";
LogUtil.APP.info("ִ��JS...��{}�������صĽ��Ϊ:{}",operationValue,result);
} else {
result = "ִ��JS...��" + operationValue + "��";
LogUtil.APP.info("ִ��JS...��{}��",operationValue);
}
break;
case "scrollto":
Point location = we.getLocation();
((JavascriptExecutor) wd).executeScript("window.scrollTo(" + location.getX() + ", " + location.getY() + ")");
result = "������Ŀ�����...������λ����:" + property + "; ��λ����ֵ:" + propertyValue + "; ��������(x,y):" + location.getX() + "," + location.getY() + "��";
LogUtil.APP.info("������Ŀ�����...������λ����:{}; ��λ����ֵ:{}; ��������(x,y):{},{}��",property,propertyValue,location.getX(),location.getY());
break;
case "scrollintoview":
// �˷���������ִ��js����������
((JavascriptExecutor) wd).executeScript("arguments[0].scrollIntoView(" + operationValue + ")", we);
result = "��Ŀ��������������...������λ����:" + property + "; ��λ����ֵ:" + propertyValue + "��";
LogUtil.APP.info("��Ŀ��������������...������λ����:{}; ��λ����ֵ:{}��",property,propertyValue);
break;
default:
break;
}
return result;
}
@Override
protected Number handleSeleneseCommand(WebDriver driver, String locator, String value) {
Point location = finder.findElement(driver, locator).getLocation();
return location.getY();
}
private static void setDriverPosition(WebDriver driver) {
Point driverPosition = driver.manage().window().getPosition();
if (driverPosition.getX() > 0 || driverPosition.getY() > 23) {
driver.manage().window().setPosition(new Point(0, 0));
}
}
/**
* Capture the image of the target HTML element.
* @param targetLocator The locator of the HTML element to capture.
* @param ignoredElements The HTML elements ignored from the comparison.
* @param scaleFactor For retina displays, this has to be set to 2.
* @param ignoredPixelsColor The color that will be used to obscure the
* HTML elements that are ignored from the comparison.
* @return The image of the HTML element.
*/
private BufferedImage captureImage(By targetLocator, By[] ignoredElements, Double scaleFactor, Color ignoredPixelsColor) {
WebElement targetElement = this.getElement(targetLocator);
ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(scaleFactor.floatValue()), 100);
AShot ashot = new AShot()
.coordsProvider(new WebDriverCoordsProvider())
.shootingStrategy(shootingStrategy);
// Hide scroll bars, so they don't impact the captured image
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
Object overflowValue = jsExecutor.executeScript("return document.querySelector('html').style.overflow");
if (!(overflowValue instanceof String)) {
overflowValue = "initial";
}
jsExecutor.executeScript("document.querySelector('html').style.overflow = 'hidden'");
Screenshot screenshot = ashot.takeScreenshot(driver, targetElement);
BufferedImage capturedImage = screenshot.getImage();
jsExecutor.executeScript(String.format("document.querySelector('html').style.overflow = '%s';", overflowValue));
for (By by : ignoredElements) {
Point targetElemLocation = targetElement.getLocation();
WebElement ignoredElement = this.getElement(by);
Point ignoredElementLocation = ignoredElement.getLocation();
Dimension size = ignoredElement.getSize();
int width = size.width;
int height = size.height;
int relativeX = ignoredElementLocation.getX() - targetElemLocation.getX();
int relativeY = ignoredElementLocation.getY() - targetElemLocation.getY();
for (int xCoord = relativeX; xCoord < relativeX + width; xCoord++) {
for (int yCoord = relativeY; yCoord < relativeY + height; yCoord++) {
capturedImage.setRGB(xCoord, yCoord, ignoredPixelsColor.getRGB());
}
}
}
return capturedImage;
}
@Override
protected boolean _executeGesture( WebDriver webDriver )
{
String executionId = getExecutionId( webDriver );
String deviceName = getDeviceName( webDriver );
if ( executionId != null && deviceName != null )
{
if ( webElement != null )
{
CloudActionProvider aP = ( (DeviceWebDriver) webDriver ).getCloud().getCloudActionProvider();
Point at = aP.translatePoint( ( (DeviceWebDriver) webDriver), webElement.getLocation() );
Dimension size = aP.translateDimension( (DeviceWebDriver) webDriver, webElement.getSize() );
if ( at != null && size != null && size.getWidth() > 0 && size.getHeight() > 0 )
{
int x = getStartOne().getX() * size.getWidth() + at.getX();
int y = getStartOne().getY() * size.getHeight() + at.getY();
Point swipeStart = new Point( x, y );
x = getEndOne().getX() * size.getWidth() + at.getX();
y = getEndOne().getY() * size.getHeight() + at.getY();
Point swipeEnd = new Point( x, y );
PerfectoMobile.instance(( (DeviceWebDriver) webDriver ).getxFID() ).gestures().pinch( executionId, deviceName, new PercentagePoint( swipeStart.getX(), swipeStart.getY(), false ), new PercentagePoint( swipeEnd.getX(), swipeEnd.getY(), false ) );
return true;
}
else
{
log.warn( "A relative elements was specified however no size could be determined" );
return false;
}
}
PerfectoMobile.instance(( (DeviceWebDriver) webDriver ).getxFID() ).gestures().pinch( executionId, deviceName, new PercentagePoint( getStartOne().getX(), getStartOne().getY() ), new PercentagePoint( getEndOne().getX(), getEndOne().getY() ) );
return true;
}
else
return false;
}
/***
* 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;
}
}
}
public Coordinates(Point point, Dimension size, Double devicePixelRatio) {
this.width = (int)(size.getWidth()*devicePixelRatio);
this.height = (int)(size.getHeight()*devicePixelRatio);
this.x = (int)(point.getX()*devicePixelRatio);
this.y = (int)(point.getY()*devicePixelRatio);
}
/**
* Distance and direction from point drag to point drop in two-dimensional plane
*
* @param drag start point of drag
* @param drop end point
*/
Offset(Point drag, Point drop) {
this.x = drop.getX() - drag.getX();
this.y = drop.getY() - drag.getY();
}