问题
I use this code to calculate the padding of a button on a page from the browser border.
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);
I tried this:
// (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");
}
The button is on the right side but I don't have a exception. The question is how to calculate that the Log in button is bound to the bottom right of the screen?
回答1:
You need to adapt your conditions to, 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
}
left 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.
}
来源:https://stackoverflow.com/questions/65189655/calculate-the-position-of-the-button-on-login-page