Java 当我的测试失败或出现异常时,我需要始终注销我的应用程序。我该怎么做?

Java 当我的测试失败或出现异常时,我需要始终注销我的应用程序。我该怎么做?,java,selenium,testing,testng,Java,Selenium,Testing,Testng,我有一个测试用例,如下所示: @Test public void checkSomething() { //line1 //line2 //line3 //line4[Exception occurs here] //line5 //line6 //line7 homepage.Logout(); } 现在,如果第4行出现异常,那么我的应用程序将永远不会注销[line7]。这将导致我的进一步测试用例失败,因为他们将无法登录,因为用户会话将处于活动状态。 当测试过早失败时,如何使注销始终发生 我

我有一个测试用例,如下所示:

@Test
public void checkSomething()
{
//line1
//line2
//line3
//line4[Exception occurs here]
//line5
//line6
//line7 homepage.Logout();
}
现在,如果第4行出现异常,那么我的应用程序将永远不会注销[line7]。这将导致我的进一步测试用例失败,因为他们将无法登录,因为用户会话将处于活动状态。 当测试过早失败时,如何使注销始终发生

我尝试将注销逻辑放入@AfterMethod。它工作得很好,但是用@AfterMethod?

这样的配置方法编写测试代码是最好的做法,我用C#工作,但是在所有语言中,这个概念很可能是相同的。在我的例子中,我在基类中使用了一个所谓的“TearDown”标记来标记一个应该在测试之后运行的方法。所有测试都从基类继承此方法,并进行相应的处理。在过去的几年里,这一做法效果良好,据我所知,任何类似的概念都被视为最佳实践

在伪代码中:

    [TearDown]
    public void Cleanup()
    {
        try
        {
            Logout();
            OtherStuffLikeClosingDriver();
        }
        catch (Exception ex)
        {
            Log(ex);                            // Obviously, this logging function needs to generate logs that are easily readable, based on the given exception.
            FinishTest(testInstance, testName); // Handles critical flows that should always be finished (and "should" not be able to error out)
            throw ex;                           // In my case, throwing the exception again makes sure that the exception is shown in the test output directly. This often speeds up the first diagnose of a failed test run.
        }
    }

只需确保相应地处理异常和类似情况:@AfterMethod中的逻辑不应被意外问题打断。

将注销置于
@AfterMethod
中会很好,但请确保以有效的方式进行

  • 如果只有测试失败,请检查注销
  • 避免使用try-catch,因为它等待给定的时间(ImplicitWait)检查元素是否存在,然后进入catch块,而不是使用列表
使用
@AfterMethod

 @AfterMethod 
 public void screenShot(ITestResult result){
       if(ITestResult.FAILURE==result.getStatus()){
            List<WebElement> username = driver.findElement(By.locator); // element which displays if user is logged in
            if(!username.isEmpty())
                // steps to logout will go here
            }
       }
  }
在testng.xml中添加以下标记

<listeners>
   <listener class-name="com.pack.listeners.TestListener"/> // your created class name with package which implemented ITestListener
</listeners>

//使用实现ITestListener的包创建的类名

Java使用“try catch finally”并将注销放在finally子句中,而使用@After方法将起作用,因为它破坏了它的真正目的,它的操作范围应限于设置和拆除与测试套件相关的服务和资源,而不是SUT,例如WebDriver或ExpectedData源。如果我使用test listener,包含测试用例的类是否应该扩展实现ITestListener接口的类,然后重写其onTestFailure方法?听起来不错。这就是你所说的,对吗?@Rajan,不,你应该创建一个单独的类,并在该类中实现
ITestListener
,该类将要求覆盖所有未实现的方法,你必须这样做,此外,你还必须在
onTestFailure
方法中编写注销代码。最后在listener中添加该类,如中所示answer@Rajan,参考这个博客,我明白你的意思。因此,基本上在类中,我将实现iTestListener接口并定义其所有方法,如onTestFailure等。在那里,我必须创建我的HomePage类的一个对象,该对象具有注销功能,我将从onTestFailure方法内部调用该函数。当然,我将在testng.xml中添加这个类作为侦听器。是吗?@Rajan,是的,内部
if(ITestResult.FAILURE==result.getStatus()){
this
<listeners>
   <listener class-name="com.pack.listeners.TestListener"/> // your created class name with package which implemented ITestListener
</listeners>