Friday, 17 June 2016

Wait until any of the elements is displayed in Selenium

Below code waits until any of the elements is displayed in Selenium.

public void waitUntilElements(){
        List<By> listOfElements = new ArrayList<By>();
        listOfElements.add(By.id("resultsPage"))
        listOfElements.add(By.xpath("//*[contains(text(),'xyz')]"))
        listOfElements.add(By.xpath("//*[contains(text(),'pqr')]"))
        listOfElements.add(By.xpath("//*[contains(text(),'abc')]"))
        waitUntilAnyOfTheElementIsDisplayed(listOfElements)
    }

    boolean waitUntilAnyOfTheElementIsDisplayed(List<By> byList) {
        for (int i=1;i<=60;i++) {
            for (int j = 0; j < byList.size(); j++){
                if (waitUntilElementIsDisplayed(byList.get(j), 1)) {
                    return true;
                }
            }
            sleep(1000)
        }
        return false
    }

public boolean waitUntilElementIsDisplayed(By by, int seconds){
        if (checkElementExists(by,seconds)){
            for (int i=0;i<seconds;i++){
                 if (driver.findElement(by).isDisplayed())
                     return true;
                 else
                     sleep(1000)
                }
                    return false;
        }
        else
        {
            return false;
        }
    }


public boolean checkElementExists(By by, int seconds){
        boolean result=false;
        try {
            driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
            driver.findElement(by);
            result = true;
        }catch (org.openqa.selenium.NoSuchElementException ex){
            result = false;
        }
        finally {
            DriverUtil.settingImplicitWait()
        }
        return  result;
    }


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors