Behat Meetup in a nutshell

17 Feb

Note : This post may not work with recent Behat. Please use and my  Github repo for to get it working without any issue https://github.com/Shashikant86/BehatDemo

 

On Feb 15, we had a great evening at Ibuildings, London office. We organized the first ever Behat meetup in the United Kingdom. A group  called “London Behat Users” organized an event to demonstrate Behavior Driven Development for the PHP developers and testers who wish to get started with Behat.

Behat is an amazing BDD framework for PHP developers which works exactly the same as Cucumber. Assuming that PHP developers and testers are new to BDD, we made the agenda ‘Behat from Scratch‘. There were about 15 behat users who attended this event.

As per the schedule, we had great Pizza before kick-off. While having drinks and Pizza, An Event organizer, Mitch asked everyone to introduced themselves and asked about their experience with Behat. All members were keen to know about other behat users and how long they have been using behat and BDD. After introductions, I explained a little bit about the ‘London Behat users’ group.

Live Coding (Developers View)

Jakub Zalas kicked it off with a talk about the developer’s view in BDD projects. He started the demonstration with a short introduction to BDD, how BDD is different from TDD and so on. Jakub covered following topics while coding:

1. Behat/Mink Installation

Jakub created composer.json file in the project root:

Then he downloaded composer.phar and ran install command which installed all the dependencies of Behat as well as Mink.

2. Creating first project

Once he was done with all the installations, he created a project called “Behat From Scratch” and initialized behat using:

$ behat --init
+d features - place your *.feature files here
+d features/bootstrap - place bootstrap scripts and static files here
+f features/bootstrap/FeatureContext.php - place your feature related code here 

The theme of the application was very simple. There was a form with two fields called ‘Title” and ‘Body” and a button called “Preview.” When a user fills in data in the ‘Title’ & ‘Body” fields and clicked on the “Preview” button, then the user should see the text that they entered.

3. Writing first Gherkin feature

Next he wrote one Gherkin feature file with couple of scenarios:

Feature: Previewing article
As a Content Manager
I want to preview an article before saving it
In order to make sure it is displayed correctly

Scenario: Article is displayed in a preview area
Given I visit "article form"
And I fill in "Hello Behat!" for "Title"
And I fill in "BDD is fun" for "Body"
When I press "Preview"
Then I should see "Hello Behat!" in the preview area
And I should see "BDD is fun" in the preview area

Scenario: Preview is not visible initially
Given I visit "article form"
Then the preview area should not be visible 

After that, he implemented Step Definitions for the integrated classes based on the Silex micro-framework, uses Twig templates and Symfony‘s form component. He implemented all the step definitions until all the steps passed.

The Source code of this is available on GitHub: Behat From Scratch

Browser Automation with Mink (Testers View)

Once Jakub finished with implementation, he handed it over to me to explain how to do browser automation using Mink. I modified the ‘config/behat.yml’ file and tagged the scenarios with ‘@javascript’ tags.

I also explained how to run feature files using different Mink drivers. Behat has provided lot of driver options for testers. It is not limited to Selenium. It has following mink drivers.

Selenium Server

You need to download the selenium server jar file and execute the following command:

$ cd /path/to/selenium-server
$java -jar selenium-server-standalone-2.15.0.jar 

Sahi Server

Download the sahi zip file from SourceForge

Now launch Sahi Server using the command below:

$ cd /path/to/sahi
$ cd userdata/bin
$bash start_sahi.sh 

There was a quick demonstration of the tests running in browsers using the Selenium 1, 2 (WebDriver), and Sahi servers. This was followed by a short question and answer session.

Try this on your own

You may be interested in trying all this stuff on your own. Jakub mentioned all of the instructions in his GitHub project. I will briefly explain the steps to follow:

$ moonstar:~ sjagtap$ git clone git://github.com/jakzal/behat-from-scratch.git
      Cloning into behat-from-scratch...
      remote: Counting objects: 123, done.
      remote: Compressing objects: 100% (59/59), done.
      remote: Total 123 (delta 47), reused 123 (delta 47)
      Receiving objects: 100% (123/123), 63.34 KiB, done.
      Resolving deltas: 100% (47/47), done.
      moonstar:~ sjagtap$ cd behat-from-scratch/ 
  • Install Behat with Composer
    Download the Composer and install Behat:
