下面列出了org.openqa.selenium.Dimension#getHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void clickAt( WebElement webElement, WebDriver webDriver, int x, int y )
{
if ( webElement != null )
{
Dimension elementSize = webElement.getSize();
int useX = (int) ((double) elementSize.getWidth() * ((double) x / 100.0)) + webElement.getLocation().getX();
int useY = (int) ((double) elementSize.getHeight() * ((double) y / 100.0)) + webElement.getLocation().getY();
if ( ((DeviceWebDriver) webDriver).getNativeDriver() instanceof AppiumDriver )
{
new TouchAction( (AppiumDriver) ((DeviceWebDriver) webDriver).getNativeDriver() ).press( PointOption.point( useX, useY ) ).perform();
}
else if ( ((DeviceWebDriver) webDriver).getNativeDriver() instanceof RemoteWebDriver )
{
if ( ((DeviceWebDriver) webDriver).getNativeDriver() instanceof HasTouchScreen )
new TouchActions( webDriver ).move( useX, useY ).click().build().perform();
else
new Actions( webDriver ).moveByOffset(useX, useY ).click().build().perform();
}
}
}
/**
* 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);
}
/**
* 判断要点击的元素是否被其它元素覆盖
*
* @param clickBy
* @param coverBy
* @return
*/
public boolean isCover(By clickBy, By coverBy) {
MobileElement clickElement = getDriver().findElement(clickBy);
MobileElement coverElement = getDriver().findElement(coverBy);
// 获取控件开始位置高度
Point clickstart = clickElement.getLocation();
int clickStartY = clickstart.y;
Point coverstart = coverElement.getLocation();
int coverStartY = coverstart.y;
// 获取控件高度
Dimension firstq = clickElement.getSize();
int height = firstq.getHeight();
// 控件中间高度是否大于底部高度
if (clickStartY + height / 2 >= coverStartY) {
return true;
}
return false;
}
/**
* @param direction
* @param startPosition
* @param distancePercent
* @param setting
* @param screenSize
* @param elementSize
* @param elementLocation
* @param actions
* @return action
* @author wasiq.bhamla
* @since Sep 18, 2018 8:03:55 PM
*/
public static <T extends TouchAction<T>> T swipeTo(final SwipeDirection direction,
final SwipeStartPosition startPosition, final int distancePercent, final PlaybackSetting setting,
final Dimension screenSize, final Dimension elementSize, final Point elementLocation, final T actions) {
final double distance = distancePercent / 100.0;
final Point source = getStartPoint(startPosition, screenSize, elementSize, elementLocation);
int endX = source.getX() + (int) (source.getX() * direction.getX() * distance);
int endY = source.getY() + (int) (source.getY() * direction.getY() * distance);
if (elementSize != null) {
endX = source.getX() + (int) (elementSize.getWidth() * direction.getX() * distance);
endY = source.getY() + (int) (elementSize.getHeight() * direction.getY() * distance);
}
return actions.press(PointOption.point(source.getX(), source.getY()))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(setting.getDelayBeforeSwipe())))
.moveTo(PointOption.point(endX, endY))
.release();
}
public void get( String url )
{
setLastAction();
webDriver.get( url );
try
{
double outerHeight = Integer.parseInt( executeScript( "return window.outerHeight;" ) + "" );
double outerWidth = Integer.parseInt( executeScript( "return window.outerWidth;" ) + "" );
Dimension windowSize = manage().window().getSize();
Object f = executeScript( "return window.outerHeight;" );
heightModifier = (double) windowSize.getHeight() / outerHeight;
widthModifier = (double) windowSize.getWidth() / outerWidth;
}
catch( Exception e )
{
log.warn( "Could not extract height/width modifiers" );
heightModifier = 1;
widthModifier = 1;
}
}
private int getWindowSize(String windowHandle)
{
WebDriver driver = getWebDriver();
driver.switchTo().window(windowHandle);
Dimension dimension = driver.manage().window().getSize();
return dimension.getHeight() * dimension.getWidth();
}
private void resizeIfNeeded(final WebDriver driver) {
final Dimension size = driver.manage().window().getSize();
if (size.getWidth() != 1272 || size.getHeight() != 768) {
// only resize if needed because it may be quite expensive (e.g. IE)
driver.manage().window().setSize(new Dimension(1272, 768));
}
}
@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();
}
public void setNextPickerWheelValue(WebElement pickerWheelElement, double step) {
Point center = ((IOSElement) pickerWheelElement).getCenter();
Dimension size = pickerWheelElement.getSize();
int height = size.getHeight();
TouchAction touchAction = new TouchAction(driver);
touchAction.press(point(center.getX(), center.getY() + (int) (height * step))).release();
touchAction.perform();
}
/**
* 参数初始化
*/
protected void initParameters() {
Dimension dimension;
// 兼容循环执行时,如果sessionId为空,则重新启动
if (DriverFactory.getInstance().getDriver().getSessionId() == null) {
DriverFactory.setDf(null);
}
dimension = DriverFactory.getInstance().getDriver().manage().window().getSize();
Constant.SCREEN_WIDTH = dimension.getWidth();
Constant.SCREEN_HEIGHT = dimension.getHeight();
LogUtil.info("界面宽高:" + Constant.SCREEN_WIDTH + "x" + Constant.SCREEN_HEIGHT);
}
private static Point getStartPoint(final SwipeStartPosition start, final Dimension screenSize,
final Dimension elementSize, final Point elementLocation) {
int x = 0;
int y = 0;
int width = screenSize.getWidth();
int height = screenSize.getHeight();
Point location = new Point(0, 0);
if (elementSize != null) {
width = elementSize.getWidth();
height = elementSize.getHeight();
location = elementLocation;
}
switch (start) {
case BOTTOM:
x = width / 2;
y = elementSize != null && height + location.getY() < screenSize.getHeight() ? height : height - 5;
break;
case CENTER:
x = width / 2;
y = height / 2;
break;
case LEFT:
x = elementSize != null && width + location.getX() > 0 ? 0 : 5;
y = height / 2;
break;
case RIGHT:
x = elementSize != null && width + location.getX() < screenSize.getWidth() ? width : width - 5;
y = height / 2;
break;
case TOP:
default:
x = width / 2;
y = 5;
break;
}
return new Point(location.getX() + x, location.getY() + y);
}
public boolean _clickAt( int offsetX, int offsetY )
{
WebElement webElement = getElement();
if (webElement != null)
{
CloudActionProvider aP = getWebDriver().getCloud().getCloudActionProvider();
Dimension elementSize = aP.translateDimension(getWebDriver(), webElement.getSize());
int useX = (int) ((double) elementSize.getWidth() * ((double) offsetX / 100.0) + webElement.getLocation().getX());
int useY = (int) ((double) elementSize.getHeight() * ((double) offsetY / 100.0) + webElement.getLocation().getY());
if (log.isInfoEnabled())
log.info("Clicking " + useX + "," + useY + " pixels relative to " + getName());
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
aP.tap(getWebDriver(), new PercentagePoint(useX, useY, false), 500);
return true;
}
return true;
}
return false;
}
@Override
protected MobileDevice initMobile(IDevice iDevice, AppiumServer appiumServer) throws Exception {
String mobileId = iDevice.getSerialNumber();
boolean isRealDevice = !iDevice.isEmulator();
Mobile mobile = new Mobile();
mobile.setPlatform(MobileDevice.PLATFORM_IOS);
mobile.setCreateTime(new Date());
mobile.setId(mobileId);
mobile.setName(IosUtil.getDeviceName(mobileId, isRealDevice));
mobile.setEmulator(isRealDevice ? Mobile.REAL_MOBILE : Mobile.EMULATOR);
if (isRealDevice) {
mobile.setSystemVersion(IosUtil.getRealDeviceSystemVersion(mobileId));
}
IosDevice iosDevice = new IosDevice(mobile, appiumServer);
log.info("[{}]开始初始化appium", mobileId);
RemoteWebDriver driver = iosDevice.freshDriver(null, true);
log.info("[{}]初始化appium完成", mobileId);
if (!isRealDevice) {
try {
AppiumDriver appiumDriver = (AppiumDriver) driver;
String sdkVersion = (String) appiumDriver.getSessionDetail("sdkVersion");
mobile.setSystemVersion(sdkVersion);
} catch (Exception e) {
log.warn("[{}]获取sdkVersion失败", mobileId, e);
}
}
// 有时window获取的宽高可能为0
while (true) {
Dimension window = driver.manage().window().getSize();
int width = window.getWidth();
int height = window.getHeight();
if (width > 0 && height > 0) {
mobile.setScreenWidth(width);
mobile.setScreenHeight(height);
break;
} else {
log.warn("[{}]未获取到正确的屏幕宽高: {}", mobileId, window);
}
}
// 截图并上传到服务器
UploadFile uploadFile = iosDevice.screenshotThenUploadToServer();
mobile.setImgPath(uploadFile.getFilePath());
driver.quit();
return iosDevice;
}
protected Point center(WebElement webElement) {
Point location = webElement.getLocation();
Dimension size = webElement.getSize();
return new Point(location.getX() + size.getWidth() / 2, location.getY() + size.getHeight() / 2);
}
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);
}
@Override
public Point translatePoint(DeviceWebDriver webDriver, Point currentPoint )
{
log.error( "Translating potin from " + currentPoint );
if ( deviceWidth == 0 || deviceHeight == 0 )
{
if ( webDriver.getPopulatedDevice().getResolution() == null )
{
deviceWidth = -1;
deviceHeight = -1;
return currentPoint;
}
String[] deviceResolution = webDriver.getPopulatedDevice().getResolution().split( "x" );
if ( deviceResolution.length != 2 )
{
deviceWidth = -1;
deviceHeight = -1;
return currentPoint;
}
try
{
deviceWidth = Integer.parseInt( deviceResolution[ 0 ] );
deviceHeight = Integer.parseInt( deviceResolution[ 1 ] );
}
catch( Exception e )
{
}
log.error( "Resolution is " + deviceWidth + ", " + deviceHeight);
if ( deviceWidth == 0 || deviceHeight == 0 )
{
deviceWidth = -1;
deviceHeight = -1;
return currentPoint;
}
}
if ( deviceWidth == -1 || deviceHeight == -1 )
return currentPoint;
//
// If we are here then we have a resolution
//
Point returnValue = new Point( currentPoint.getX(), currentPoint.getY() );
Dimension wD = webDriver.manage().window().getSize();
if ( deviceWidth > wD.getWidth() )
returnValue.x = returnValue.x * ( deviceWidth / wD.getWidth() );
else if ( deviceWidth < wD.getWidth() )
returnValue.x = returnValue.x * ( wD.getWidth() / deviceWidth );
if ( deviceHeight > wD.getHeight() )
returnValue.y = returnValue.y * ( deviceHeight / wD.getHeight() );
else if ( deviceHeight < wD.getHeight() )
returnValue.y = returnValue.y * ( wD.getHeight() / deviceHeight );
log.error( "New Point " + returnValue );
return returnValue;
}
@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;
}
/**
* Switch between data and wifi(specific to android).
*
* @param wifi
* @param data
* @throws InterruptedException
*/
public void setNetworkConnection(boolean wifi, boolean data,
boolean airplane) throws InterruptedException {
if (deviceOS.equalsIgnoreCase("android")) {
NetworkConnectionSetting connectionSetting = new NetworkConnectionSetting(
0);
connectionSetting.setWifi(wifi);
connectionSetting.setData(data);
connectionSetting.setData(airplane);
((AndroidDriver) appiumDriver)
.setNetworkConnection(connectionSetting);
connectionSetting = ((AndroidDriver) appiumDriver)
.getNetworkConnection();
} else {
HashMap<String, Double> coords = new HashMap<String, Double>();
JavascriptExecutor js = (JavascriptExecutor) appiumDriver;
Dimension devicediam = appiumDriver.manage().window().getSize();
int height = devicediam.getHeight();
// int width = devicediam.getWidth();
if (deviceName.equals("iPhone 6")) {
appiumDriver.swipe(100, height, 100, 100, 500);
Thread.sleep(3 * 1000);
coords.put("x", new Double("" + 120));
coords.put("y", new Double("" + 290));
coords.put("duration", 0.5);
js.executeScript("mobile: tap", coords);
Thread.sleep(3 * 1000);
coords.put("x", new Double("" + 100));
coords.put("y", new Double("" + 100));
coords.put("duration", 0.5);
js.executeScript("mobile: tap", coords);
} else {// For iPhone 5
appiumDriver.swipe(100, height, 100, 100, 500);
Thread.sleep(3 * 1000);
coords.put("x", new Double("" + 100));
coords.put("y", new Double("" + 180));
coords.put("duration", 0.5);
js.executeScript("mobile: tap", coords);
Thread.sleep(3 * 1000);
coords.put("x", new Double("" + 100));
coords.put("y", new Double("" + 100));
coords.put("duration", 0.5);
js.executeScript("mobile: tap", coords);
}
}
}
public boolean _clickFor( int length )
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver<?>) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).press(createPoint(webElement)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(length))).release().perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
CloudActionProvider aP = getWebDriver().getCloud().getCloudActionProvider();
Point clickLocation = aP.translatePoint(getWebDriver(), webElement.getLocation());
Dimension clickSize = aP.translateDimension(getWebDriver(), webElement.getSize());
Dimension windowSize = aP.translateDimension(getWebDriver(), getWebDriver().manage().window().getSize());
double x = clickLocation.getX() + (clickSize.getWidth() / 2);
double y = clickLocation.getY() + (clickSize.getHeight() / 2);
int percentX = (int) (x / windowSize.getWidth() * 100.0);
int percentY = (int) (y / windowSize.getHeight() * 100.0);
try
{
new TouchActions(getWebDriver()).longPress(webElement).build().perform();
}
catch (Exception e)
{
((DeviceWebDriver) getWebDriver()).getCloud().getCloudActionProvider().tap((DeviceWebDriver) getWebDriver(), new PercentagePoint(percentX, percentY, true), length);
}
}
return true;
}
}
return false;
}
/**
* height is multiplied because we must avoid the situation when, in fact, different parties will
* give the same sum for example 15 + 25 == 25 + 15, but 15 + 25*10000 != 25 + 15*10000
*
* @param dimension consist partial sizes
* @return partial sizes sum with shift
*/
private int getSizeHashCode(Dimension dimension) {
return dimension.getWidth() + (dimension.getHeight() * 10000);
}