Search code examples
javaseleniumselenium-webdriverexpected-condition

How to best use ExpectedConditions.or with variable number of conditions


I've been working with ExpectedConditions.or . It is quite useful to find if one or the other element is present, for example. What I would like to do now is build a more flexible method using variable arguments.

See what I did below. It works...such as it is, but I'd like a more elegant solution that would actually work with any number of elements.

public void waitForSomeElementToBeVisible(int timeout, final By... locators) throws Exception, TimeoutException {
        boolean found = false;

        try {
            waitUntilJSReady();
            setImplicitWait(0);
            WebDriverWait wait = new WebDriverWait(driver, timeout);
            if (1 == locators.length) {
                WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(locators[0]));
                found = null == element ? false : true; 
            } else if (2 == locators.length) {
                found = wait.until(ExpectedConditions.or(ExpectedConditions.visibilityOfElementLocated(locators[0]), 
                        ExpectedConditions.visibilityOfElementLocated(locators[1])));
            } else if (3 == locators.length ) {
                found = wait.until(ExpectedConditions.or(ExpectedConditions.visibilityOfElementLocated(locators[0]), 
                        ExpectedConditions.visibilityOfElementLocated(locators[1]),
                        ExpectedConditions.visibilityOfElementLocated(locators[2])));
            } else if (4 == locators.length ) {
                found = wait.until(ExpectedConditions.or(ExpectedConditions.visibilityOfElementLocated(locators[0]), 
                        ExpectedConditions.visibilityOfElementLocated(locators[1]),
                        ExpectedConditions.visibilityOfElementLocated(locators[2]),
                        ExpectedConditions.visibilityOfElementLocated(locators[3])));           
            }
        } catch (Exception e)  {
            // log...whatever
            throw e;
        } finally {
            setImplicitWait(SelTestCase.WAIT_TIME_OUT);
        }
        if (!found) throw new TimeoutException("Nothing found");
    }

Solution

  • You can obtain the number of locators during runtime and use them in for loop.

    In the below code I created the array which holds ExpectedCondition[]. Store them before you use them in the until method and then just pass it to until

    This allows you to get rid of if-else :)

    public void waitForSomeElementToBeVisible(int timeout, final By... locators) throws Exception, TimeoutException {
            boolean found = false;
    
            try {
                waitUntilJSReady();
                setImplicitWait(0);
                WebDriverWait wait = new WebDriverWait(driver, timeout);
    
                ExpectedCondition<?>[] conditionsToEvaluate = new ExpectedCondition[locators.length];
                for (int i = 0; i < locators.length; i++) {
                    conditionsToEvaluate[i] = ExpectedConditions.visibilityOfElementLocated(locators[i]);
                }
    
                found = wait.until(ExpectedConditions.or(conditionsToEvaluate));
            } catch (Exception e)  {
                // log...whatever
                throw e;
            } finally {
                setImplicitWait(SelTestCase.WAIT_TIME_OUT);
            }
            if (!found) throw new TimeoutException("Nothing found");
        }
    

    Hope it helps!