Tuesday, January 21, 2025

java – Tips on how to proceed script when aspect just isn’t present in selenium


What you’re experiencing is a fast-fail sort of habits whenever you use .findElement . You do not really want to make use of .findElements(el) to get a soft-fail habits. What you possibly can do is one thing like this as a substitute:

protected void checkElementPresence(ultimate WebDriver driver,ultimate By by,ultimate String errorMsg){
  new WebDriverWaitWithMessage(driver,10).failWith(errorMsg).till(new ExpectedCondition(){
    @Override public Boolean apply(    WebDriver webDriver){
      attempt {
        return driver.findElement(by).isDisplayed();
      }
 catch (      NoSuchElementException ignored) {
        return false;
      }
catch (      StaleElementReferenceException ignored) {
        return false;
      }
    }
  }
);

However requires this assist class:

protected static class WebDriverWaitWithMessage extends WebDriverWait {

        non-public String message;

        public WebDriverWaitWithMessage(WebDriver driver, lengthy timeOutInSeconds) {
            tremendous(driver, timeOutInSeconds);
        }

        public WebDriverWait failWith(String message) {
            if (message == null || message.size() == 0) {
                throw new IllegalArgumentException("Error message should not be null nor empty");
            }
            this.message = message;
            return this;
        }

        @Override
        public  V till(Operate tremendous WebDriver, V> isTrue) {
            if (message == null) {
                return tremendous.till(isTrue);
            } else {
                attempt {
                    return tremendous.till(isTrue);
                } catch (TimeoutException e) {
                    throw new TimeoutException(message, e);
                }
            }
        }
    }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

PHP Code Snippets Powered By : XYZScripts.com