EzDevInfo.com

Behat

BDD framework for PHP 5.3+ Behat Documentation — Behat 2.5.3 documentation

Behat over Cucumber in PHP

Don't get me wrong. I think both projects are fantastic.

But as both php and ruby developer I wonder if there are there any compelling reasons, besides possible language barrier, why would one choose Behat over Cucumber (with cuke4php) for BDD even when working with php or some php framework.


Source: (StackOverflow)

PHPUnit & Behat; complementing or alternatives?

I have been looking around SO and Google, but I couldn't really get a definitive answer.

PHPUnit is a framework for unittesting, like JUnit. I use it, also in combination with the Selenium-extension for functional testing. When browsing around I see Behat/Mink keeps on getting mentioned. But I do not completely understand how Behat fits in here.

With Behat you write scenarios in human-readable format. Behat can then translate that into skeleton classes for a new project? But does it also provide skeleton classes for unittesting? Do you write unittests using Behat, or you would use PHPUnit / SimpleTest for those?

But then Behat/Mink does replace PHPUnit_Selenium-extension for functional testing?

Do you use Behat only for new projects, or can it also be adapted to existing projects?


Source: (StackOverflow)

Advertisements

Zend Framework integration with Behat BDD

Anyone had used Behat with Zend Framework? Any examples on how to use both?


Source: (StackOverflow)

Mink doesn't work with behat 3.0.12

I installed Behat, Mink and a few other related packages. Here is my composer.json file:

  "require":{
    //...
    "behat/behat": "~3.0.6",
    "behat/symfony2-extension": "dev-master",
    "behat/mink": "dev-master",
    "behat/mink-browserkit-driver": "dev-master",
    "behat/mink-goutte-driver": "dev-master",
    "behat/mink-selenium2-driver": "dev-master",
    "phpunit/php-code-coverage": "dev-master",
    "phpunit/phpunit-mock-objects": "dev-master",
    "phpunit/phpunit": "dev-master"
   }

And here is my behat.yml file:

default:
  extensions:
    Behat\Symfony2Extension:
      mink_driver: true
      kernel:
        env: test
        debug: true
    Behat\MinkExtension\Extension:
        base_url: 'http://localhost/app_test.php/'
        #javascript_session: sahi
        browser_name: chrome
        sahi:
        goutte: ~
        selenium2: ~
paths:
    features: features
    bootstrap: %behat.paths.features%/Context

Now when I run behat I get following error: [Behat\Testwork\ServiceContainer\Exception\ExtensionInitializationException]
Behat\MinkExtension\Extension extension file or class could not be located.

Does anyone know how to fix this? Thanks in advance.

SOLVED:

I simply forgot to add this line:

