While working on creating test cases using Selenium, I encountered an issue.
In one of my test cases, there is a small form and a search button on the website I'm testing. Filling the form and clicking the button are not the problem. The issue arises after clicking the button.
Upon clicking the button, the following function is triggered:
function showLoading(){
document.getElementById("loadingDiv").style.display = "";
}
This function essentially displays a DIV that contains a "loading" image to prevent visitors from seeing the content loading via AJAX.
Once the content is loaded, the following function is called:
function hideLoading(){
document.getElementById("loadingDiv").style.display = "none";
}
This function hides the "loading" DIV so the visitor can view the results.
Since the loading time may vary and SLEEPs cannot be used due to the need for real execution time, is there a way (using Java - JUnit4) to make Selenium wait until the second function is executed before proceeding with the next steps?
EDIT: I am using Selenium RC. To initiate the driver, I use:
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
selenium.start();
}
Ultimately, the solution provided by Pavel Janicek worked perfectly for me:
boolean proceed = true;
int i = 0;
while (proceed){
i= i+200;
Thread.sleep(1000);
if (i>30000){
proceed = false;
}
if (!selenium.isVisible("id=loadingDiv")){
proceed = false;
}
if (selenium.isVisible("id=loadingDiv")){
proceed = true;
}
}