将条件签入if check

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

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

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.

你知道我怎样才能正确地执行如下检查:

if((screenWidth - loginButtonWidth) < loginButtonX < screenWidth){

}

现在我得到一个错误: Operator '<' cannot be applied to 'boolean', 'int' . 我怎样才能解决这个问题?

vjrehmav

vjrehmav1#

(screenWidth - loginButtonWidth) < loginButtonX 退货 true 或者 false 根据情况是否属实。所以在你的情况下,声明 (window_width - loginButtonWidth) < loginButtonX < window_width 等于:

if(true/false < number)

这给了你一个错误: Operator '<' cannot be applied to 'boolean', 'int' ,因为您无法检查布尔值是否大于或小于数字。
您应该执行以下操作:

int subtracted = screenWidth - loginButtonWidth;
if(subtracted < loginButtonX && subtracted < screenWidth){

}

相关问题