"require": {
//...
"behat/mink-extension": "dev-master",
//... }

and in your behat.yml: comment this:

# mink_driver: true

and change this:

Behat\MinkExtension\Extension:

to this:

Behat\MinkExtension:

Source: (StackOverflow)

Step definitions in external files in Behat

Behat by default looks for the step definitions in file named FeatureContext (all steps in one file).
Having a lot of steps, it's hard to maintain such a big file.

I'd like to have one definition file per feature file.

How can I have step definitions in external files?

e.g.

homepage.feature
HomepageContext extends FeatureContext

Source: (StackOverflow)

Behat authenticate Symfony2 user

I'm using Behat in Symfony2 / Doctrine2. Now, I have this scenario that boils down to the fact that "if I'm logged in and I go to /login, I shoud go to / instead":

@login
Scenario: Go to the login page while being logged in
  Given I am logged in
  When I go to "/login"
  Then I should be on "/"

For the @login, I created the following:

/**
 * @BeforeScenario @login
 */
public function loginUser()
{
    $doctrine = $this->getContainer()->get('doctrine');
    $userRepository = $doctrine->getRepository('MyTestBundle:User');
    $user = $userRepository->find(1); // 1 = id

    $token = new UsernamePasswordToken($user, NULL, 'main', $user->getRoles());
    $this->getContainer()->get('security.context')->setToken($token);
}

In the "when I go to /login" code (the controller gets called), the token seems gone (not what I intended):

/**
 * @Route("/login", name="login")
 */
public function loginAction()
{
    $token = $this->get('security.context')->getToken();
    $fd = fopen('/tmp/debug.log', 'a');
    fwrite($fd, $token);

    // prints 'AnonymousToken(user="anon.", authenticated=true, roles="")'
    ...

But in the FeatureContext, it seems to stick around (the way I hoped it would work). In the "Given I am logged in":

/**
 * @Given /^I am logged in$/
 */
public function iAmLoggedIn()
{        
    $token = $this->getContainer()->get('security.context')->getToken();
    $fd = fopen('/tmp/debug.log', 'a');
    fwrite($fd, $token);

    // prints 'UsernamePasswordToken(user="admin", authenticated=true, roles="ROLE_ADMIN")'
    ...

I run behat like this:

app/console -e=test behat

I also did this in the controller to be sure it's test:

fwrite($fd, $this->get('kernel')->getEnvironment());
// prints 'test'

Any clue how to authenticate a user? I will have to test a lot of admin pages, so it would be nice if I could hook the login into @BeforeSuite, @BeforeFeature (or @BeforeScenario ...) so that I don't get blocked.

(Suggestions on disabling the authentication mechanism for testing, or a way to stub/mock a user are also welcome.)


Source: (StackOverflow)

How to integrate Behat with PHPStorm / other IDE

Behat is the leading BDD framework for PHP. I use PHPStorm and want to integrate Behat into the IDE as an external tool.

Question: How should I set up Behat as an external tool (ie, output filters, macros, etc) in PHPStorm or any other IDE for that matter?

note: PHPUnit has deprecated their BDD support and will remove it in 3.6 (in deference to Behat), so I believe the integrated PHPUnit testing in PHPStorm isn't ideal for Behavior Driven Development.

update Feb 8-2012: PHPStorm's roadmap indicates Behat syntax will be integrated in the 4.0 release which is due Q1 2012! What can I say - PHPStorm rocks.


Source: (StackOverflow)

Struggling to get Mink working with Behat

I've been following this guide (and installed everything through composer): http://docs.behat.org/cookbook/behat_and_mink.html and am attempting to get Behat + Mink working but everytime I try and run bin/behat I get the following error:

PHP Fatal error:  Call to a member function getSession() on a non-object in vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 80

That line of code is:

return $this->getMink()->getSession($name);

So for some reason the mink attribute is empty but I've no idea why.

My .feature file is exactly the same as the one in the guide, the FeatureContext class is also from the guide:

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;

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

}

and my vendor/behat/mink/behat.yml file contains:

context:
  extensions:
    Behat\MinkExtension\Extension:
      base_url:  'http://en.wikipedia.org/'
      goutte:    ~
      selenium2: ~

I've also tried making my class extend BehatContext and then call useContext but that gives me the same error. Behat itself seems to work it's just anything with Mink produces that fatal error and I've no idea how to fix it.


Source: (StackOverflow)

Travis-CI + Sauce Connect + Behat: Unable to get Internet Explorer to run tests

I have Travis-CI running Sauce Connect to run Behat tests. Everything works just fine if I tell Sauce to use Firefox 26 on Windows 7. But if I change the browser to Internet Explorer (any of the three versions that Sauce Labs makes available on Windows 7--that is, IE8, IE9, and IE10), then it doesn't work.

On the Sauce page that shows the IE browser test, it shows a long video of it displaying nothing other than This is the initial start page for the WebDriver server. The error message shown at the top of the page that shows that browser screenshot is: Test did not see a new command for 90 seconds. Timing out. However, the screencast is over 13 minutes long so it was at least receiving some commands even if it was never acting on them.

Meanwhile, on the Travis side, I'm seeing this:

2014-02-18 04:34:13,124 - Request started: GET http://ctldl.windowsupdate.com/msdownload/update/v3/static/trustedr/en/disallowedcertstl.cab?f20efc77fc170e42
2014-02-18 04:34:13,211 - GET http://ctldl.windowsupdate.com/msdownload/update/v3/static/trustedr/en/disallowedcertstl.cab?f20efc77fc170e42 -> 200 (88ms, 6356 bytes)
2014-02-18 04:34:13,417 - Request started: GET https://ieonline.microsoft.com/iedomainsuggestions/ie10/201402/suggestions.en-US
2014-02-18 04:34:13,503 - GET https://ieonline.microsoft.com/iedomainsuggestions/ie10/201402/suggestions.en-US -> 200 (87ms, 18176 bytes)
No output has been received in the last 10 minutes, this potentially indicates a stalled build or something wrong with the build itself.
The build has been terminated

I did find an entry in the Sauce Labs support docs in indicating that this can be caused by unconventional ports, but I'm running my app over HTTPS on port 443, so that wouldn't seem to be the issue.

Here's my Behat YAML config file for running Internet Explorer 9 via Sauce:

# Use this profile to run tests on Sauce against the Travis-CI instance
default:
    context:
        class: "FeatureContext"
    extensions:
        Behat\MinkExtension\Extension:
            base_url: https://localhost
            default_session: saucelabs
            javascript_session: saucelabs
            saucelabs:
                browser: "internet explorer"
                capabilities:
                    platform: "Windows 7"
                    version: 9

I am running Behat 2.5.2, although I had the same issue with 2.4.x.

I'm not sure where to go or what to do from here. What should my next step be?


Source: (StackOverflow)

It is possibile to exclude a tag with Behat?

I know the way to run only the tests tagged with a chosen @tag:

@invite
Feature: As User I want to invite a friend to join on MySocial

  @mytag
  Scenario: Exists a Facebook user
    Given I go to "/"
    When I follow "Invite a friend"
    ...

Is is possible to do exactly the opposite?


Source: (StackOverflow)

how to activate mink in behat

I'm trying to use behat and mink together, reading that link:

http://docs.behat.org/cookbook/behat_and_mink.html#method-1-composer

and trying to activate Mink in Behat framework, but it does not work for me :(

here is text form manual

    And this executable will already autoload all the needed classes in order to activate MinkExtension through behat.yml.

Now lets activate it:

I'm thinking he is talking about /vendor/behat/mink-extension/behat.yml ?

I had added these lines into that file

# behat.yml
default:
    extensions:
        Behat\MinkExtension\Extension:
            goutte: ~
            selenium2: ~

But when i'm doing $bin/behat -dl i can see only

Given /^I am in a directory "([^"]*)"$/
Given /^I have a file named "([^"]*)"$/
 When /^I run "([^"]*)"$/
 Then /^I should get:$/

