If you’re developing a web application, especially one that uses PHP, you should know about Behat.
Behat introduces itself as “a php framework for testing your business expectations”. And it does exactly that. You write down your business expectations of the application, and it automatically tests whether or not your application achieves them.
You begin every feature description with a three liner following the form:
Feature: Foo bar
In order to … (achieve what goal?)
As a … (what target audience?)
I need to … (use what feature?)
This is then split up into individual scenarios of using this feature, all of which are described using natural English following the Gherkin syntax. It then uses Mink which is a browser abstraction layer to run these tests.
I’ve been enjoying Behat for quite some time now, and I’ve noticed certain tests I need to write that come up again and again which aren’t included in the default Mink definitions.
@mink:sahi, and now we can use the following definition:
* @Then /^"([^"]*)" should be visible$/
public function shouldBeVisible($selector)
$element = $this->getSession()->getPage()->find(‘css’, $selector);
throw new Exception(‘Element "’.$selector.’" not found’);
$display = $this->getSession()->evaluateScript( 'jQuery("'.$selector.'").css("display")' ); if ($display === 'none') throw new Exception('Element "'.$selector.'" is not visible'); }
… so you can now write …
Then "div" should be visible
Worth highlighting is the
->evaluateScript() function that is being called. This means that anything you can check with JQuery can be tested. This is pretty much everything.
Another useful query is dealing with images. Modern web apps have to handle image uploading quite a bit, and often this comes with resizing or cropping (for avatars, keeping to layout widths, thumbnails). Wouldn’t it be great if you could just write…
Given I have an image with width "500" and height "400" in "/tmp/foo.png"
Then the "img" element should display "/tmp/foo.png"
And the "img" element should be "500" by "400" pixels