Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

driver.wait() throws IllegalMonitorStateException

All the variations of wait(...) are throwing the below exception from the following code. What am I doing wrong?

java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:485)
    at LoginPage.main(LoginPage.java:29)

try
        {
            driver.get("http://domain:port/coco/webapp/login/login.faces");

            driver.findElement(By.id("clientCode")).sendKeys("coco");
            driver.findElement(By.id("systemCode")).sendKeys("consumer");
            driver.findElement(By.id("userId")).sendKeys("ffadmin");
            driver.findElement(By.id("password")).sendKeys("password");

            driver.findElement(By.className("af_commandButton")).click();
            driver.wait();
            Assert.assertTrue(driver.getPageSource().contains("Administration"));

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
like image 864
Aravind Yarram Avatar asked May 02 '11 14:05

Aravind Yarram


3 Answers

I hope this helps you

driver.manage().timeouts().implicitlyWait(long time, java.util.concurrent.TimeUnit unit); 

OR

WebDriverWait wait = new WebDriverWait(driver, long timeOutInSeconds);

WebElement element = wait.until(presenceOfElementLocated(org.openqa.selenium.By locator));

Please note that I have not executed this code as I don't have webdriver but I wrote this after referring to javadocs.

Please refer javadocs for more details on this.

like image 56
9ikhan Avatar answered Oct 23 '22 04:10

9ikhan


You can only wait on an object if you've acquired the lock for it using synchronized.

I don't know whether you're meant to use wait using WebDriver - if you are, you'd need something like:

synchronized (driver)
{
    driver.wait();
}

However, if you're waiting for something to occur, it's more likely that there's an alternative method you're meant to be using. Perhaps WebDriverWait?

like image 20
Jon Skeet Avatar answered Oct 23 '22 04:10

Jon Skeet


driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); is the best solution. Else, you have surround the driver.wait by synchronize block

like image 21
Vinay Pratap Avatar answered Oct 23 '22 03:10

Vinay Pratap