How to add program code to your automated tests?

So far we have only been configuring our automated website tests here, which allows you to quickly write your automation tests, but comes at the expense of customisation.

This feature allows you to programmatically customise your tests to an extend that should allow you to achieve most of your automated testing out-of-the-box.

The <setup> and/or <teardown> feature allows you to run any custom programming tasks either before/after your <testsuite> executes, or alternatively, before/after only some of your selected <userstory>, <scenario> or <step> nodes.

To clarify: This means each <setup> and/or <teardown> node does not inherit further down the XML hierarchy, but it runs exactly at the point where it has been specified!

This is how you can execute your own custom program code using the <setup> and/or <teardown> nodes provided.

Please note that this is not restricted to just executing PHP code, but you can also execute your own custom *.aspx, *.sh or any other program code, as long as it is available on the file system of where you run the tests from.

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <fixture>
        <setup label="Create Temp File">
            php -r "fopen('../../../../../demo_application/temp_file.txt', 'w');"
        </setup>
        <teardown label="Remove Temp File">
            php -r "unlink('../../../../../demo_application/temp_file.txt');"
        </teardown>
    </fixture>
    <userstory>
        <scenario>
            <step title="Test that there IS NO temp file to start off with">
                <url>http://automateyourtests.com/demo_application/temp_file.txt</url>
                <status>404</status>
            </step>
            <step title="Test that there now IS a temp file, because the 'setup' fixture creates the file it BEFORE anything else in this 'step'">
                <setup>Create Temp File</setup>
                <url>http://automateyourtests.com/demo_application/temp_file.txt</url>
                <status>200</status>
                <teardown>Remove Temp File</teardown>
            </step>
            <step title="Test that there is NO temp file anymore, because AFTER the last 'step' the 'teardown' fixture removed the file">
                <url>http://automateyourtests.com/demo_application/temp_file.txt</url>
                <status>404</status>
            </step>
        </scenario>
    </userstory>
</testsuite>

This is how you would execute multiple <setup> and/or <teardown> commands.

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <fixture>
        <setup label="Create Temp File 1">
            php -r "fopen('../../../../../demo_application/temp_file_1.txt', 'w');"
        </setup>
        <setup label="Create Temp File 2">
            php -r "fopen('../../../../../demo_application/temp_file_2.txt', 'w');"
        </setup>
        <teardown label="Remove Temp File 1">
            php -r "unlink('../../../../../demo_application/temp_file_1.txt');"
        </teardown>
        <teardown label="Remove Temp File 2">
            php -r "unlink('../../../../../demo_application/temp_file_2.txt');"
        </teardown>
    </fixture>
    <userstory>
        <scenario>
            <step title="Show how to run multiple 'setup' tasks by creating temp files 1 and 2">
                <setup>Create Temp File 1</setup>
                <setup>Create Temp File 2</setup>
            </step>
            <step title="Test that temp file 1 has been created">
                <url>http://automateyourtests.com/demo_application/temp_file_1.txt</url>
                <status>200</status>
            </step>
            <step title="Test that temp file 2 has been created">
                <url>http://automateyourtests.com/demo_application/temp_file_2.txt</url>
                <status>200</status>
            </step>
            <step title="Show how to run multiple 'teardown' tasks by removing temp files 1 and 2">
                <teardown>Remove Temp File 1</teardown>
                <teardown>Remove Temp File 2</teardown>
            </step>
            <step title="Test that temp file 1 has been removed">
                <url>http://automateyourtests.com/demo_application/temp_file_1.txt</url>
                <status>404</status>
            </step>
            <step title="Test that temp file 2 has been removed">
                <url>http://automateyourtests.com/demo_application/temp_file_2.txt</url>
                <status>404</status>
            </step>
        </scenario>
    </userstory>
</testsuite>

Please note: This feature relies on your php.ini directive “disable_functions” on your server not to have the PHP “exec” function included. As long as your test server is not on a shared web host, you should be alright.

Please also note: We recommend to restrict the php.ini directive “open_basedir” on your server to those dedicated directories. Both <setup> and/or <teardown> are very powerful and it is your responsibility to safeguard your application server.

  • Where you decide to store all your general PHP code and
  • Where you decide to store any other programming code that you intend to execute through and . That way no code outside these directories can ever be executed through and which helps with your server security.

Warning: count(): Parameter must be an array or an object that implements Countable in /home/nemyoe7sj5jr/public_html/wp-includes/class-wp-comment-query.php on line 388