Behat how to create a multiple context - behat

After vendor/bin/behat --dry-run --append-snippets
my behat.yml
default:
suites:
default:
contexts:
- FeatureContext
- OneContext
but context is not shown. enter code here

Related

error [ModuleException] Doctrine2: Module can't be accessed

I am having trouble to follow the doc in https://codeception.com/docs/modules/Doctrine2. This the error
[ModuleException] Doctrine2: Module can't be accessed
I want to access $entityManger in the unit test. The doctrine bootstrap.php have a createEntityManager function that returns entityManager.
#codeception.yml
suites:
unit:
path: .
actor: UnitTester
modules:
enabled:
# add more modules here
- Asserts
settings:
bootstrap: ../config/bootstrap.php
shuffle: true
lint: true
paths:
tests: tests
output: tests/_output
support: tests/_support
data: tests
# unit.suite.yml
actor: UnitTester
modules:
enabled:
- Asserts
- Doctrine2:
connection_callback: ['createEntityManager']
cleanup: true # All doctrine queries will be wrapped in a transaction, which will be rolled back at the end of each test
- \Helper\Unit
Your problem is that you have unit suite configured in codeception.yml file, so your unit.suite.yml is ignored.
Remove this section from codeception.yml or remove unit.suite.yml and update codeception.yml
suites:
unit:
path: .
actor: UnitTester
modules:
enabled:
# add more modules here
- Asserts

Behat : Cant activate the mink extension in feature context

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.

Codeception acceptance tests run before browser starts

When I run Codeception tests, sometimes on acceptance tests a browser starts too late, tests do not wait for it to start, and I get errors for tests that passed before a browser start:
[ConnectionException] Can't connect to Webdriver at http://127.0.0.1:4444/wd/hub. Please make sure that Selenium Server or PhantomJS is running.
#1 Codeception\Subscriber\Module->before
#2 D:\path\Yii\basic\vendor\symfony\event-dispatcher\EventDispatcher.php:212
#3 D:\path\Yii\basic\vendor\symfony\event-dispatcher\EventDispatcher.php:44
One time a browser didn't start at all.
My command:
C:\Windows\System32\cmd.exe /K "cd /D D:\path\Yii\basic && vendor\bin\codecept run"
acceptance.suite.yml:
class_name: AcceptanceTester
extensions:
enabled:
- Codeception\Extension\RunProcess:
- java -jar -Dwebdriver.chrome.driver="D:/Selenium/chromedriver.exe" -Dwebdriver.gecko.driver="D:/Selenium/geckodriver.exe" "D:/Selenium/selenium-server-standalone-3.6.0.jar"
modules:
enabled:
- WebDriver:
url: https://hotel.localhost/
browser: chrome
- Yii2:
part: orm
entryScript: index-test.php
cleanup: false
codeception.yml:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
memory_limit: 1024M
colors: true
modules:
config:
Yii2:
configFile: 'config/test.php'
cleanup: false
coverage:
enabled: true
whitelist:
include:
- models/*
- controllers/*
Thank you in advance.
Add some sleep to RunProcess configuration as documented here.
To wait for the process to be launched use sleep option. In this case you need configuration to be specified as object:
extensions:
enabled:
- Codeception\Extension\RunProcess:
0: java -jar ~/selenium-server.jar
1: mailcatcher
sleep: 5 # wait 5 seconds for processes to boot

Behat 3.1 - multiple contexts, second context class not found

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.

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.
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: ~