$ moonstar:behat-from-scratch sjagtap$ wget  http://getcomposer.org/composer.phar
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
       100  308k  100  308k    0     0   366k      0 --:--:-- --:--:-- --:--:--  582k

$moonstar:behat-from-scratch sjagtap$ php composer.phar install 

Here, you may face some problems while installing, depending on your network and permission issues. (There may be a bug in composer.)

I managed to install it properly and it’s up and running in my localhost. You need to cross your fingers at this point.
By default the custom  Silex micro-framework session is used so there’s no need for additional configuration. Behat will create a Silex application and simulate requests.

All the project’s scenarios can be run with:

$ moonstar:behat-from-scratch sjagtap./bin/behat
  As a Content Manager
I want to preview an article before saving it
In order to make sure it is displayed correctly

Scenario: Article form is accessible from the homepage # features/previewing-article.feature:7
Given I visit "homepage"                             # FeatureContext::iVisit()
When I follow "Add article"                          # FeatureContext::clickLink()
Then I should see "Title" input on the article form  # FeatureContext::iShouldSeeInputOnTheArticleForm()
And I should see "Body" textarea on the article form # FeatureContext::iShouldSeeTextareaOnTheArticleForm()

Scenario: Article is displayed in a preview area       # features/previewing-article.feature:14
Given I visit "article form"                         # FeatureContext::iVisit()
And I fill in "Hello Behat!" for "Title"             # FeatureContext::fillField()
And I fill in "BDD is fun" for "Body"                # FeatureContext::fillField()
When I press "Preview"                               # FeatureContext::pressButton()
Then I should see "Hello Behat!" in the preview area # FeatureContext::iShouldSeeInPreviewArea()
And I should see "BDD is fun" in the preview area    # FeatureContext::iShouldSeeInPreviewArea()

Scenario: Preview is not visible initially             # features/previewing-article.feature:23
Given I visit "article form"                         # FeatureContext::iVisit()
Then the preview area should not be visible          # FeatureContext::thePreviewAreaShouldNotBeVisible()

Scenario: Title and body are required                  # features/previewing-article.feature:28
Given I visit "article form"                         # FeatureContext::iVisit()
When I press "Preview"                               # FeatureContext::pressButton()
Then I should see "Article title is required"        # FeatureContext::assertPageContainsText()
And I should see "Article body is required"          # FeatureContext::assertPageContainsText()
And the preview area should not be visible           # FeatureContext::thePreviewAreaShouldNotBeVisible()

4 scenarios (4 passed)
17 steps (17 passed)
0m12.957s 

You don’t need to configure the web server to run scenarios using Silex driver.  But you do need to configure the web server while running tests in browser.

Configure Web Server.

Follow these steps to configure the web server.

  1. Update ‘hosts’ file and add ‘behat.dev’ and IP as follows
$moonstar:~ sjagtap$ cd /etc
$moonstar:etc sjagtap$ sudo vi hosts
# Host Database
     # localhost is used to configure the loopback interface
     # when the system is booting.  Do not change this entry.
      ##
    127.0.0.1       localhost
    127.0.0.1  behat.dev 
  • Make sure you have updated ‘/etc/apache2/httpd.conf’ to enable virtual host. Remove the comment from Virtual hosts.
$moonstar:~ sjagtap$ cd /etc/apache2/
$moonstar:apache2 sjagtap$ cat httpd.conf
 # Virtual hosts
     Include /private/etc/apache2/extra/httpd-vhosts.conf 
  • Now you need to update the ‘/etc/apache2/extra/httpd-vhosts.conf ‘ file and add the following configuration to it:
$mmoonstar:~ sjagtap$ cd /etc/apache2/extra/
$moonstar:extra sjagtap$ sudo vi httpd-vhosts.conf
     VirtualHost *:80;
ServerName behat.dev

DocumentRoot /var/www/behat.dev/web
DirectoryIndex index.php

Directory /var/www/behat.dev/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
/Directory>
     /VirtualHost>

