When running automated tests, sometimes the results of the use case run due to unstable network, test environment or third-party environment being restarted Unstable, sometimes can run but sometimes can’t run. Use case failures caused by these difficult-to-reproduce environmental factors can be very troublesome for testers, and troubleshooting is time-consuming and there is not much room for improvement. In response to this situation, we can retry and monitor the failure results based on TestNG’s retryer and listener. Once the failed test case is monitored, start our own customized retry method and processing method to achieve re-run and screenshots. .
IRetryAnalyzer implementation failed to retry
TestNG provides the IRetryAnalyzer interface. By implementing this interface, users can customize the retry method. This interface only defines one retry method法< /span>
The tester implements the IRetryAnalyzer interface and decides whether to continue according to the status of ITestResuly Try until the custom maximum number of retries
TestNG will automatically monitor the results of the test method on the test method with RetryAnalyzer added, and call IRetryAnalyzer to process different results. The custom retry method in this article will judge the running result of each test method. If the test succeeds, no action is taken to exit the method; if the test fails And the number of executions is less than the maximum number of attempts, then enter the retry and add one to the number of runs; if the test fails and the number of attempts is greater than the maximum number of attempts, exit the method and mark the method as failed.
IestListener implementation failed screenshot
TestNG provides the ITestListener interface. By extending and implementing this interface, you can Implement a custom success/failure handling method. The ITestListener interface mainly defines the following methods:
• onTestStart
• onTestSuccess
• onTestFailure
• onTestSkipped
• onTestFailedButWithinSuccessPercentage
• onStart
• onFinish
The above method mainly defines the test class before construction/after running, and before each @Test annotation method in the test class is created, after obtaining the running result (run successfully/ Run failure/skip run/partial success) should trigger the action. After customizing the ITestListener interface, you can implement custom processing for the test method.
Implemented a custom listener In the future, you can add corresponding annotations before the methods that need to fail and retry, Specify the processed listener through the annotation’s value attribute. The method that adds the listener will call the method implemented in the listener at runtime, otherwise it will not do any processing
To achieve automatic screenshot failure, you need to in Add screenshot processing in the onTestFailure method of the listener. Selenium supports converting WebDriver into a screenshot tool, and save the screenshot to the hard disk. The screenshot can be processed by the following statement. The method is encapsulated in the onTestFailure of the listener.
Author: