title: Selenium - Wait for Javascript function to execute before continuing
body: <p>I'm currently creating some test cases with Selenium and I've come across a problem.</p>
  
  <p>In my test case, the website I'm trying to walk through has a small form and a button to search. No problem filling the form and clicking the button. The problem comes once clicking the problem.</p>
  
  <p>Once clicked the button, this function is called:</p>
  
  <pre><code>function muestraEspera(){
  document.getElementById("divCargando").style.display = "";
  }
  </code></pre>
  
  <p>Basically, this makes visible a DIV which contains an "loading" image so the visitor do just see the image and wont be able to actually see the website loading the content until finished (the content is loaded with ajax).</p>
  
  <p>Once the content is loaded, this function is executed:</p>
  
  <pre><code>function escondeEspera(){
  document.getElementById("divCargando").style.display = "none";
  }
  </code></pre>
  
  <p>Which basically hides the "loading" DIV so the visitor can see the results.</p>
  
  <p>Now, I can't use SLEEPS because loading can take more or less, and because I need the real execution time of the website. Is there any way (using java - junit4) to make selenium wait until the second function is executed before continuing with the next steps?</p>
  
  <p>EDIT: I do use Selenium RC. To start the driver I use:</p>
  
  <pre><code>public void setUp() throws Exception {
          selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
          selenium.start();
      }
  </code></pre>
  
  <p><strong>At the end, the solution that worked perfectly for me, given by Pavel Janicek is:</strong></p>
  
  <pre><code>boolean doTheLoop = true;
        int i = 0;
        while (doTheLoop){      
        i= i+200;
        Thread.sleep(1000);
        if (i&gt;30000){
          doTheLoop = false;
        }
        if (!selenium.isVisible("id=divCargando")){
           doTheLoop = false;
        }      
        if (selenium.isVisible("id=divCargando")){
               doTheLoop = true;
            }      
       }
  </code></pre>