{"id":139,"date":"2011-10-04T11:15:08","date_gmt":"2011-10-04T10:15:08","guid":{"rendered":"http:\/\/blogs.kent.ac.uk\/webdev\/?p=139"},"modified":"2011-10-11T09:01:16","modified_gmt":"2011-10-11T08:01:16","slug":"working-with-behat","status":"publish","type":"post","link":"https:\/\/blogs.kent.ac.uk\/webdev\/2011\/10\/04\/working-with-behat\/","title":{"rendered":"Working with Behat"},"content":{"rendered":"<p>Here in the webteam we have been moving towards including an automated test suite to every piece of work that we produce.\u00a0 We have focused on 2 main toold to provide these test suites: <a title=\"Behat\" href=\"http:\/\/behat.org\/\">Behat <\/a>for the <a title=\"Behaviour Driven Development\" href=\"http:\/\/en.wikipedia.org\/wiki\/Behavior_Driven_Development\">BDD <\/a>approach. and <a title=\"PHPUnit\" href=\"https:\/\/github.com\/sebastianbergmann\/phpunit\/\">PHPUnit <\/a>for the <a title=\"Test Driven Development\" href=\"http:\/\/en.wikipedia.org\/wiki\/Test_Driven_Development\">TDD <\/a>approach.<\/p>\n<p>Behat is an amazing tool for working through repeatable steps in your web applications.\u00a0 You define the tests in a human readable way, and they then getinterpreted and translated into the actions performed.<\/p>\n<h2>Behat tests<\/h2>\n<p>A test in behat is known as a feature, and each feature can contain multiple scenarios. In turn each scenario consists of steps. These steps are what drive the test, and they are translated into something that Behat can execute by step definitions.<\/p>\n<p>There are several default step definitions\u00a0that ship with it, especially if you use Mink, another Behat produce that will act as a browser to go get pages from your server during the tests. However there are some\u00a0definitions that\u00a0don&#8217;t come\u00a0free of charge, which we thought would be useful to share.<\/p>\n<h3>Check that the correct value in a select list is\u00a0(not)\u00a0selected.<\/h3>\n<p><strong>Syntax:<\/strong> Then &#8220;an interesting value&#8221; in &#8220;my select&#8221; should be selected.<\/p>\n<pre class=\"brush: php\">\/**\r\n * @Then \/^\"([^\"]*)\" in \"([^\"]*)\" should be selected$\/\r\n *\/\r\npublic function inShouldBeSelected($optionValue, $select) {\r\n    $selectElement = $this-&gt;getSession()-&gt;getPage()-&gt;find('named', array('select', \"\\\"{$select}\\\"\"));\r\n    $optionElement = $selectElement-&gt;find('named', array('option', \"\\\"{$optionValue}\\\"\"));\r\n    \/\/it should have the attribute selected and it should be set to selected\r\n    assertTrue($optionElement-&gt;hasAttribute(\"selected\"));\r\n    assertTrue($optionElement-&gt;getAttribute(\"selected\") == \"selected\");\r\n}<\/pre>\n<p><strong>Syntax:<\/strong> Then &#8220;an interesting value&#8221; in &#8220;my select&#8221; should not\u00a0be selected.<\/p>\n<pre class=\"brush: php\">\/**\r\n * @Then \/^\"([^\"]*)\" in \"([^\"]*)\" should not be selected$\/\r\n *\/\r\npublic function inShouldNotBeSelected($optionValue, $select) {\r\n\t$selectElement = $this-&gt;getSession()-&gt;getPage()-&gt;find('named', array('select', \"\\\"{$select}\\\"\"));\r\n\t$optionElement = $selectElement-&gt;find('named', array('option', \"\\\"{$optionValue}\\\"\"));\r\n\t\/\/it should have the attribute selected and it should be set to selected\r\n\tassertFalse($optionElement-&gt;hasAttribute(\"selected\"));\r\n}<\/pre>\n<h3>Check if something is (not)\u00a0visible to the user by inspecting CSS values<\/h3>\n<p><strong>Syntax<\/strong>: Then &#8220;something&#8221; should be visible<\/p>\n<pre class=\"brush: php\">\/**\r\n * @Then \/^\"([^\"]*)\" should be visible$\/\r\n *\/\r\npublic function shouldBeVisible($selector) {\r\n\t$el = $this-&gt;getSession()-&gt;getPage()-&gt;find('css', $selector);\r\n\t$style = '';\r\n\tif(!empty($el)){\r\n\t\t$style = preg_replace('\/\\s\/', '', $el-&gt;getAttribute('style'));\r\n\t} else {\r\n\t\tthrow new Exception(\"Element ({$selector}) not found\");\r\n\t}\r\n\r\n\tassertFalse(false !== strstr($style, 'display:none'));\r\n}<\/pre>\n<p><strong>Syntax<\/strong>: Then &#8220;something&#8221; should not be visible<\/p>\n<pre class=\"brush: php\">\/**\r\n * @Then \/^\"([^\"]*)\" should not be visible$\/\r\n *\/\r\npublic function shouldNotBeVisible($selector) {\r\n\t$el = $this-&gt;getSession()-&gt;getPage()-&gt;find('css', $selector);\r\n\t$style = '';\r\n\tif(!empty($el)){\r\n\t\t$style = preg_replace('\/\\s\/', '', $el-&gt;getAttribute('style'));\r\n\t} else {\r\n\t\tthrow new Exception(\"Element ({$selector}) not found\");\r\n\t}\r\n\r\n\tassertTrue(false !== strstr($style, 'display:none'));\r\n}<\/pre>\n<h3>Confirming that you have valid JSON<\/h3>\n<p><strong>Syntax<\/strong>: Then I should see valid JSON<\/p>\n<pre class=\"brush: php\">\/**\r\n * @Then \/^should see valid JSON$\/\r\n *\/\r\npublic function shouldSeeValidJSON() {\r\n\t$json = $this-&gt;getSession()-&gt;getPage()-&gt;getContent();\r\n\tassertTrue($json !== 0 &amp;&amp; (false !== json_decode($json)));\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here in the webteam we have been moving towards including an automated test suite to every piece of work that we produce.\u00a0 We have focused &hellip; <a href=\"https:\/\/blogs.kent.ac.uk\/webdev\/2011\/10\/04\/working-with-behat\/\">Read&nbsp;more<\/a><\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1464,8940],"tags":[442,8960,8953,8944,8965],"_links":{"self":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/139"}],"collection":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":12,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/139\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}