Archive | Uncategorized RSS feed for this section
Link

London JenkinsCI User Meetup.

23 Dec

London JenkinsCI User Meetup.

Advertisements

Automating Web Performance Data Collection with Behat and BrowserMob Proxy

30 Oct

BrowserMob Proxy is a utility which is used for capuring HTTP traffic and performance data from the browser. BrowserMob-Proxy adds in essential missing capabilities such as checking HTTP status codes and injecting headers for HTTP Basic Auth. Web Perfomance data can be manually captured by other tools like Firebug or Developers Tools. Using BrowserMob Proxy we can capture perfonace data in HAR format while running automated tests. There is lots of food to learn about BrowserMob on thier official website.

In this article, we will see how to integrate BrowserMob Proxy with Behat. In order to get started we need to install PHPBrowserMob package.

PHPBrowserMob

$ sudo pear channel-discover element-34.github.com/pear
$ sudo pear install -f element-34/PHPBrowserMobProxy
$ sudo pear install -f element-34/Requests

Download latest version of the BrowserMob Proxy from the site. You can launch BrowserMob proxy like this

$ cd browsermob-proxy-2.0-beta-6/
$ cd bin
$ sh browsermob-proxy -port 9090

You will see something like this:

$ sh browsermob-proxy -port 9090
INFO 10/30 21:29:50 o.b.p.Main           - Starting BrowserMob Proxy version 2.0-beta-6
INFO 10/30 21:29:51 o.e.j.u.log          - jetty-7.3.0.v20110203
INFO 10/30 21:29:51 o.e.j.u.log          - started o.e.j.s.ServletContextHandler{/,null}
INFO 10/30 21:29:52 o.e.j.u.log          - Started SelectChannelConnector@0.0.0.0:9090

This means, BrowserMob Proxy is running correcrtly.

Download and start selenium version 2.25.0. Note that, proxy won’t work for some version of selenium server. Better Download latest.

$ java -jar selenium-server-standalone-2.25.0.jar

Now, We need to install Behat by following official doc Or just follow my previous post on Behat installtion with composer.

$ sudo mkdir Behat-BrowserMob
$ sudo chmod -R 777 Behat-BrowserMob
$ cd Behat-BrowserMob/
$ behat --init

Now create a feature file for exporting the performance data in the HAR format.

$ sudo vim BehatBrowserMob.feature
 Feature: BrowserMob Proxy with Behat
In order to check website performance
As a automated tester
I need to see network traffic captured in the HAR format

@javascript
Scenario: Behat Bowsermob
Given I setup BrowserMobProxy
And I Navigate to "http://www.facebook.com/"
When I export HAR
Then I should see network traffic in the HAR file

You need to include ‘PHPWebDriver‘ and ‘PHPBrowserMobProxy
We can implement the feature using following code :

<?php

use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Context\BehatContext,
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;

require_once __DIR__ . '/PHPBrowserMobProxy/Client.php';
require_once __DIR__ . '/php-webdriver/PHPWebDriver/WebDriver.php';
require_once __DIR__ . '/php-webdriver/PHPWebDriver/WebDriverProxy.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
require_once 'PHPUnit/Autoload.php';

/**
* Features context.
*/
class FeatureContext extends BehatContext {

protected static $driver;
protected static $BrowserMob;
protected static $BrowserMobSession;
public $data;

/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters) {

}

/**
* @BeforeScenario
*/
public function cleanup() {
$HARFile = "/tmp/BROWSERMOBHAR.php";
if (file_exists($HARFile)) {
echo "The file $HARFile exists, I am going to delete it \n";
unlink($HARFile);
}
}

/**
* @Given /^I setup BrowserMobProxy$/
*/
public function iSetupBrowsermobproxy() {
self::$driver = new PHPWebDriver_WebDriver();
self::$BrowserMob = new PHPBrowserMobProxy_Client("localhost:9090");
$additional_capabilities = array();
$webDriverProxy = new PHPWebDriver_WebDriverProxy();
self::$BrowserMob->new_har("google");
$webDriverProxy->httpProxy = self::$BrowserMob->url;
$webDriverProxy->add_to_capabilities($additional_capabilities);
$this->session = self::$driver->session('firefox', $additional_capabilities);
}

/**
* @Given /^I Navigate to "([^"]*)"$/
*/
public function iNavigateTo($url) {

$this->session->open($url);
}

/**
* @When /^I export HAR$/
*/
public function iExportHar() {
file_put_contents("/tmp/BROWSERMOBHAR.php", var_export(self::$BrowserMob->har, true));
}

/**
* @Then /^I should see network traffic in the HAR file$/
*/
public function iShouldSeeNetworkTrafficInTheHarFile() {
assertFileExists('/tmp/BROWSERMOBHAR.php');
}

}

Now, run ‘behat’ and watch the test running in the Browser. You will find data stored in the ‘/tmp/BROWSERMOBHAR’ file. You can use HAR viewer to see performance data in fancy way !

There are number of possibilities you can use this data. Here is best article which describe use of HAR file.

  • Calculate Load time.
  • Set/Get HTTP Headers during your tests
  • Capture performance data with HAR files.
  • Simulate network traffic and latency
  • Rewrite HTTP requests and respones

SourcCode on GitHub:

Source Code is available on GitHub repo ‘Behat-BrowserMobProxy‘ . Just give it a try !

Final Thoughts:

Combination of Behat, WebDriver and BrowserMob Proxy can be used for capturing the network trafic while running automated tests. We can use HAR files as per our need eg. Calculate Page load times, Page performance analysis etc etc..

Protected: Private

20 Oct

This content is password protected. To view it please enter your password below:

Behat2.4 : Painless installation with Composer

31 Aug

Behat is a BDD framework for PHP application. Behat has becoming so popular in recent days. Behat has drastic changes in recent days in order to add some cool features into it.

One of the amazing feature is, behat installation with use of Composer. Composer is dependency manager for PHP application which helps us to manage all the third party dependencies at one place and use them properly. Another great feature behat added is ‘Behat Extensions’. You can extend behat by adding extensions of your choice.

In this tutorial, we will see how to upgrade Behat vesion with use of composer . Most of us have been installed Behat with ‘pear’ packages. behat version installed with pear has no longer supported. Read Behat installation guide for further information.

Now, get ready to uninstall old version of behat and install new one. Open your terminal and type in following comands.

Check your existing installation,

Shashi-MacBook-Pro:~ user$ which behat

You will see something like this, if you have installed Behat with pear

Shashi-MacBook-Pro:~ user$ which behat
/usr/local/pear/bin/behat

Remove this Behat vesion by typing

Shashi-MacBook-Pro:~ user$ sudo pear uninstall behat/behat
Password:
uninstall ok: channel://pear.behat.org/behat-2.3.5

Remove ‘Gherkin’ package

Shashi-MacBook-Pro:~ user$ sudo pear uninstall behat/gherkin
uninstall ok: channel://pear.behat.org/gherkin-2.1.1

Remove ‘Mink‘ as we are going to use ‘MinkExtension’


Shashi-MacBook-Pro:~ user$ sudo pear uninstall behat/mink
uninstall ok: channel://pear.behat.org/mink-1.3.3

MinkExtension creates Mink instance in each sub-context or it could be even used as subcontext on its own.
You can read more about it on MinkExtension documentation.

Behat 2.4 installation

As we have just removed old version of behat, so we won’t find anything in binary when you execute ‘behat’ command

Shashi-MacBook-Pro:~ user$ behat
-bash: /usr/local/pear/bin/behat: No such file or directory

Now create new Behat installation Direcory, I have created in /opt/behat like this

Shashi-MacBook-Pro:~ user$ sudo mkdir /opt/behat

Shashi-MacBook-Pro:~ user$ cd /opt/behat/
Shashi-MacBook-Pro:behat user$ sudo chmod 777 /opt/behat/

Create ‘composer.json’ file with all the required dependencies like this

Shashi-MacBook-Pro:behat user$ sudo vi composer.json

Insert following dependencies in the json file, you can add more

