Did you miss Part 1?
Behat is a tool you can use to automate testing of your website with behaviour driven tests.
Sample FeatureContext.php
The FeatureContext.php is the file holding your custom test commands. In your file structure, it is at D:\Behat\features\bootstrap\FeatureContext.php.
This is what mine looks like. Including in full because this was one piece of the puzzle that caused stress for me. For a vanilla file, remove all functions after __construct().
<?php
use Behat\Behat\Context\Context;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements Context
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
/**
* @When I wait for :text to appear
* @Then I should see :text appear
* @param $text
* @throws \Exception
*/
public function iWaitForTextToAppear($text)
{
$this->spin(function(FeatureContext $context) use ($text) {
try {
$context->assertPageContainsText($text);
return true;
}
catch(ResponseTextException $e) {
// NOOP
}
return false;
});
}
/**
* @When I wait for :text to disappear
* @Then I should see :text disappear
* @param $text
* @throws \Exception
*/
public function iWaitForTextToDisappear($text)
{
$this->spin(function(FeatureContext $context) use ($text) {
try {
$context->assertPageContainsText($text);
}
catch(ResponseTextException $e) {
return true;
}
return false;
});
}
/**
* Based on Behat's own example
* @see https://docs.behat.org/en/v2.5/cookbook/using_spin_functions.html#adding-a-timeout
* @param $lambda
* @param int $wait
* @throws \Exception
*/
public function spin($lambda, $wait = 30)
{
$time = time();
$stopTime = $time + $wait;
while (time() < $stopTime)
{
try {
if ($lambda($this)) {
return;
}
} catch (\Exception $e) {
// do nothing
}
usleep(5000);
}
throw new \Exception("Spin function timed out after {$wait} seconds");
}
/**
* @Then page should have body class :text
* @param $text
* @throws \Exception
*/
public function pageShouldHaveBodyClass($text)
{
$session = $this->getSession();
$page = $session->getPage();
$el = $page->find('css', 'body');
try {
$outer = $el->getOuterHtml();
if ($el->hasClass($text)) {
return;
}
} catch (\Exception $e) {
// do nothing
}
throw new \Exception ("Body does not have {$text} class.");
}
}
Sample Feature File
Your feature file holds your tests. Best practice is to have one feature file per feature of your site. e.g. recipes.feature
If you put @javascript on the first line, Behat will attempt to run the tests with Selenium -> Firefox. Otherwise Behat will run the tests with Goutte. Note: Goutte runs tests MUCH faster than Firefox does. Don’t be put off by the weird name. Give Goutte a try.
@recipes
Feature:
Publish my best recipes
@cake
Scenario: Cake main page
Given I go to "https://mysite/cake"
Then I should see "Torte"
And I should see "Muffin"
And I should see "Sponge"
Don’t forget, you can type “behat -dl” at the command line to see all the commands that are enabled by Mink Extension.
Run your tests
From the command line at D:\Behat, all you need to do is type “behat” and all tests in your feature files will run.
Use
--tag
to limit which feature files run. e.g.
behat --tags=recipes
combined with @recipes at the start of the feature file will select those tests only.
Let me know if this helped you test with Behat in the comments below.
Hi Melissa,
thx for sharing your knowledge. I got it running in Windows 10 now, but unfortunately there were still problems at the end when running a test:
“When I go to “” # FeatureContext::visit()
Mink instance has not been set on Mink context class. Have you enabled the Mink Extension? (RuntimeException)”
I have enabled this extension for sure. I now created a linux VM, set up everything and there it was working. probably Windows is not made for this.
Anyway cheers and thx again!
max
I think it’s probably the behat.yml file. Did you just copy and paste it? Like I said in the post, it was a pain to get that right.
Thanks for a great introduction to behat!
I started learning about this tool last week and this is the best startup guide I have come across.
Since I don’t have a mac and use windows 10 this was very helpful indeed.
Best Regards,
Marcus
Stockholm
Sweden
Thanks Marcus! It took a lot of experimenting to get it going!
Hi again, have you ever tried to run behat in Jenkins by any chance?
I tried to run this bat-file which works perfect from cmd-terminal:
“@ECHO OFF
ECHO Running behat tests..
call C:\Behat\bin\behat –config behat.yml
PAUSE”
But in Jenkins I get the following error:
C:\Program Files (x86)\Jenkins\workspace\behat testing>C:\Behat\testing.bat
Running behat tests..
In Application.php line 116:
The requested config file does not exist
Maybe I should convert it to a JUnit scenario instead?
Best Regards,
Marcus
Sorry Marcus. No experience with Jenkins.