I try to create a context with Behat but I have always this error :
"Mink instance has not been set on Mink context class. Have you enabled the Mink Extension? (RuntimeException)"
I tried a lot of things to activate it, I change my behat.yml, I add to composer
but nothing changing.
I tried all the solutions i saw on the web, now i'm lost...
This is the behat.yml
default:
extensions:
Behat\MinkExtension:
goutte: ~
selenium2: ~
sessions:
goutte:
goutte: ~
selenium2:
selenium2: ~
symfony2:
symfony2: ~
suites:
default:
contexts:
- FeatureContext
- Mink\MinkExtension\Context\DrupalContext
- Mink\MinkExtension\Context\MinkContext
And my context
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Mink;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements Context
{}
As far as I know, if FeatureContext extends MinkContext or another class that extends MinkContext, then you need to add to the contexts in behat.yml just the FeatureContext.
Also in your feature add #api tag to use the goutte driver or add #javascript to use selenium2 driver.
Related
New to PHP and Codeception, but not to programming. I've just set up Codeception in my web site's root folder.
During the install, I ran:
php vendor/bin/codecept bootstrap
and
php vendor/bin/codecept build
My _support/AcceptanceTester.php contains this:
class AcceptanceTester extends \Codeception\Actor
{
use _generated\AcceptanceTesterActions;
/**
* Define custom actions here
*/
}
and acceptance.suite.yaml has this:
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/shop
- \Helper\Acceptance
step_decorators: ~
What am I missing???
Depending on if my FeatureContext class extends RawMinkContext or MinkContext I'm having issues running Behat tests.
Here's my FeatureContext.php
<?php
use Behat\Behat\Context\ClosuredContextInterface;
use Behat\Behat\Context\TranslatedContextInterface;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\BehatContext;
use Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Goutte\Client;
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext {
/**
* Initializes context.
* Every scenario gets its own context object.
*
* #since 1.0.0
*
* #param array $parameters context parameters (set them up through behat.yml)
* #return null
*/
public function __construct() {
}/* __construct() */
}
?>
and my behat.yml
default:
suites:
default:
path: %paths.base%/features
contexts:
- FeatureContext
- Behat\MinkExtension\Context\MinkContext
- Behat\MinkExtension\Context\RawMinkContext
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: 'http://somesite.com'
goutte: ~
javascript_session: selenium2
files_path: 'vendor'
selenium2: ~
sessions:
default:
goutte: ~
goutte:
goutte: ~
selenium2:
selenium2: ~
symfony2:
symfony2: ~
formatters:
html:
output_path: %paths.base%/reports
PHP Fatal error: Call to a member function getSession() on a non-object in /home/behat/composer/vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 101
If I extend RawMinkContext instead of MinkContext then the tests run, but it adds (blank) versions of the default steps that are defined in MinkContext (i.e. If I am on [url] or When I go to [url] etc. ).
If I run behat -c ./config/behat.yml (or, just ./behat.yml - I have an identical behat.yml file in both locations as I wasn't sure it was being used properly) I get the same results as above. I'm almost certainly doing something dumb...
My composer.json for reference:
{
"name": "behat mink skeleton",
"require": {
"behat/behat": "*",
"behat/mink-extension": "*",
"behat/mink-browserkit-driver": "*",
"behat/mink-goutte-driver": "*",
"behat/mink-selenium2-driver": "*",
"bossa/phpspec2-expect": "*",
"teaandcode/behat-guzzle-extension": "*",
"sauce/sausage": ">=0.5",
"sauce/connect": ">=3.0",
"emuse/behat-html-formatter": "dev-master"
},
"minimum-stability": "dev",
"config": {
"bin-dir": "bin/",
"github-oauth": {
"github.com": "ac1dd3678488663ccc1ba02a5d1d474e1a78bb93"
}
}
}
If you extend MinkContext in FeatureContext then remove MinkContext from behat.yml.
behat.yml file should contain only the local context under contexts section, in your case you don't need MinkContext or RawAwareContext, only FeatureContext.
I am having such behat.yml
default:
autoload:
#'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
#'': %paths.base%/tests/AppBundle/features/user_login/bootstrap
[ %paths.base%/tests/AppBundle/features/user_registration/bootstrap, %paths.base%/tests/AppBundle/features/user_login/bootstrap ]
formatters:
progress: ~
suites:
app_features:
#paths:
#features: %paths.base%/tests/AppBundle/features
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
#- tests\AppBundle\user_registration\UserRegistrationContext
- UserRegistrationContext
- UserLoginContext
#suites:
# default:
# contexts:
# - FeatureContext:
# session: '#session'
extensions:
Behat\Symfony2Extension: ~
Also having this folder structure:
With UserRegistrationContext class its ok, it does not throw errors. Then I added UserLoginContext and it cannot find.
I am using "behat/behat": "^3.1" in composer.json.
behat.yml file is in same level as 'tests' directory.
UserLoginContext looks like this:
<?php
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Driver\GoutteDriver;
use Behat\Mink\Session;
use AppBundle\Controller\UserController;
/**
* Defines application features from the specific context.
*/
class UserLoginContext implements Context, SnippetAcceptingContext
{
private $session;
/**
* 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()
{
// Choose a Mink driver. More about it in later chapters.
$driver = new GoutteDriver();
$this->session = new Session($driver);
}
}
Why the UserLoginContext is not found?
Update
I noticed that if I do like this - leave one contexts, then UserLoginContext is found:
default:
autoload:
#'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
'': %paths.base%/tests/AppBundle/features/user_login/bootstrap
#[ %paths.base%/tests/AppBundle/features/user_registration/bootstrap, %paths.base%/tests/AppBundle/features/user_login/bootstrap ]
formatters:
progress: ~
suites:
app_features:
#paths:
#features: %paths.base%/tests/AppBundle/features
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
#- tests\AppBundle\user_registration\UserRegistrationContext
#- UserRegistrationContext
- UserLoginContext
#suites:
# default:
# contexts:
# - FeatureContext:
# session: '#session'
extensions:
Behat\Symfony2Extension: ~
Update
Temporary solution I found - to create a new profile for each context. But I guess its not how it should be, but still will show how I have done:
user_login:
autoload:
'': %paths.base%/tests/AppBundle/features/user_login/bootstrap
formatters:
progress: ~
suites:
app_features:
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
- UserLoginContext
extensions:
Behat\Symfony2Extension: ~
And then run with --profile parameter:
sudo vendor/behat/behat/bin/behat --profile user_login
The small problem is that when I will want to run all tests, I will have to run many commands. Also config repetition. Still waiting for better solution.
Here I noticed small text which helped a bit.
http://docs.behat.org/en/v3.0/guides/6.profiles.html
Using behat.yml to autoload will only allow for PSR-0. You can also
use composer.json to autoload, which will also allow for PSR-4
Shame for behat - they really would need to write more about this, cause new user would not understand at all how to use it together with composer.json.
What I have done:
added namespace for the UserLoginContext class same as folder structure
namespace Tests\AppBundle\features\user_login\bootstrap;
Added this namespace to behat.yml near contexts:
.
# for each context class - new profile.
default:
autoload:
'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
formatters:
progress: ~
suites:
app_features:
#paths:
#features: %paths.base%/tests/AppBundle/features
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
- UserRegistrationContext
- Tests\AppBundle\features\user_login\bootstrap\UserLoginContext
#suites:
# default:
# contexts:
# - FeatureContext:
# session: '#session'
extensions:
Behat\Symfony2Extension: ~
Also those who have not in their composer.json, need to add this:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
Ran
composer dump-autoload -o
I am lucky that I have experience with PSR-4 and could guess the solution and that I had time to think and experiment.
I am using behat 2.4(stable), and I am trying to pass parameters into class's, that extends BehatContext class, constructor.
However I get 'Unrecognized options "suites" under "behat"', with the following set up.
I am using the following behat.yml file:
default:
suites:
default:
contexts:
exampleContext:
browser: http://localhost:8080
browser_version: /var/tmp
paths:
features: %behat.paths.base%/../../
bootstrap: %behat.paths.base%/../../
extensions:
Behat\MinkExtension\Extension:
# base_url is the URL to your dev site. Make sure you include
# the port at the end. e.g.
base_url: http://example.web01.com
default_session: selenium2
browser_name: 'chrome'
selenium2:
capabilities: { "browser": "firefox", "version": "14"}
sites/all/libraries/extensions/DrupalBehatExtension.php: ~
filters:
# The default profile does not rebuild from nothing so do not run tests
# that require a rebuild from nothing.
tags: ~#require-rebuild
context:
class: DrupalContext
parameters:
# If you'd like to test on a clone of the actual site data set to true.
use_test_database: false
# If an existing test database exists then it will be used unless reset_database is set to true.
reset_database: false
# If you would like to clear the db and run site install on every feature
# set to the name of an install profile, otherwise set to false.
# If you do set this you should also set use_test_database to true.
rebuild_on_feature: false
# If you would like to enable a module after running site install
rebuild_module: false
# Set the name of the site folder to use for settings.php.
site: dev.example.com
My class implementation is
class exampleContext extends BehatContext {
public function __construct($browser = 'ie', $browser_version = '8') {
...
}
How can I achieve this? The only answer I've found is the one that demands to take out the DrupalContext which is rather important.
How can i get the Parameters from Behat.yml to a php file?
suites option was added in behat3. You should update version.
Try this. This is for Behat 2 not 3 though!
mySymfonyProject/composer.json:
"require": {
"behat/behat": "2.5.*#stable",
"behat/behat-bundle": "1.0.0",
"behat/symfony2-extension": "1.1.2",
"behat/mink": "1.5.0",
"behat/mink-extension": "~1.3",
"behat/mink-selenium2-driver": "1.1.1",
"behat/mink-goutte-driver": "1.0.9"
},
"config": {
"bin-dir": "bin"
},
"minimum-stability": "dev",
behat.yml
default:
context:
class: FeatureContext
parameters:
browser: 'ie'
browser_version: '8'
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
kernel:
env: test
debug: true
Behat\MinkExtension\Extension:
base_url: 'http://mysymfonyproject.local/app_test.php/'
javascript_session: selenium2
browser_name: firefox
goutte: ~
selenium2: ~
paths:
features: %behat.paths.base%/src
bootstrap: %behat.paths.features%/Context
CONTEXT FEATURE
mySymfonyProject/src/Site/CommonBundle/Features/Context/FeatureContext.php
<?php
namespace Site\CommonBundle\Features\Context;
use Behat\MinkExtension\Context\MinkContext;
class FeatureContext extends MinkContext
{
public function __construct($browser, $browser_version)
{
// Do whatever you wish with params
}
//And your own methods
}
TESTS
When you have feature files you can run them like this (this runs all in one go. for more info read behat doc):
bin/behat #SiteCommonBundle
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.
This is because you should copy vendor/behat/behat/behat.yml.dist file to your/project/root/behat.yml, rather than editing the file in the vendor dir and add extesions to the default section.
And here's what it looks like:
default:
extensions:
Behat\MinkExtension\Extension:
base_url: http://lunch-time/app_dev.php
goutte: ~
selenium2: ~
paths:
features: features
bootstrap: features/bootstrap
annotations:
paths:
features: features/annotations
closures:
paths:
features: features/closures
I was facing a similar issue. We need to tell Symfony to initialize the object.
Mine got fixed after adding under the default > suites > my_suite.
contexts: [Behat\MinkExtension\Context\MinkContext]
Here is how my new behat.yml looks like.
default:
suites:
my_suite:
type: symfony_bundle
bundle: AcmeProjectManagerBundle
contexts: [Behat\MinkExtension\Context\MinkContext]
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://en.wikipedia.org
goutte: ~
selenium2: ~
sessions:
default:
symfony2: ~