{
"require": {
"behat/behat": "2.4.*@stable",
"behat/mink": "1.4@stable",
"behat/mink-goutte-driver": "*",
"behat/mink-selenium-driver": "*",
"behat/mink-selenium2-driver": "*",
"behat/mink-extension": "*",
"behat/mink-sahi-driver": "*"

},
"minimum-stability": "dev",
"config": {
"bin-dir": "bin/"
}
}

Note that we have included ‘Mink Extension’ as well in the dependencies. Now download the composer and intstall dependencies.

Shashi-MacBook-Pro:behat user$ ls
composer.json

 

Shashi-MacBook-Pro:behat user$ curl http://getcomposer.org/installer | php
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100 11038    0 11038    0     0  88692      0 --:--:-- --:--:-- --:--:--  307k
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /opt/behat/composer.phar
Use it: php composer.phar

This will download ‘composer.phar’ file. Check it

Shashi-MacBook-Pro:behat user$ ls
composer.json    composer.phar

Now install it using


php composer.phar install

This will download all the dependencies. You can see it in the ‘Behat’ directory.

Shashi-MacBook-Pro:behat user$ ls
bin        composer.json    composer.lock    composer.phar    vendor

Now, you are ready to run Behat using ‘bin/behat’ command.

Shashi-MacBook-Pro:behat user$ ./bin/behat

You will see something like this in the output. This means you have installed behat successfully !!

Shashi-MacBook-Pro:behat user$ ./bin/behat

[RuntimeException]
Context class not found.
Maybe you have provided wrong or no `bootstrap` path in your behat.yml:
http://docs.behat.org/guides/7.config.html#paths

behat [--init] [-f|--format="..."] [--out="..."] [--lang="..."] [--[no-]ansi] [--[no-]time] [--[no-]paths] [--[no-]snippets] [--[no-]snippets-paths] [--[no-]multiline] [--[no-]expand] [--story-syntax] [-d|--definitions="..."] [--name="..."] [--tags="..."] [--cache="..."] [--strict] [--dry-run] [--rerun="..."] [--append-snippets] [features]

Check Behat version


Shashi-MacBook-Pro:behat user$ ./bin/behat --version
Behat version DEV

Make Behat Global

Now Create a global symlink to access behat from anywhere

Shashi-MacBook-Pro:behat user$ sudo ln -s /opt/behat/bin/behat /usr/local/bin/behat

You can run behat from anywhere. It completes Behat2.4 installation process.

Note: You may need to restart your terminal session but not nessessarily.

Shashi-MacBook-Pro:bin user$ which behat
/usr/local/bin/behat

Enjoy New Behat !! 

Behat-SauceLabs Integration on GitHub

17 Mar

Behat is a new generation Behavior Driven Development tool for PHP applications. Behat is inspired by Ruby’s Cucumber. Behat can be used for web acceptance testing by using Mink. Mink uses browser emulators like Selenium, Sahi, WebDriver to launch browser.

SauceLabs is cloud testing service which allow selenium based tests into cloud machine with different browser combination. Create an account with Saucelabs and you will definitely love it

To install and setup Behat project, read SauceLabs blog ‘Adding Sauce To Behat‘.

Let’s directly move on to code. I assume you have installed Behat and Mink as per the blog ‘Adding Sauce To Behat’.

You can use code available on GitHub project ‘Behat-Sauce‘ directly into your project.

Requirement:

*Behat/Mink Installed

*Ant

*SauceLabs Account (Username and API Key)

*Jenkins (optional)

How to use?

* Clone the repository from my GitHub

   
$ git clone git@github.com:Shashi-ibuildings/Behat-Sauce.git
$ cd /path to/Behat-Sauce 

* Edit ‘Sauce.yml’ file in order to specify your username and API key.

* Now, Run ANT command to execute feature on sauce Labs

   
$ ant runSauce 

* Once All tests executed see the reports generated in ‘report/report.html’ file.

*  Use it in your project by updating ‘behat.yml’ and ‘sauce.yml’. Change Base url to your application url.

* Write and implement features in ‘features’ directory.

You can setup your Jenkins Continuous Integration server by following the blog post ‘Adding Sauce To Behat’

Don’t invest your time/money and effort in setting up different machine/browser combinations for cross browser testing. Better to use SauceLabs.

Any questions? Let me know.