计算按钮在页面上的位置

w46czmvw  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(316)

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

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);

问题是如何计算登录按钮绑定到屏幕右下角?

vq8itlhq

vq8itlhq1#

假设 coordinate system 作为:

y ^
    |
    |              _
    |             |_|
    |---------------->
    (0,0)           x

你需要做的是

(window_width - loginButtonWidth) < loginButtonX < window_width

0 < `loginButtonY` < loginButtonHeight.

然后它在屏幕的右下角。
如果按钮不是严格地放在角落里,可能需要添加一些填充。

相关问题