Codeception RuntimeException Call to undefined method FunctionalTester::amOnPage - php

I have set up a functional test following the tutorials at:
http://codeception.com/docs/04-FunctionalTests
http://codeception.com/docs/modules/Symfony
http://codeception.com/09-04-2015/using-codeception-for-symfony-projects.html
The main difference is that I've set up Codeception the traditional way because I don't want to mix test code with project code.
This is my functional test (I know it's not actually testing anything):
<?php
class MyFirstCest {
public function _before(FunctionalTester $I) {
}
public function _after(FunctionalTester $I) {
}
// tests
public function tryToTest(FunctionalTester $I) {
$I->amOnPage('/app/login/');
}
}
When I run the functional test I get:
[RuntimeException] Call to undefined method FunctionalTester::amOnPage
When I rebuild Codeception, I get:
Building Actor classes for suites: acceptance, functional, unit
-> AcceptanceTesterActions.php generated successfully. 0 methods added
\AcceptanceTester includes modules: PhpBrowser, \Helper\Acceptance
-> FunctionalTesterActions.php generated successfully. 0 methods added
\FunctionalTester includes modules:
-> UnitTesterActions.php generated successfully. 0 methods added
\UnitTester includes modules: Asserts, \Helper\Unit
The critical part seems to be \FunctionalTester includes modules: which is empty.
My functional.suite.yml file looks like this:
actor: FunctionalTester
modules:
- Symfony:
app_path: 'app'
environment: 'local_test'
class_name: FunctionalTester
modules:
enabled:
- Symfony2:
app_path: 'path/to/app'
var_path: 'path/to/app'
- Doctrine2:
depends: Symfony2
- \Helper\Functional
- PhpBrowser:
url: dev.hmr-app
- \AcmeBundle\Helper\Functional
where the Symfony app lives in path/to/app. I know there's a lot of junk in there, but that's because I've been experimenting, trying to get it to work.
What am I doing wrong?

I think that your problem is that you have
modules:
- Symfony:
app_path: 'app'
environment: 'local_test'
in the config.
That section is completely misplaced and it probably is causing you issues.
Please remove it and rename Symfony2 with Symfony in enabled section.
Also make sure that you are using the latest version of Codeception.

Related

Codeception Module \Helper\Api could not be found and loaded

When i try to add some Helper in Api.suite.yml and run build:
php vendor/bin/codecept build
, then get error:
Building Actor classes for suites: Api
In ModuleContainer.php line 102:
Module \Helper\Api could not be found and loaded
build
My Helper autogenerated with that command:
php vendor/bin/codecept generate:helper Api
directory location in attachment enter image description here
code in Api.suite.yml:
actor: ApiTester
class_name: ApiTester
modules:
enabled:
- REST:
depends: Laravel
part: Json
- \Helper\Api
config:
- Laravel:
environment_file: .env.test
step_decorators:
- \Codeception\Step\AsJson
code in Api.php:
<?php
declare(strict_types=1);
namespace Tests\Support\Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Api extends \Codeception\Module
{
}
Codeception version: Codeception 5.0.7
i tried to put other location in Api.suite.yml:
\project\tests\Support\Helper\Api
\tests\Support\Helper\Api
\Support\Helper\Api
\Helper\Api
\Api
it still doesn't work for me

Codeception. Actor classes loaded twice

I try to configure unit-tests suite in Codeception. When I run suite Codeception fail with error Fatal error: Cannot re-declare class rest\tests\UnitTester. With debugger I located places where it loads class first and second time.
First time Codeception load Actor in Codeception\Test\Loader\Gherkin during adding steps to loader.
Second time it initialize Actor instance for class with tests. In second place this class must be already included, but again loader is triggered. Here error appear.
Class is loaded with Codeception loader. At first I think that it was caused by namespace, but UnitTester is only one class in namespace. My .yml files a listed below. I would be very grateful for help.
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
modules:
config:
Yii2:
configFile: 'config/test-local.php'
unit.suite.yml:
class_name: UnitTester
modules:
enabled:
- Yii2
- Asserts
I found a reason of this problem. My codeception.yml for this tests missing namespace param when classes in test folder was under namespace. This caused my problem.

Codeception with Yii1 enabled module

I am using codeception 2.2.5 aand Yii 1.1.4
I have the issue with the Yii1 module. I have it configured for functional and unit tests
Here is the functional.suite.yml
class_name: FunctionalTester
modules:
enabled:
- Yii1:
appPath: 'www/test.php'
url: 'http://localhost/test.php'
part: init
- PhpBrowser:
url: 'http://localhost/index-test.php'
- \Helper\Functional
coverage:
enabled: true
remote: false
Here is the unit.suite.yml
class_name: UnitTester
modules:
enabled:
- Asserts
- Yii1:
appPath: 'www/test.php'
url: 'http://localhost/test.php'
- \Helper\Unit
When I run the tests separately everything works great. e.g.
php codecept.phar run functional --xml --html
php codecept.phar run unit --xml --html
When I run all together
php codecept.phar run --xml --html
It runs the functional without the issue connected with the Yii1 module. And on the unit it brings
[Codeception\Exception\ModuleConfigException]
Yii1 module is not configured!
Couldn't load application config file www/test.php
Please provide application bootstrap file configured for testing
The main goal to run all together is the code coverage.
Here is the answer to my problem:
I created a separate module Yii1Ext(Yii1Ext.php) which extends to Yii1 module:
namespace Helper;
use Codeception\Module\Yii1;
class Yii1Ext extends Yii1
{
public function _initialize()
{
$this->config['appPath'] = FRONTEND.'/'.$this->config['appPath'];
parent::_initialize();
}
}
And in configuration files have the Yii1Ext module enabled instead of Yii1 module
class_name: FunctionalTester
modules:
enabled:
- \Helper\Yii1Ext:
appPath: 'www/test.php'
url: 'http://localhost/test.php'
part: init
- PhpBrowser:
url: 'http://localhost/'
Note: Make sure all defined variables in Yii are defined by the statement
defined('ROOT_DIR') or define('ROOT_DIR', realpath(__DIR__ . '/../'));

Are some Codeception modules incompatible?

I am using Codeception to make acceptance tests for Symfony project. I need to test everything, this means testing pages (html content) as well as the ajax calls they perform (JSON content).
I have enabled the Symfony2 module in my suite settings but I find few methods in it to check JSON strings, so I guess I'm supposed to enable the REST module as well. However, if I do so, other previously existing tests fail.
For instance, this simple test:
public function privateExtranetHome(AcceptanceTester $I)
{
$I->wantTo('test something');
$I->amOnRoute('acme_site_home.es');
$I->assertEquals(
'container',
$I->grabAttributeFrom('#container-slider', 'class')
);
}
This is not a JSON testing test, just an already existing test in my Cest class, that passed when I only had the Symfony2 module enabled, and fails now that I added the REST module. The error message is :
Scenario:
* I am on route "acme_site_home.es"
[Page] http://localhost/es
[User] anon. []
* I grab attribute from "#container-slider","class"
Fatal error: Call to a member function hasAttribute() on null in [...]\Codeception\Module\REST.php on line 993
Maybe the amOnRoute() method is incompatible with the REST module, because when the latter is enabled, I get an empty response. But therefore my question is: is there some documentation, somewhere which states which modules are incompatible and should not be enabled together? Or should I take as a rule of thumb to use a separate suite for each module?
Just in case, my acceptance suite settings are as follows:
class_name: AcceptanceTester
modules:
enabled:
- Asserts
- Db
- Symfony2
- Doctrine2
- \Tests\Helper\Acceptance
And my Codeception settings:
namespace: Tests
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
extensions:
enabled: [ Codeception\Extension\RunFailed ]
modules:
config:
Db:
dsn: 'mysql:host=localhost;dbname=acme_test'
user: 'root'
password: 'blahblah'
dump: 'tests/_data/dump.sql'
populate: false
cleanup: false
Doctrine2:
depends: Symfony2
cleanup: false
PhpBrowser:
url: http://acme.com
WebDriver:
browser: firefox
url: http://acme.com
REST:
depends: Symfony2
url: http://acme.com
REST module is compatible with and relies on framework modules to process requests.
However you discovered an unreported incompatibility - grabAttributeFrom method is defined both modules - Symfony2 and REST. I will raise an issue for that.
There is one workaround you could use - grabAttributeFrom is defined in XML part of REST module, so if your REST requests return only JSON, you could enable JSON functionality only.
REST:
depends: Symfony2
part: JSON

WebGuy codeception

I'm new using codeception and i try to do a simple test: Log in and check links inside.
I'm using $I = new AcceptanceTester($scenario); in my acceptance test but i see people using WebGuy($scenario) and i don't know the difference between WebGuy and AcceptanceTester.
SigninCept.php code:
<?php
//webLOG IN
$I = new AcceptanceTester($scenario);
$I->wantTo('Log in my app mobile');
//$I->amOnUrl('192.168.X.X/app/mobile/');
$I->amOnPage('/');
$I->fillField('user','test');
$I->fillField('password','test1234');
$I->fillField('zone','01');
//$I->uncheckOption('input[type=checkbox]');
$I->seeCheckboxIsChecked('#rememberme');
$I->seeElement('input[name=submit]');
$I->click('input[type=submit]');
//succes
$I->wantTo('Check main page');
$I->amOnPage('/principal.php');
$I->seeLink('salir','salir.php');
[...]
?>
Result(cmd):
C:\xampp\htdocs\public_html\codeception>php codecept.phar run acceptance --steps
Codeception PHP Testing Framework v2.0.9
Powered by PHPUnit 4.4.0 by Sebastian Bergmann.
Acceptance Tests (1) -----------------------------------------------------------
---------------------------------------
Trying to Log in App Mobile (SigninCept)
Scenario:
* I am on page "/"
* I fill field "user","test"
* I fill field "password","test1234"
* I fill field "zone","01"
* I see checkbox is checked "#rememberme"
* I see element "input[name=submit]"
* I click "input[type=submit]"
* I am on page "/principal.php"
* I see link "salir","salir.php"
[...]
PASSED
When i use $I = new WebGuy($scenario); i get this:
C:\xampp\htdocs\public_html\codeception>php codecept.phar run acceptance --steps
Codeception PHP Testing Framework v2.0.9
Powered by PHPUnit 4.4.0 by Sebastian Bergmann.
Acceptance Tests (1) -----------------------------------------------------------
---------------------------------------
Trying to Log in App mobile(SigninCept)
Scenario:
Fatal error: Class 'WebGuy' not found in C:\xampp\htdocs\public_html\codeception\tests\acceptance\SigninCept.php on line 3
FATAL ERROR. TESTS NOT FINISHED.
Class 'WebGuy' not found
in C:\xampp\htdocs\public_html\codeception\tests\acceptance\SigninCept.php:3
I have **WebGuy.php in my acceptance dir.**
If i do build i get this:
C:\xampp\htdocs\public_html\codeception>php codecept.phar build acceptance --steps
[RuntimeException]
Too many arguments.
build [-c|--config[="..."]]
Codeception 2.x use only AcceptanceTester.
As it was said, Guys were renamed to Testers. Actor classes and
helpers are named by suite. For instance, acceptance test will start
with this line:
<?php
$I = new AcceptanceTester($scenario);
// and uses AcceptanceHelper
?>
See here : http://codeception.com/06-06-2014/codeception-2.0-final.html
The name of the "guy" depends on what you have in codeception configuration of your suite (in your case the acceptance suite):
# acceptance.suite.yml
class_name: AcceptanceTester
modules:
enabled:
- WebDriver
...
When running codecept build the guy class will be created integrating all the methods of the modules you have defined, and it will then used by CodeCeption when running the tests. Any other classes will be ignored.
Also see the help for the build command (codecept help build) to see what options you can pass to it (i.e. normally you don't need any option).
the WebGuy class is only a custom class that extends from the AcceptanceTester Class. You can implement your own and call it from the acceptance.suite.yml
class_name: AcceptanceTester ==> or ==> WebGuy // here is the call of the class
modules:
enabled:
- WebDriver:
url: 'http://local.symmetryk.com/'
browser: firefox
window_size: maximize
wait: 10
capabilities:
unexpectedAlertBehaviour: 'accept'
env:
chrome:
modules:
config:
WebDriver:
browser: 'chrome'
- Db:
hope this will help you understand :)

Categories