Working with Behat

Here in the webteam we have been moving towards including an automated test suite to every piece of work that we produce.  We have focused on 2 main toold to provide these test suites: Behat for the BDD approach. and PHPUnit for the TDD approach.

Behat is an amazing tool for working through repeatable steps in your web applications.  You define the tests in a human readable way, and they then getinterpreted and translated into the actions performed.

Behat tests

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.

There are several default step definitions that 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 definitions that don’t come free of charge, which we thought would be useful to share.

Check that the correct value in a select list is (not) selected.

Syntax: Then “an interesting value” in “my select” should be selected.

/**
 * @Then /^"([^"]*)" in "([^"]*)" should be selected$/
 */
public function inShouldBeSelected($optionValue, $select) {
    $selectElement = $this->getSession()->getPage()->find('named', array('select', "\"{$select}\""));
    $optionElement = $selectElement->find('named', array('option', "\"{$optionValue}\""));
    //it should have the attribute selected and it should be set to selected
    assertTrue($optionElement->hasAttribute("selected"));
    assertTrue($optionElement->getAttribute("selected") == "selected");
}

Syntax: Then “an interesting value” in “my select” should not be selected.

/**
 * @Then /^"([^"]*)" in "([^"]*)" should not be selected$/
 */
public function inShouldNotBeSelected($optionValue, $select) {
	$selectElement = $this->getSession()->getPage()->find('named', array('select', "\"{$select}\""));
	$optionElement = $selectElement->find('named', array('option', "\"{$optionValue}\""));
	//it should have the attribute selected and it should be set to selected
	assertFalse($optionElement->hasAttribute("selected"));
}

Check if something is (not) visible to the user by inspecting CSS values

Syntax: Then “something” should be visible

/**
 * @Then /^"([^"]*)" should be visible$/
 */
public function shouldBeVisible($selector) {
	$el = $this->getSession()->getPage()->find('css', $selector);
	$style = '';
	if(!empty($el)){
		$style = preg_replace('/\s/', '', $el->getAttribute('style'));
	} else {
		throw new Exception("Element ({$selector}) not found");
	}

	assertFalse(false !== strstr($style, 'display:none'));
}

Syntax: Then “something” should not be visible

/**
 * @Then /^"([^"]*)" should not be visible$/
 */
public function shouldNotBeVisible($selector) {
	$el = $this->getSession()->getPage()->find('css', $selector);
	$style = '';
	if(!empty($el)){
		$style = preg_replace('/\s/', '', $el->getAttribute('style'));
	} else {
		throw new Exception("Element ({$selector}) not found");
	}

	assertTrue(false !== strstr($style, 'display:none'));
}

Confirming that you have valid JSON

Syntax: Then I should see valid JSON

/**
 * @Then /^should see valid JSON$/
 */
public function shouldSeeValidJSON() {
	$json = $this->getSession()->getPage()->getContent();
	assertTrue($json !== 0 && (false !== json_decode($json)));
}

 

2 responses to “Working with Behat

  1. Just wanted to point out that Simon has included the Behat 2 way of writing step definitions

    Behat 2 has a wrapper to allow you to write step definitions in the Behat 1 way which i think are supperior for a couple of reason which I’ll try and write up soon.

Leave a Reply

Your email address will not be published.