简体   繁体   English

如果 20% 或第 20 个测试用例测试方法失败,我如何停止 selenium 自动化?

[英]How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?

I have 100 test cases, if 20% or first 20 test cases all fail how can I stop the execution?我有 100 个测试用例,如果 20% 或前 20 个测试用例都失败了,我该如何停止执行? Already have testng ITestResult where should I break the build?已经有 testng ITestResult 我应该在哪里中断构建?

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC001(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code 
}

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC002(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code 
}

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC003(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code 
}
///////////////////////////////

Where can I break this suite if Result, "FAIL is over 20? DO i require to create new class or can I add in same below?如果结果,我在哪里可以打破这个套件,“失败超过 20?我需要创建新的 class 还是我可以在下面添加相同的?

@AfterMethod(alwaysRun = true)
public void reporterDataResults(ITestResult Result) throws InterruptedException {
    boolean flag = false;
    Testfail = TestResultStatus.Testfail;

    /*System.out.println("test fail flag in AfterMethod: " + Testfail); */
    if (Result.getStatus() == ITestResult.SKIP) {
        Resmark.put(Result.getName(), "");
        captureScreenShot(Result, "SKIP", getDriver());
        Reporter.log(Result.getName() + " is SKIPPED");
        Add_Log.info(Result.getName() + " is SKIPPED");
        TestResultTL.put(Result.getName(), "SKIP");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
    } else if (Result.getStatus() == ITestResult.FAILURE) {

        Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
        String resultout = String.join(" | ", values);
        System.out.println(resultout);
        Resmark.put(Result.getName(), resultout);

        captureScreenShot(Result, "FAIL", getDriver());
        Reporter.log(Result.getName() + " is FAIL");
        Add_Log.info(Result.getName() + " is FAIL");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
        TestResultTL.put(Result.getName(), "FAIL");


    } else {
        captureScreenShot(Result, "PASS", getDriver());
        Resmark.put(Result.getName(), "");
        Reporter.log(Result.getName() + " is PASS");
        Add_Log.info(Result.getName() + " is PASS");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
        TestResultTL.put(Result.getName(), "PASS");

    }
    Testskip = false;
    TestResultStatus.Testfail = false;

}

You can implementISuiteListener and in onFinish method you'll have access to ISuite and ISuiteResult您可以实现ISuiteListener并在onFinish方法中访问ISuiteISuiteResult

Then you can do然后你可以做

public void onFinish(ISuite suite) {

        final Map<java.lang.String,ISuiteResult>  res = suite.getResults();

                for (ISuiteResult r : res.values()) {
                  context =  r.getTestContext()  ;
                  failedTestCases =context.getFailedTests().size();
                 }
         }

size() will give you number of failed test for that suite. size()将为您提供该套件的失败测试数。 Once you know that number you can implement to stop execution using strategies in this一旦您知道该数字,您就可以使用本文中的策略实施以停止执行

If your test cases are in different suites then in each call to onFinish method you can count number of failed test cases per suite and based on that stop execution.如果您的测试用例位于不同的套件中,那么在每次调用onFinish方法时,您可以计算每个套件的失败测试用例数,并基于该停止执行。

One other alternative is to implement ITestListener .另一种选择是实现ITestListener In onTestFailure method you have access to ITestResultonTestFailure方法中,您可以访问ITestResult

You can count how many times onTestFailure method is called and based on that stop execution.您可以计算onTestFailure方法被调用的次数并基于该停止执行。 I think implementing ITestListener is more suitable and easy in your case.我认为在您的情况下实施ITestListener更合适和更容易。

Here, I edited to explain how you'd implement listener在这里,我进行了编辑以解释您将如何实现侦听器

import org.testng.ISuiteListener;

public class listener implements Itestlistener {
            public int i = 0;

             public void onTestFailure(ITestResult result) {

                     result.getName();
                     i++;
                     //your break logic goes here
                       if (i ==20){
                              // do something or call some function to stop execution
                             }
         }

     }

You can read more about testng listenershere .您可以在此处阅读有关 testng 听众的更多信息。

For your EDIT above (if you want to go that way).对于上面的编辑(如果你想 go 那样)。 Though I still think you should implement listener, which is more cleaner.尽管我仍然认为您应该实现更清洁的侦听器。 It will be called only when the test fails.只有在测试失败时才会调用它。

But do the same thing as I did in onTestFailure method, add a counter and increase it in else if .但是做和我在onTestFailure方法中做的一样的事情,添加一个计数器并在else if中增加它。

   public int i = 0;  //do this in your class

then in your method然后在你的方法中

 else if (Result.getStatus() == ITestResult.FAILURE) {
    i++;  //increase counter here
    Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
    String resultout = String.join(" | ", values);
    System.out.println(resultout);
    Resmark.put(Result.getName(), resultout);

    captureScreenShot(Result, "FAIL", getDriver());
    Reporter.log(Result.getName() + " is FAIL");
    Add_Log.info(Result.getName() + " is FAIL");
    if (!(getDriver() == null)) {
        closeWebBrowser();
    }
       TestResultTL.put(Result.getName(), "FAIL");
      if (i==20){
         // stop execution here
      }

  }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM