计算按钮在登录页面上的位置



我使用此代码从浏览器边框计算页面上按钮的填充。

Dimension dm = new Dimension(1024,768);
//Setting the current window to that dimension
driver.manage().window().setSize(dm);
// Click Login button to submit login form
WebDriverWait loginButtonWebDriverWait = new WebDriverWait(driver, 4000);
WebElement loginButtonWebElement = loginButtonWebDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.id("login")));
int loginButtonX = loginButtonWebElement.getLocation().getX();
int loginButtonY = loginButtonWebElement.getLocation().getY();
int loginButtonWidth = loginButtonWebElement.getRect().getWidth();
int loginButtonHeight = loginButtonWebElement.getRect().getHeight();
System.out.println("Login Button is " + loginButtonX + " pixels from left border.");
System.out.println("Login Button is " + (screenWidth - loginButtonX + loginButtonWidth) + " pixels from right border.");
System.out.println("Login Button is " + loginButtonY + " pixels from top border.");
System.out.println("Login Button is " + (screenHeight - loginButtonY + loginButtonHeight) + " pixels from bottom border.");
// We need to check that the size is not less than 10 pixels. If the space is less trow exception and fail the test.
assertThat(loginButtonX).isGreaterThan(5);
assertThat(loginButtonY).isGreaterThan(5);

我试过这个:

// (window_width - loginButtonWidth) < loginButtonX < window_width
// 0 < `loginButtonY` < loginButtonHeight.
int subtracted = screenWidth - loginButtonWidth;
if(subtracted < loginButtonY && subtracted < loginButtonHeight){
throw new RuntimeException("button is not on right side");
}
// Check left position
// 0 < loginButtonX < loginButtonWidth
if(subtracted < loginButtonX && subtracted < loginButtonWidth){
throw new RuntimeException("button is not on right side");
}

按钮在右边,但我没有例外。问题是如何计算"登录"按钮绑定到屏幕的右下角?

您需要调整您的条件,右侧:

if((screenWidth - loginButtonWidth < loginButtonX) && (loginButtonX < screenWidth)
&& (0 < loginButtonY) && (loginButtonY < loginButtonHeight)){
// If this is true than it is because the button is on the right side
}

左侧:

if((0 < loginButtonX) && (loginButtonX < loginButtonWidth)
&& (0 < loginButtonY) && (loginButtonY < loginButtonHeight)){
// If this is true than it is because the button is on the left side.
}

最新更新