Question

I'm trying to set up some functional/acceptance/integration testing using Cucumber for my PHP project. I'm trying to understand the best approach to implementing these types of tests.

I understand that Selenium can test javascript, but Selenium is slow and I don't always need to test javascript. I'm looking for a "headless browser" equivalent for PHP.

Would either of these be classified as "headless browsers?"

  • SimpleTest web testing
  • Zend_Test_PHPUnit_ControllerTestCase

What have you done to implement integration testing of your Zend Framework project?

Was it helpful?

Solution 6

First of all, you should use Capybara (a replacement for Webrat). It's used to simplify and standardize the DSL used to interact with the browser and provides some nice features.

Even though Selenium is a little slow, it's easy to use to get started since it comes bundled with Capybara. FYI: it defaults to using Firefox.

Example support/env.rb:

require 'capybara/cucumber'

Capybara.app_host = "http://your.app.com"
Capybara.default_driver = :selenium

Now that you're using Capybara, you should use capybara-webkit driver (a truly headless browser which uses Webkit behind the scenes). There's a little bit of setup involved, but once you've done that, the speed is improved from using Selenium.

Example support/env.rb:

require 'capybara/cucumber'

Capybara.app_host = "http://your.app.com"
Capybara.default_driver = :webkit

OTHER TIPS

If you set up Cucumber to use Webrat, you can set up Webrat to use Mechanize by default. Mechanize is essentially a headless browser. This is what my env.rb file looks like:

# RSpec
require 'rspec/expectations'

# Webrat
require 'webrat'

require 'test/unit/assertions'
World(Test::Unit::Assertions)

Webrat.configure do |config|
  config.mode = :mechanize
end

World do
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Matchers)
  session
end

Also, according to this article, you can set up Cucumber to use Capybara and configure it to use Celerity (a headless browser with javascript support). It also includes instructions on how to configure Capybara to use Selenium RC (which I thought was not possible). I have not attempted this approach yet, so I don't know how well it works.

Why not use behat ( http://behat.org/ ) ?

It should have all the requirements you listed above, and it's written in php.

It has a SahiDrvier to handle "in-browser" testing, and a Simple php browser too.

give a try to Codeception: http://codeception.com

UPDATE:

It's like Capybara , but with a PHP DSL. With codeception you can do something like this:

$I = new WebGuy($scenario);
$I->wantTo('create wiki page');
$I->amOnPage('/');
$I->click('Pages');
$I->click('New');
$I->see('New Page');
$I->fillField('title', 'Hobbit');
$I->fillField('body', 'By Peter Jackson');
$I->click('Save');
$I->see('page created'); // notice generated
$I->see('Hobbit','h1'); // head of page of is our title
$I->seeInCurrentUrl('pages/hobbit'); 
$I->seeInDatabase('pages', array('title' => 'Hobbit'));

You can use Selenium2 for browsing, or a PHPBrowser (headless) for better performance on js less scenarios (PHPBrowser doesn't execute javascript)

Update: It looks like Akephalos hasn't been updated in a while, so it might not be a good solution for working with a newer version of Capybara.


Use Capybara (a replacement for Webrat) and Akephalos (a headless browser). Capybara is used to interact with Akephalos.

Example support/env.rb:

# Capybara configuration (using Akephalos)
require 'capybara/cucumber'
require 'akephalos'

Capybara.default_driver = :akephalos
Capybara.app_host = 'http://your.web.app'

Capybara.register_driver :akephalos do |app|
  # available options:
  #   :ie6, :ie7, :ie8, :firefox_3, :firefox_3_6
  Capybara::Driver::Akephalos.new(app, :browser => :firefox_3_6)
end

If you're using Cucumber, are you not already using Ruby? Why not use celerity or culerity?

I've used Cucumber with Celerity to test a Struts 2 app as well as a ColdFusion 8 app. Basically, you use Celerity and JRuby (which wraps HtmlUnit) or Culerity which runs on native ruby, to drive your browser.

I suggest looking at one of these two projects to help you get started:

Cheesy UI Testing - Corresponding Blog

WatirMelon Page Objects - Corresponding Blog

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top