How to optimize testng and seleniums tests

后端 未结 3 1465
陌清茗
陌清茗 2021-02-02 04:12

For my internship, I have to use TestNG and selenium for testing a web-application. But I have a problem, sometimes selenium or the Browser is not working for some random reason

3条回答
  •  清酒与你
    2021-02-02 04:46

    You need not implement onTestFailure. TestNG calls retry automatically when test fails. So no need to override onTestFailure. this causes retry method 2 calls. I implemented retry as below.

    
    private final Map rerunCountForTesCase = new HashMap();
        @Override
        public boolean retry(ITestResult result) {
                    // here i have unique test case IDs for each of test method. 
                    // So using utility method to get it. You can use method calss+name combination instead of testcaseid like me
            String executingTestCaseID = ReportingUtilities.getTestCaseId(result);
            if(rerunCountForTesCase.containsKey(executingTestCaseID))
            {
                count = rerunCountForTesCase.get(executingTestCaseID);
            }
            else
            {
                rerunCountForTesCase.put(executingTestCaseID, 0);
            }
            if (count 0)
                    {
                        logInfo(tcID,"Sleeping for "+timeInSecs+ " secs before rerunning the testcase");
                        Thread.sleep(timeInSecs * CommonFwBase.SHORTWAIT );
                    }
                } catch (InterruptedException e) {
                    logError(null, e.getMessage());
    
                }
    
                rerunCountForTesCase.put(executingTestCaseID, ++count);
                return true;
            }
            return false;
    
        }
    
    

    In the above thread retry getting called twice because of implementation of onTestFailure. I remove the failed results so that when you retry It uses latest result. Otherwise if you have dependency test method it get skipped(though on retry it passed as it uses first result).You might have to handle failed retried results while reporting. You might have to remove the tests that are passing after retry like this.

            m_ptests = suiteTestContext.getPassedTests();
            m_ftests = suiteTestContext.getFailedTests();
            m_stests = suiteTestContext.getSkippedTests();
    
            List methodsToRemove = new ArrayList();
    
            for(ITestResult failed_result : m_ftests.getAllResults())
            {
                String failed_testName = failed_result.getMethod().getMethodName();
                String failingTest_className = failed_result.getClass().getName();
                for(ITestResult passed_result : m_ptests.getAllResults())
                {
                    String passing_testName = passed_result.getMethod().getMethodName();
                    String passingTest_className = failed_result.getClass().getName();
                    if(failed_testName.equals(passing_testName) &&  
                            passingTest_className.equals(failingTest_className) ))
                    {
                        if(passed_result.getEndMillis() > failed_result.getEndMillis())
                        {
    
                            methodsToRemove.add(failed_result.getMethod());
                            break;
                        }
    
                    }
                }
            }
    
            // remove the test that passed on retry
            for(ITestNGMethod failedMethodToRemove : methodsToRemove)
            {
                m_ftests.removeResult(failedMethodToRemove);
            }
    

    Hope it helps to understand retry better.

提交回复
热议问题