How to Deal with Potential Timing Errors

If you suspect that your tests are taking too long to execute, your first step should be to look at the TestNG HTML report. As you learned in the previous section, this report includes an overview of the running times of executed methods.

We can see the accumulated times in the Times option of the HTML report:

Also, we can see more detailed information in the Chronological view of the execution:

With this information, you can identify any methods that seem to be taking too long to run. The following are some suggestions to take into account when identifying possible solutions:

  • Review the code for the method to gauge its complexity:
    • How many lines of code does it have? The longer a test is, the longer it can take to run.
    • What does the method do? For example, a test that verifies the entire checkout process of an online store can include so many tasks or subprocesses that we would expect it to take a long time to run. A good practice would be to separate the method into smaller ones, if possible.
  • Review the code for the method and check its interaction with external services or methods:
    • Does the method interact with other methods in the test? Does the interaction involve waiting for information from other methods? It might be that the method is only taking long because a method it depends on has a long execution time.
    • Does the method interact with other external services (such as a database)? An external service can slow down your methods if the service runs a complex process, or if it is just slow by design. In those cases, there might not be much you can do, because the service is out of your domain. Explore some alternatives, such as mocking the dependencies.

In testing, mocking is a technique that focuses on isolating the behavior of the specific part of the system that you want to test. In short, this is done by replacing the dependencies on other complex objects or systems with mocks. Mocks are objects or simple systems that simulate the behaviors of real dependencies.
  • Review your hardware and software environments:
    • Perhaps your test server is running other heavy processes while your test is being executed.
    • Verify the configuration of your hardware.
    • Verify the configuration of your testing environment. Read the documentation for your IDE, or any other testing tool that you are using.
    • If you are using a test framework (which you should be), read its guidelines.
    • Sometimes, you will be able to write a test suite from scratch; but, usually, you will join a team that has already written some automated tests with Selenium. This is a great opportunity to review the waiting times in the tests: spot any Thread.sleep() calls, and look for too-generous waiting times. Excessively long wait times can slow down your tests, or, even worse, hide a real bug in the web application. One suggestion is to use the ExpectedConditions class of Selenium, which provides different waiting conditions; you can even use custom waits, so that the test manages not only the ideal scenario (the text to appear on the screen), but also potential exception conditions.

You should now understand how to track down timing errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.89