You are done with web server configuration, and now you can run your tests through a browser.  The Paths to source code and a domain name will have to be updated. Also the base_url in  ‘config/behat.yml’ needs to be changed accordingly.

Run Tests in Browser

  • Tag all your scenarios with ‘@javascript’ tags
  • Run Selenium server as mentioned above
$ cd /path/to/selenium-server
$java -jar selenium-server-standalone-2.15.0.jar
  • Update ‘config/behat.yml’
default:
context:
parameters:
default_session: goutte
javascript_session: webdriver
browser: firefox
               base_url: http://behat.dev/
  • Run ‘./bin/behat’ from a terminal and watch results in browser.
$ moonstar:behat-from-scratch sjagtap./bin/behat
Feature: Previewing article
As a Content Manager
I want to preview an article before saving it
In order to make sure it is displayed correctly

@javascript
Scenario: Article form is accessible from the homepage # features/previewing-article.feature:7
Given I visit "homepage"                             # FeatureContext::iVisit()
When I follow "Add article"                          # FeatureContext::clickLink()
Then I should see "Title" input on the article form  # FeatureContext::iShouldSeeInputOnTheArticleForm()
And I should see "Body" textarea on the article form # FeatureContext::iShouldSeeTextareaOnTheArticleForm()

@javascript
Scenario: Article is displayed in a preview area       # features/previewing-article.feature:14
Given I visit "article form"                         # FeatureContext::iVisit()
And I fill in "Hello Behat!" for "Title"             # FeatureContext::fillField()
And I fill in "BDD is fun" for "Body"                # FeatureContext::fillField()
When I press "Preview"                               # FeatureContext::pressButton()
Then I should see "Hello Behat!" in the preview area # FeatureContext::iShouldSeeInPreviewArea()
And I should see "BDD is fun" in the preview area    # FeatureContext::iShouldSeeInPreviewArea()

@javascript
Scenario: Preview is not visible initially             # features/previewing-article.feature:23
Given I visit "article form"                         # FeatureContext::iVisit()
Then the preview area should not be visible          # FeatureContext::thePreviewAreaShouldNotBeVisible()

@javascript
Scenario: Title and body are required                  # features/previewing-article.feature:28
Given I visit "article form"                         # FeatureContext::iVisit()
When I press "Preview"                               # FeatureContext::pressButton()
Then I should see "Article title is required"        # FeatureContext::assertPageContainsText()
And I should see "Article body is required"          # FeatureContext::assertPageContainsText()
And the preview area should not be visible           # FeatureContext::thePreviewAreaShouldNotBeVisible()

4 scenarios (4 passed)
17 steps (17 passed)
0m12.957s

It’s done !!

Discussion about Behat and BDD

After we finished with the demo, we started a discussion about BDD and behat. To me, this was the most interesting part of the meetup. Lot of things about BDD, tester-developer collaboration, acceptance criteria, Jenkins and other CI systems were discussed.

Some of the highlights of the discussion are as follows:

  • Ben and Jakub Zalas  shared thoughts about test reporting with Jenkins. (using HTML reports).
  • Mitch raised points about making scenarios and running scenarios with different data sets and everybody contributed
  • Marcello talked about the role of testers and developers in BDD
  • Miles and Alex raised some good questions about web testing using Gherkin DSL.
  • John played a key role within the whole session talking about collaborative tools, challenges in BDD and some tricks about BDD.

Conclusion:

The first Behat meetup was an amazing experience for all the attendees. Everybody learned by sharing experiences and ideas. In short, all of us enjoyed our evening of knowledge sharing. We are looking forward to arrange monthly meetups to further discuss Behat and BDD.  The next meetup is tentatively scheduled for Wed Mar 21 at 7:00 PM (GMT). Click here for details.

Many Thanks to the creator of Behat: ‘Konstantin Kudryashov

 

Happy BDD !!

Advertisements

2 Responses to “Behat Meetup in a nutshell”

  1. Sylvia February 5, 2013 at 1:09 pm #

    Hello, I think your blog might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, very
    good blog!

    • shashikant86 February 5, 2013 at 1:45 pm #

      Hi Sylvia, Thanks a lot. This post is outdated as I put note at the start of the blog. Anyway I can’t assure it will work in IE 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: