Java 一旦发生异常,如何停止执行测试中的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18494932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:33:34  来源:igfitidea点击:

How to stop execution of the code in Testing once an exception is occurred

javaselenium-webdrivertestng

提问by 2676698

I'm automating a web application using Selenium-web driver. Once the script breaks at any point I want to stop execution of the code instead of continuing with the next method so that I can know where the automation is breaking. Could anyone please help me how can I do that.

我正在使用 Selenium-web 驱动程序自动化 Web 应用程序。一旦脚本在任何时候中断,我想停止执行代码而不是继续使用下一个方法,以便我可以知道自动化在哪里中断。任何人都可以帮助我我该怎么做。

Here is the snippet of the catch block(i.e. the exception I'm using now)

这是 catch 块的片段(即我现在使用的异常)

try {
   //code
} 
catch(Exception e){
   System.out.println("Exception occurred *** " +e.getMessage());
}

采纳答案by Apcragg

If I understand what you're asking, I would put this in the catch block to help you trace the problem.

如果我明白你在问什么,我会把它放在 catch 块中以帮助你追踪问题。

catch(Exception e)
{
   e.printStackTrace();
   System.exit(1)
}

or alternatively you could put a print statement in each catch block that identifies which block failed.

或者,您可以在每个 catch 块中放置一个打印语句,以标识哪个块失败。

ie.

IE。

try{
//make a cake
}
catch(CakeException e)
{
   System.out.println("Failed at cake baking);
}

try{
//make a salad
}
catch(LettuceException e)
{
   System.out.println("Failed at lettuce chopping);
}

回答by Jk1

Well, tests are supposed to be independent by design, so the fail of the one should not affect the others. If you stop the execution on the first failed test , then how do you know how many more errors you have in your code?

好吧,测试在设计上应该是独立的,所以一个的失败不应该影响其他的。如果您在第一个失败的测试中停止执行,那么您怎么知道您的代码中还有多少错误?

That's a kind of a general idea explaining why testng doesn't contain desired feature out of the box. You can, however, still get an advantage of test dependency mechanism provided by testng:

这是一种解释为什么 testng 不包含开箱即用的所需功能的一般想法。但是,您仍然可以利用 testng 提供的测试依赖机制:

@Test
public void serverStartedOk() {}

@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}

So you need to chain your methods this way to ensure execution is broken at the first fail.

因此,您需要以这种方式链接您的方法,以确保在第一次失败时执行被中断。

回答by maks tkach

that is my way control all exeptions.

这就是我控制所有例外的方式。

create method which visible for all classes

创建对所有类可见的方法

protected void exeption(String variable){
  driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    System.out.println(" ");
    System.out.println("stop test ____ something your code - exeption ______");
    System.out.println(" ");
    System.out.println("Check Variables:  " + variable);
    System.out.println("____________________________________________");
    driver.findElement(By.xpath("//ooppss"));
}

private void testName() {
String variable = "something";
        if(1!=2){
      exeption(variable);
    }
}