Java.IllegalMonitorStateException 是由 Selenium webdriver 代码 driver.wait(0) 引发的。

huangapple go评论73阅读模式
英文:

Java.IllegalMonitorStateException thrown because of Selenium webdriver code driver.wait(0)

问题

以下是代码的翻译部分:

我正在使用以下Selenium webdriver代码并且在driver.wait(0)行收到异常我使用等待语句等待弹出窗口加载并允许测试执行

这是我的堆栈跟踪副本

    java.lang.IllegalMonitorStateException
    	在java.lang.Object.wait(Native Method)    	在Test.MounaTest.test(MounaTest.java:61)    	...

这是代码

    package Test;
    
    // 由Selenium IDE生成
    import org.junit.Before;
    import org.junit.jupiter.api.AfterAll;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.junit.After;
    import static org.junit.Assert.*;
    ...
英文:

I am using the following Selenium webdriver code and I am getting an exception at the driver.wait(0) line. I am using the wait statement to wait for a popup window to be loaded and allow the test to execute.

Here is a copy of my stacktrace:

java.lang.IllegalMonitorStateException
	at java.lang.Object.wait(Native Method)
	at Test.MounaTest.test(MounaTest.java:61)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at java.util.ArrayList.forEach(ArrayList.java:1259)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at java.util.ArrayList.forEach(ArrayList.java:1259)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
	at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
	at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

Here is the code:

    package Test;
    
    // Generated by Selenium IDE
    import org.junit.Before;
    import org.junit.jupiter.api.AfterAll;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.junit.After;
    import static org.junit.Assert.*;
    import static org.hamcrest.CoreMatchers.is;
    import static org.hamcrest.core.IsNot.not;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.Keys;
    import java.util.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    public class MounaTest {
      private static WebDriver driver;
      private Map<String, Object> vars;
      JavascriptExecutor js;
      @BeforeEach
      public void setUp() {
        driver = new ChromeDriver();
        js = (JavascriptExecutor) driver;
        vars = new HashMap<String, Object>();
      }
      @AfterAll
      public static void tearDown() {
        driver.quit();
      }
      public String waitForWindow(int timeout) {
    	    try {
    	      Thread.sleep(timeout);
    	    } catch (InterruptedException e) {
    	      e.printStackTrace();
    	    }
    	    Set<String> whNow = driver.getWindowHandles();
    	    Set<String> whThen = (Set<String>) vars.get("window_handles");
    	    if (whNow.size() > whThen.size()) {
    	      whNow.removeAll(whThen);
    	    }
    	    return whNow.iterator().next();
    	  }
      @Test
      public void test() throws InterruptedException {
    	    driver.get("https://tinder.com/");
    	    System.out.println("MOUNA CAMELIA");
    	    driver.manage().window().setSize(new Dimension(1552, 840));
    	    driver.wait(0); 
    	    driver.findElement(By.cssSelector(".Miw\\(120px\\) .l17p5q9z")).click();
    	    driver.switchTo().frame(0);
    	    vars.put("window_handles", driver.getWindowHandles());
    	    driver.findElement(By.cssSelector(".ssJRIf")).click();
    	    vars.put("win8338", waitForWindow(2000));
    	    vars.put("root", driver.getWindowHandle());
    	    driver.switchTo().window(vars.get("win8338").toString());
    	    driver.findElement(By.cssSelector(".fFW7wc-ibnC6b-r4m2rf:nth-child(4) > .fFW7wc-ibnC6b-ssJRIf")).click();
    	    driver.close();
    	    driver.switchTo().window(vars.get("root").toString());
    	    {
    	      WebElement element = driver.findElement(By.linkText("Terms"));
    	      Actions builder = new Actions(driver);
    	      builder.moveToElement(element).perform();
    	    }
    	    driver.findElement(By.cssSelector(".Mx\\(a\\):nth-child(4) path")).click();
    	    driver.findElement(By.cssSelector(".Sq\\(28px\\) > path")).click();}
    }

答案1

得分: 0

This error message...

java.lang.IllegalMonitorStateException

...at the line driver.wait(0); implies that it isn't a valid line of code as per current Selenium Java bindings. Hence you see the error.


Solution

In case you are looking for a hardcoded wait, you need to replace the line with:

Thread.sleep(3000); //program execution gets halted for 3 seconds

You can also induce ImplicitWait, then you need to replace the line with:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); //forces the driver will poll the HTML DOM for 10 seconds.
英文:

This error message...

java.lang.IllegalMonitorStateException

...at the line driver.wait(0); implies that it isn't a valid line of code as per current Selenium Java bindings. Hence you see the error.


Solution

Incase you are looking for a hardcoded wait, you need to replace the line with:

Thread.sleep(3000); //program execution gets halted for 3 seconds

You can also induce ImplicitWait, then you need to replace the line with:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); //forces the driver will poll the HTML DOM for 10 seconds.

huangapple
  • 本文由 发表于 2023年6月13日 07:41:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定