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

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

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

问题

以下是代码的翻译部分:

  1. 我正在使用以下Selenium webdriver代码并且在driver.wait(0)行收到异常我使用等待语句等待弹出窗口加载并允许测试执行
  2. 这是我的堆栈跟踪副本
  3. java.lang.IllegalMonitorStateException
  4. java.lang.Object.wait(Native Method)
  5. Test.MounaTest.test(MounaTest.java:61)
  6. ...
  7. 这是代码
  8. package Test;
  9. // 由Selenium IDE生成
  10. import org.junit.Before;
  11. import org.junit.jupiter.api.AfterAll;
  12. import org.junit.jupiter.api.BeforeEach;
  13. import org.junit.jupiter.api.Test;
  14. import org.junit.After;
  15. import static org.junit.Assert.*;
  16. ...
英文:

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:

  1. java.lang.IllegalMonitorStateException
  2. at java.lang.Object.wait(Native Method)
  3. at Test.MounaTest.test(MounaTest.java:61)
  4. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  5. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  6. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  7. at java.lang.reflect.Method.invoke(Method.java:498)
  8. at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
  9. at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
  10. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
  11. at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
  12. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
  13. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
  14. at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
  15. at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
  16. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
  17. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
  18. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
  19. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
  20. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
  21. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
  22. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
  23. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  24. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
  25. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
  26. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
  27. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
  28. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  29. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
  30. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  31. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
  32. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  33. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
  34. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
  35. at java.util.ArrayList.forEach(ArrayList.java:1259)
  36. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
  37. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
  38. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  39. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
  40. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  41. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
  42. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  43. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
  44. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
  45. at java.util.ArrayList.forEach(ArrayList.java:1259)
  46. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
  47. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
  48. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  49. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
  50. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
  51. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
  52. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  53. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
  54. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
  55. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
  56. at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
  57. at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
  58. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
  59. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
  60. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
  61. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
  62. at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
  63. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
  64. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
  65. at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
  66. at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
  67. at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
  68. at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
  69. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
  70. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
  71. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
  72. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

Here is the code:

  1. package Test;
  2. // Generated by Selenium IDE
  3. import org.junit.Before;
  4. import org.junit.jupiter.api.AfterAll;
  5. import org.junit.jupiter.api.BeforeEach;
  6. import org.junit.jupiter.api.Test;
  7. import org.junit.After;
  8. import static org.junit.Assert.*;
  9. import static org.hamcrest.CoreMatchers.is;
  10. import static org.hamcrest.core.IsNot.not;
  11. import org.openqa.selenium.By;
  12. import org.openqa.selenium.WebDriver;
  13. import org.openqa.selenium.firefox.FirefoxDriver;
  14. import org.openqa.selenium.chrome.ChromeDriver;
  15. import org.openqa.selenium.remote.RemoteWebDriver;
  16. import org.openqa.selenium.remote.DesiredCapabilities;
  17. import org.openqa.selenium.Dimension;
  18. import org.openqa.selenium.WebElement;
  19. import org.openqa.selenium.interactions.Actions;
  20. import org.openqa.selenium.support.ui.ExpectedConditions;
  21. import org.openqa.selenium.support.ui.WebDriverWait;
  22. import org.openqa.selenium.JavascriptExecutor;
  23. import org.openqa.selenium.Alert;
  24. import org.openqa.selenium.Keys;
  25. import java.util.*;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28. public class MounaTest {
  29. private static WebDriver driver;
  30. private Map<String, Object> vars;
  31. JavascriptExecutor js;
  32. @BeforeEach
  33. public void setUp() {
  34. driver = new ChromeDriver();
  35. js = (JavascriptExecutor) driver;
  36. vars = new HashMap<String, Object>();
  37. }
  38. @AfterAll
  39. public static void tearDown() {
  40. driver.quit();
  41. }
  42. public String waitForWindow(int timeout) {
  43. try {
  44. Thread.sleep(timeout);
  45. } catch (InterruptedException e) {
  46. e.printStackTrace();
  47. }
  48. Set<String> whNow = driver.getWindowHandles();
  49. Set<String> whThen = (Set<String>) vars.get("window_handles");
  50. if (whNow.size() > whThen.size()) {
  51. whNow.removeAll(whThen);
  52. }
  53. return whNow.iterator().next();
  54. }
  55. @Test
  56. public void test() throws InterruptedException {
  57. driver.get("https://tinder.com/");
  58. System.out.println("MOUNA CAMELIA");
  59. driver.manage().window().setSize(new Dimension(1552, 840));
  60. driver.wait(0);
  61. driver.findElement(By.cssSelector(".Miw\\(120px\\) .l17p5q9z")).click();
  62. driver.switchTo().frame(0);
  63. vars.put("window_handles", driver.getWindowHandles());
  64. driver.findElement(By.cssSelector(".ssJRIf")).click();
  65. vars.put("win8338", waitForWindow(2000));
  66. vars.put("root", driver.getWindowHandle());
  67. driver.switchTo().window(vars.get("win8338").toString());
  68. driver.findElement(By.cssSelector(".fFW7wc-ibnC6b-r4m2rf:nth-child(4) > .fFW7wc-ibnC6b-ssJRIf")).click();
  69. driver.close();
  70. driver.switchTo().window(vars.get("root").toString());
  71. {
  72. WebElement element = driver.findElement(By.linkText("Terms"));
  73. Actions builder = new Actions(driver);
  74. builder.moveToElement(element).perform();
  75. }
  76. driver.findElement(By.cssSelector(".Mx\\(a\\):nth-child(4) path")).click();
  77. driver.findElement(By.cssSelector(".Sq\\(28px\\) > path")).click();}
  78. }

答案1

得分: 0

This error message...

  1. 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:

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

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

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

This error message...

  1. 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:

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

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

  1. 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:

确定