简体   繁体   English

未处理的异常:java.lang.InterruptedException

[英]Unhandled Exception: java.lang.InterruptedException

During the write automation test in selenium, I received Unhandled Exception: java.lang.InterruptedException for line which uses "wait"在 selenium 的写入自动化测试期间,我收到 Unhandled Exception: java.lang.InterruptedException for line which uses "wait"

   public static void clickFirstAccountDetails() {
        getDriver().switchTo().frame(0);
        getDriver().wait(5000);
        getDriver().findElement(By.partialLinkText("Xyz")).click();
    }

Unhandled Exception: java.lang.InterruptedException未处理的异常:java.lang.InterruptedException

Other methods like switchTo, findElement are working fine.其他方法如 switchTo、findElement 工作正常。

Yes, it is a compiler error in java to not explicitly deal with a so-called checked exception, and InterruptedException is checked.是的,java 没有显式处理所谓的已检查异常是编译器错误,并检查了InterruptedException。 The solution is fortunately quite simple:幸运的是,解决方案非常简单:

Just add the exception to your throws line.只需将异常添加到 throws 行。 In fact, for any test code, feel free to just tack throws Exception at the end of it.事实上,对于任何测试代码,您都可以随意在其末尾添加throws Exception A test method that throws is treated as a failed test which is almost always precisely what you want to happen, and this way, every detail that is available in the exception will end up in your test report, also what you usually want.抛出的测试方法被视为失败的测试,这几乎总是您想要发生的事情,这样,异常中可用的每个细节都将最终出现在您的测试报告中,这也是您通常想要的。 So, update the first line to read:因此,将第一行更新为:

public static void clickFirstAccountDetails throws Exception {

and that'll fix it.这将解决它。 You may want to do:你可能想做:

public static void clickFirstAccountDetails throws InterruptedException {

instead if you prefer to continue to get compiler errors when you expand your test code and include calls to code that throws other checked exceptions.相反,如果您希望在扩展测试代码并包含对引发其他已检查异常的代码的调用时继续获得编译器错误。

wait() method throws InterruptedException that is why you are getting Unhandled Exception: java.lang.InterruptedException wait() 方法抛出 InterruptedException 这就是为什么你得到未处理的异常:java.lang.InterruptedException

public static void clickFirstAccountDetails() {
        getDriver().switchTo().frame(0);
        try{
           getDriver().wait(5000);
          }catch(InterruptedException){}
        getDriver().findElement(By.partialLinkText("Xyz")).click();
    }

you can use try catch (Exception handling)你可以使用 try catch (异常处理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用swingworker的java.lang.InterruptedException - java.lang.InterruptedException using swingworker Azure MobileService EasyApi java.lang.interruptedException - Azure MobileService EasyApi java.lang.interruptedException 执行Java类时发生异常。 java.lang.InterruptedException - An exception occured while executing the Java class. java.lang.InterruptedException 睡眠中断异常:java.lang.InterruptedException:睡眠中断 - 如何让浏览器超时 40 分钟 - Sleep Interupt Exception: java.lang.InterruptedException: sleep interrupted - How to give timeout for browser for 40mins 引起原因:java.lang.RuntimeException:java.lang.InterruptedException - Caused by: java.lang.RuntimeException: java.lang.InterruptedException 使用Process.waitFor()和java.lang.InterruptedException的Java多线程 - java multithreading using Process.waitFor() and java.lang.InterruptedException SpringApplication.exit() retuns java.lang.InterruptedException: null - SpringApplication.exit() retuns java.lang.InterruptedException: null 执行 Apache HttpClient 时出现 java.lang.InterruptedException - java.lang.InterruptedException while executing Apache HttpClient java.lang.InterruptedException导致Web应用程序无法部署 - java.lang.InterruptedException causes web application not deploy 运行批处理文件时出现 java.lang.InterruptedException - java.lang.InterruptedException while running a batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM