zebrunner / carina-webdriver

Apache License 2.0
7 stars 10 forks source link

Add more helper methods for Android/IOS #200

Open akamarouski opened 10 months ago

akamarouski commented 10 months ago
public void hideKeyboardIfPresent(Duration timeout) {
    boolean isKeyboardShown = false;
    try {
        isKeyboardShown = new FluentWait<>(getDriver()).pollingEvery(Duration.ofMillis(300))
                .withTimeout(timeout)
                .until((driver -> isKeyboardShown()));
    } catch (TimeoutException e) {
        //do nothing
    }
    if (!isKeyboardShown) {
        return;
    }

    try {
        ((HidesKeyboard) getDriver()).hideKeyboard();
    } catch (Exception e) {
        if (!StringUtils.containsIgnoreCase(ExceptionUtils.getRootCauseMessage(e), "The software keyboard cannot be hidden")) {
            ExceptionUtils.rethrow(e);
        }
        // try another way to close keyboard
        ((PressesKey) getDriver()).pressKey((new KeyEvent((AndroidKey.BACK))));
    }
    try {
        isKeyboardShown = !new FluentWait<>(getDriver()).pollingEvery(Duration.ofMillis(300))
                .withTimeout(timeout)
                .until((driver -> !isKeyboardShown()));
    } catch (TimeoutException e) {
        // do nothing, isKeyboardShow already true
    }
    Screenshot.capture(getDriver(), ScreenshotType.EXPLICIT_VISIBLE);
    if (isKeyboardShown) {
        throw new IllegalStateException("The keyboard still present.");
    }
}