Seems mink-extension do not activated...but how i can activate it, if i did everything what is written in the manual :(

P.S. I just tried to follow instruction (from here http://docs.behat.org/cookbook/behat_and_mink.html) in totally new clean place (new folder) but it does not work it shows me next error

bin/behat -dl

  [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                         

But in tutorial nothing says about paths and yml modifications :( Maybe is there exist any updated tutorial version ?

based on error message I have to make some php file in bootstrap folder, but it was not describe in tutorial :( strange

SOLVED:

$ mkdir behat_mink_test && cd behat_mink_test
$ touch composer.json
$ echo '{
>     "require": {
>         "behat/behat": "2.4.*@stable",
>         "behat/mink": "1.4.*@stable",
>         "behat/mink-extension": "*",
>         "behat/mink-goutte-driver": "*",
>         "behat/mink-selenium2-driver": "*"
>     },
>     "minimum-stability": "dev",
>     "config": {
>         "bin-dir": "bin/"
>     }
> }' > composer.json
$ curl http://getcomposer.org/installer | php
$ php composer.phar install
$ bin/behat -h
$ touch behat.yml
$ echo 'default:
>     extensions:
>         Behat\MinkExtension\Extension:
>             goutte: ~
>             selenium2: ~' > behat.yml
$ bin/behat -dl

and finally it works now :) thanks


Source: (StackOverflow)

How to make Behat wait for an AJAX call?

Scenario: Modify and save an incomplete change to a Campaign

Given I click on the Campaign section folder
And I press Save in the selected Campaign
Then I should see an error balloon informing the changes cannot be saved

Point is that this 'error balloon' in the final step is a ajax call which will then bring a green or red balloon according to the success of the operation. Currently what I do is after 'And I press Save...' I will do a sleep(3) to give it time for this balloon to show up. This doesn't seem very smart coz you are wasting time and also because some times it can take more or less time for this call to be processed.

How do you guys make your behat tests wait for Ajax do be done instead of just putting the beasts to sleep?

thank you very much for any feedback!


Source: (StackOverflow)

Mocking The Time used by all instances of DateTime for testing purposes.

I'd like to be able to set the time for every instance of DateTime instantiated for the duration of a PHPUnit or Behat Test.

I'm testing business logic relating to time. For example that a method in a class only returns events in the past or future.

Thing's I dont want to do if possible:

1) Write a wrapper around DateTime and use this instead of DateTime throughout my code. This would involve a bit of a re-write of my current code base.

2) Dynamically generate a dataset each time the test / suite is run.

So the question is: Is it possible to override DateTimes behaviour to always supply a specific time when requested?


Source: (StackOverflow)

How to find a text node element with Mink?

I was wondering, i have this HTML :

<li>
  <span class="jqTransformRadioWrapper">
    <a rel="choices[choices]" class="jqTransformRadio jqTransformChecked" href = "#"></a>
    <input type="radio" id="choices_choices_5" value="5" name="choices[choices]" class="jqTransformHidden">
  </span>
  <label for = "choices_choices_5" style = "cursor: pointer;">My awesome test</label>
</li>

Some of you might recognize that the input has been jqTransformed

I was wondering how to click on the label named "My awesome test".

Right now, i do :

   $el = $this->getSession()->getPage()->find('css', 'ul li span.jqTransformRadioWrapper a');
   $el->click();

But it selects the first element. And i want to select them with their Name (and only) for this example it would be "My awesome test".

Thanks


Source: (StackOverflow)

Can't upload a file with Sahi / Mink / Behat in a Symfony2 application

I am using Mink and Sahi for my user interface tests inside a Symfony2 application. But actually I can't manage to upload a file with Sahi.

My Sahi server is up and running:

[09:51:33] coil@ubuntu:~/Webdev/sahi/bin$ ./sahi.sh 
--------
SAHI_HOME: ..
SAHI_USERDATA_DIR: ../userdata
SAHI_EXT_CLASS_PATH:
--------
Sahi properties file = /home/coil/Webdev/sahi/config/sahi.properties
Sahi user properties file = /home/coil/Webdev/sahi/userdata/config/userdata.properties
Added shutdown hook.
>>>> Sahi started. Listening on port: 9999
>>>> Configure your browser to use this server and port as its proxy
>>>> Browse any page and CTRL-ALT-DblClick on the page to bring up the Sahi Controller
-----
Reading browser types from: /home/coil/Webdev/sahi/userdata/config/browser_types.xml
-----

My step implementation:

    // $element->getXpath() --> (//html/descendant-or-self::*[@id = 'attachment'])[1]
$element->attachFile($file);

Note here that if I use a file that is not /home/coil/Webdev/sahi/userdata directory, I get the following error:

$element->attachFile('toto');
error:_setFile2(_byXPath("(//html/descendant-or-self::*[@id = 'attachment'])[1]"), "toto")
      Error: File not found: toto; Base directory is userdata directory: /home/coil/Webdev/sahi/userdata
      Error: File not found: toto; Base directory is userdata directory: /home/coil/Webdev/sahi/userdata
      at Sahi._setFile (http://dev.project.com/_s_/spr/concat.js:1398:12)
      at Sahi._setFile2 (http://dev.project.com/_s_/spr/concat.js:1367:7)
      at eval (eval at <anonymous> (http://dev.project.com/_s_/spr/concat.js:3480:14), <anonymous>:1:7)
      at Sahi.ex (http://dev.project.com/_s_/spr/concat.js:3480:9)
      at <anonymous>:1:11
      <a rel='nofollow' href='/_s_/dyn/Log_getBrowserScript?rel='nofollow' href=null&n=-1'><b>Click for browser script</b></a>

So, Sahi can "find" the file as it doesn't raise any error with a valid and existing file. But when the form is submitted, the file is never uploaded by the Sahi proxy.

Other checks:

  • I removed the client side HTML5 and JavaScript validation to be sure there is no side effect.
  • All my other Sahi tests are Ok, only the 3 with an Upload don't pass
  • The proxy is set in my testing browser
  • I can open the Sahi controller in the browser without problem
  • Same problem on MaxOsX and Ubuntu
  • Each time I run an upload test, I've got a new entry in /userdata/temp/download named like sahi_11a83f8806be8046fc0aaa80eac076110b95__fr-fr-2-0.bdic

What is really weird, is that I am sure that those tests passed some times ago, something must have changed in my application or configuration that breaks the Sahi file upload but I can't find what. And before in the Sahi console I had logs about the files that it was uploading, now there is no log at all.


Source: (StackOverflow)