Codeception Content-disposition assert file - codeception

I want to assert a xls file. I get this back:
"attachment; filename="testify.xlsx""
How can I do assertions on the file?
I have the tmp Path from the $_SERVER['TMPDIR'] and a clean filename, but there is no file in directory.

There are no built-in methods for this purpose, you will have to write your own helper.
If you are using PhpBrowser or one of framework modules, they have two useful hidden methods:
_getResponseContent returns page(or file) content,
_savePageSource saves it to file.
So your helper method would look like this:
function seeXlsFileIsValid()
{
$fileContent = $this->getModule('PhpBrowser)->_getResponseContent();
$this->assertTrue(..., 'returned xls file is not valid');
}
If you want to assert response headers, copy seeHttpHeader method from REST module.

codeception.yml
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
shuffle: true
extensions:
enabled:
- Codeception\Extension\RunFailed
coverage:
enabled: true
c3_url: 'http://www-dev.testify.com:8080/c3.php'
remote: false
whitelist:
include:
- _php/*
api.suite.yml
class_name: ApiTester
modules:
enabled:
- PhpBrowser:
url: http://www-dev.testify.com:8080
clear_cookies: false
restart: false
curl:
CURLOPT_RETURNTRANSFER: true
- REST:
url: http://www-dev.testify.com:8080
depends: PhpBrowser
part: Json
- Asserts
- Helper\Api
- Db:
dsn: 'sqlite:tests/_output/database.sqlite'
user: ''
password: ''
env:
fast:

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

Codeception, how override url from command line for a specific (or not specific) suite test?

I'm looking for a way to override the base url of my tests from the command line. In the future, I'll tests a lot of websites, so it's very unwieldy if I must add each website in a new environment in the acceptance.suite.yml file.
Currently, my acceptance.suite.yml is:
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://foo.com
browser: chrome
I see I can use the option 'override' with the run command, but even if I read the codeception document and navigate through help website (like stack overflox..), I can't find a good way for override. Can someone help me?
When I prints all config (via ./vendor/bin/concept), I get:
Array
(
actor => AcceptanceTester
modules => Array
(
enabled => Array
(
0 => Array
(
WebDriver => Array
(
url => http://foo.foo
browser => chrome
)
)
1 => \Helper\Acceptance
)
config => Array
(
)
depends => Array
(
)
)
I tried : ./vendor/bin/codecept run acceptance --steps -o 'modules: enabled: 0: WebDriver: url: http://faa.faa, but the test run ever on http://foo.foo
In this codeception issue post, it seems it's impossible to override a config value when we run a specific suite (my english is not very good , so maybe I misunderstood). So I add an env in my acceptance.suite.yml file :
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://foo.foo
browser: chrome
env:
generic:
modules:
config:
WebDriver:
url: http://faa.faa
And I tried theses commands :
./vendor/bin/codecept run acceptance --env generic --steps -o 'env: generic: modules: config: WebDriver: url: http://faa.faa
And
./vendor/bin/codecept run acceptance --env generic --steps -o 'WebDriver: url: http://faa.faa
And nothing happened. My test are always on http://foo.foo
EDIT AFTER "LEGION" HELP
When I use this acceptance.suite.yml :
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: ''
browser: ''
env:
chrome:
modules:
config:
WebDriver: {}
I get an error :
So when I use this acceptance.suite.yml :
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: ''
browser: chrome
env:
chrome:
modules:
config:
WebDriver: {}
I get an another error :
And if I use this acceptance.suite.yml :
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: ''
browser: chrome
env:
chrome:
modules:
config:
WebDriver:
url: 'http://foo.foo'
browser: 'chrome'
No error ! buuuUUUT ! I'm not on the good url x)
The url I get is "data:", its strange...
For get the url, I add this simple line in my test file :
$this->comment("I am on the url : " . $this->executeJS("return window.location.href") . "\n");
N.B: You need Codeception 1.8 or above! Before that version is bugged.
If the version is > 1.8 it should works... you may try editing your acceptance file like below and see if something is changed:
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: 'http://foo.foo/'
browser: 'firefox'
env:
chrome: #or watherver you want
modules:
config:
WebDriver:
url: 'http://fuu.fuu/'
browser: 'chrome'
run it like below:
php vendor/bin/codecept run acceptance --env chrome
** EDIT **
To pass the url from commandline you need empty WebDriver Configuration:
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver: {}
env:
chrome: #or watherver you want
modules:
config:
WebDriver:
url:'http://foo.foo'
browser:'firefox'
commandline:
./vendor/bin/codecept run acceptance --env chrome --steps -o 'WebDriver: url: \'http://faa.faa\' browser: \'chrome\''
(I usually use apex for url and browser... but not sure are really neded).

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.

Unrecognized options "suites" under "behat"

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