How to automatically test for a JSON value?

The following automated test makes sure that the JSON that the <url> returns <contains> the specified sub string “ok” anywhere within the JSON return string.

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <userstory title="Test that the sub string 'ok' below appears ANYWHERE within the JSON return string">
        <scenario>
            <step>
                <url>http://automateyourtests.com/demo_application/?json=get_posts</url>
                <contains>ok</contains>
            </step>
        </scenario>
    </userstory>
</testsuite>

That last example is a lazy way of testing for JSON content, which often is too relaxed and results in a wrong PASS/FAILURE. Because in reality the value “ok” may be the value of other unrelated JSON nodes throughout the whole JSON object, which is not what you actually had in mind with your test assertion.

The following automation test is better. The following <xpath> expression tests whether the JSON node “status” has the value “ok”. It ignores other nodes that may also have the value “ok”, which is far more suitable for real-life testing.

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <userstory title="Test that the JSON node 'status' is equal 'ok' through an XPath expression">
        <scenario>
            <step>
                <url>http://automateyourtests.com/demo_application/?json=get_posts</url>
                <xpath>status[.='ok']</xpath>
            </step>
        </scenario>
    </userstory>
</testsuite>

The following example makes sure that not only the JSON “status” is “ok”, but also that at least one “count_total” element of JSON content was returned (logical and).

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
    <userstory title="Test a JSON response for multiple XPath expressions (logical and)">
        <scenario>
            <step>
                <url>http://automateyourtests.com/demo_application/?json=get_posts</url>
                <xpath>status[.='ok']</xpath>
                <xpath>count_total[.>0]</xpath>
            </step>
        </scenario>
    </userstory>
</testsuite>

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