seleniumselenium-rc

Stop the selenium server until File uploaded


I am using a file upload task with Selenium.

The problem here is, to upload file, it is taking 5-10 seconds time. But i have to stop the Selenium server until it uploaded completely.

Here is some example code.

selenium.type("id=Fileuploader","c:\\mypic.jpg");
selenium.click("id=submmit");

It is giving error because the selenium.click statement is executing right after the selenium.type statement without waiting for the file to upload fully.

So, what should I use here to stop the Selenium server (server has to wait for sometime)?


Solution

  • Asynchronous requests are always hard to track. There must be some change on the page when the file is uploaded, so look for it, wait for it.

    You can try waitForCondition().

    Or some sort of isElementPresent() magic

    final int TIMEOUT = 10000;  // ten seconds
    long targetTime = System.currentTimeInMillis() + TIMEOUT;
    while((System.currentTimeInMillis() < targetTime)) {
        if (selenium.isElementPresent("xpath=something")) {
            break;
        }
    }
    

    EDIT: If there's no new element, there has to be at least some sort of change. For example, if your asynchronous upload changes a value of some hidden input element, you could test for it using getValue(), or maybe just a smart locator:

    isElementPresent("xpath=//input[@type='hidden' and contains(@value,'mypic.jpg')]");
    

    EDIT2: If the element for which we check (in this case, picture preview) is present even before the upload, then it was just not visible and we can test that by isVisible()