Calculate the position of the button on login page

≡放荡痞女 提交于 2020-12-13 03:10:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!