12

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();
        }
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327

5 Answers5

27

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Apparently wait is not required from seleniunm 2 onwards as it supports implicit waits. With implicit waits, you only need to use one of the findElement methods, and if the target element does not exist or is not visible, Selenium will wait until it is. Unfortunately this example is out of date, however if you look at some of my more recent examples, hopefully you will find them to be working. http://seleniumexamples.com/blog/examples/selenium-2-examples/ – Aravind Yarram May 02 '11 at 17:20
9

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.

Thomas
  • 1,085
  • 9
  • 14
9ikhan
  • 1,177
  • 3
  • 11
  • 22
  • There is an error here, the last line should not include the timeout as a parameter for wait.until. At least, not as of the 2015 code (but I can't see why the old version would have that duplication either). – Erica Kane Apr 02 '15 at 00:39
  • This worked for me `driver.manage().timeouts().implicitlyWait(2000,TimeUnit.MILLISECONDS);` – uzay95 Nov 10 '20 at 19:43
2

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

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17
0

This is a necro, but since there aren't better answers, and someone else might happen by: you're calling the wrong method.

You probably intended to call the selenium method to wait for a condition: https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html

What you actually called was the very primitive multithreading method (to wait until someone else has called notify() on your thread): https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

Hazel T
  • 859
  • 1
  • 8
  • 22
0

Use the below code, this will work.

synchronized (driver)
{
  driver.wait(2000);
}
driver.context(NATIVE_APP);
driver.findElementByXPath("//android.widget.Button[@resourceid=‘android:id/button1’]").click();
slfan
  • 8,950
  • 115
  • 65
  • 78