Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I'm new to selenium. I am facing an issue. The number of web elements in a page increases on a button click(triggered by ajax call). So while getting the size of the element after button click gives the same value.(But it was updated in site manually).

I have tried waiting for ajax, and also implicit wait. On implicit wait it throws element not found exception.

How can I wait until the count of the web element gets changed.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

To add to gomesr's answer, you might find yourself needing to wait for other elements during your tests. One way to easily have reusable code for this is to use a closure (also known as an anonymous function or a lambda function) to create a spinner function that you can call whenever you're waiting for an element to load.

The spinner function would look like

public function spin ($lambda, $wait = 60) {
    for ($i = 0; $i < $wait; $i++) {
        try {
            if ($lambda($this)) {
                return true;
            }
        } catch (Exception $e) {
            // do nothing
        }

        sleep(1);
    }

    $backtrace = debug_backtrace();

    throw new Exception(
        "Timeout thrown by " . $backtrace[1]['class'] . "::" . $backtrace[1]['function'] . "()\n" .
        $backtrace[1]['file'] . ", line " . $backtrace[1]['line']
    );
}

And to call the function

$this->spin(function($context) {
    return ($context->getSession()->getPage()->findById('example')->isVisible());
 });

When you call the spinner function you pass an anonymous function with the logic that you want to assert on, in this example it's waiting for an element with an id of 'example' to be visible. The spinner also includes a timeout that will throw an exception if the element is not found within the specified time (default 60 seconds).

The above sample code is from http://docs.behat.org/cookbook/using_spin_functions.html

You can also find more information on spin functions and another code example at http://sauceio.com/index.php/2011/04/how-to-lose-races-and-win-at-selenium/

share|improve this answer
    
thank you for your response. But it seems I'm facing facing a combined issue in this case. let me post it as a different question. so that It might be helpful for users. –  Ajmal Oct 23 '13 at 11:59

You will have to do a manual loop waiting on the count being equal to X. Have a timeout so that it doesn't wait indefinitely. So something like:
start_time=now
count=number of elements
while count < X:
count=number of elements
if now - start_time > timeout
exit loop

share|improve this answer

Thanks for your responses. But this is the script I'm using to resolve this issue

public void waitUntilCountChanges() {
        WebDriverWait wait = new WebDriverWait(getDriver(), 5);
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                int elementCount = driver.findElement(By.xpath("xxxx")).size();
                if (elementCount > 1)
                    return true;
                else
                    return false;
            }
        });
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.