PART 5: defining test rules to catch javascript errors

BUILDING TEST AUTOMATION FRAMEWORK WITH TEST JUNKIE & SELENIUM WEBDRIVER

THE GOAL

Catch any and all Javascript errors thrown during testing.

THE HOW

One of the best & easiest things you can do in your black box/GUI tests is to always be on the look out for JavaScript errors. It will allow you to catch a lot of bugs and regression issues that made its way through to the codebase. To do this, we will use whats known, in Test Junkie, as Test Rules.

Test Rules can be added to any of the test suites that have been created. All we need to do is create a class and define our rules. So lets do that now. I'm going to add the following code to the TestRules.py.

from test_junkie.rules import Rules

from src.pages.Browser import Browser


class TestRules(Rules):

    def after_test(self, **kwargs):

        log_msgs = Browser.get_driver().get_log("browser")
        errors = []
        if log_msgs:
            for msg in log_msgs:
                errors.append(msg.get('message', None))
        if errors:
            raise AssertionError("{} Errors/Warnings in console logs :: {}".format(len(errors), errors))

        Browser.shutdown()  # moved from suite's afterTest() to here

Now, if this rules are added to a suite, any time a test in that suite has finished running, this rule will run right after in the same thread. Thus we can can make a call to the Browser object and check the browser logs. If log has error messages or warnings, we will throw an AssertionError and the test will fail with an appropriate stack trace.

The only thing left to do now is to add the rules to the suites where we want to use them. I will be adding it to all of my test suites which will require me to make a small change to each of the suite definitions. Lets start with DocumentationSuite.py
...
from tests.TestRules import TestRules

@Suite(rules=TestRules)
class DocuamentationSuite:
...
This is literally the only change I need to do. So all I did was imported TestRules and added rules=TestRules to the @Suite() decorator. Now I'm going to do the same for NavigationSuite.py

...
from tests.TestRules import TestRules

@Suite(parameters=[HomePage, DocumentationPage, AboutPage], rules=TestRules)
class NavigationSuite:
...
Well, there is one more thing we need to do. Because we still want to have access to the same browser that ran our tests, we need to remove, afterTest() decorators in every suite. The reason for this is very simple. It comes down to the sequence of events. The suite's afterTest() decorator will run before the after_test() rule does. Due to the fact we perform Browser.shutdown() in the decorator, when our rule runs it wont have access to same browser that was used to run the tests. Therefore, I'm going to remove the decorators and add the shutdown logic to the rule itself.

That wraps up this tutorial. Thanks for reading and happy testing!

footer-background-top