简体   繁体   中英

Check conditions into if check

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 need to implement some check like this:

(window_width - loginButtonWidth) < loginButtonX < window_width

and

0 < `loginButtonY` < loginButtonHeight. 

Do you know how I can implement properly check like this:

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

}

Now I get error: Operator '<' cannot be applied to 'boolean', 'int' . How I can solve this issue?

(screenWidth - loginButtonWidth) < loginButtonX returns true or false based on whether the condition is true. So in your case, the statement (window_width - loginButtonWidth) < loginButtonX < window_width would be equal to:

if(true/false < number)

Which gives you the error: Operator '<' cannot be applied to 'boolean', 'int' , since you cannot check whether a boolean is bigger or smaller than a number.

You should be doing the following:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM