How to automatically test that a URL does (not) return a HTTP status code?

Here we show you how to test that your web pages result in certain specified HTTP status codes (typically codes of the 200/300 range). Equally, the automation test tool can also make sure that they do not result in other specified HTTP status codes that would indicate a server side failure (typically anything of the 400/500 range).

The following example makes sure that your <url> results in the HTTP <status> code “200”.

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <userstory title="Test that the web page request returns a (healthy) HTTP status code of 200">
        <scenario>
            <step>
                <url>http://automateyourtests.com/demo_application/</url>
                <status>200</status>
            </step>
        </scenario>
    </userstory>
</testsuite>

This tests that the <url> results in either the HTTP <status> code “200” or “301” (logical or).

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <userstory title="Test that the web page request returns a (healthy) HTTP status code of either 200 or 301 (logical or)">
        <scenario>
            <step>
                <url>http://automateyourtests.com/demo_application/wp-admin/</url>
                <status>200</status>
                <status>301</status>
                <status>302</status>
            </step>
        </scenario>
    </userstory>
</testsuite>

This tests that your <url> has a <notstatus> of “404”, which means that it should not result in “404”.

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <userstory title="Test that the web page request does NOT return a (faulty) HTTP status code of 404">
        <scenario>
            <step>
                <url>http://automateyourtests.com/demo_application/</url>
                <notstatus>404</notstatus>
            </step>
        </scenario>
    </userstory>
</testsuite>

And finally, this makes sure that the <url> does not result in neither in status code “404” nor in “500” (logical and).

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <userstory title="Test that the web page request does NOT return a (faulty) HTTP status code of 404 and neither 500 (logical and)">
        <scenario>
            <step>
                <url>http://automateyourtests.com/demo_application/</url>
                <notstatus>404</notstatus>
                <notstatus>500</notstatus>
            </step>
        </scenario>
    </userstory>
</testsuite>