Codeception functional and acceptance test errors - php

I'm running the latest version of Codeception on a vagrant box and I can't get either the acceptance or functional testing working.
Here's acceptance.suite.yml:
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser
- AcceptanceHelper
config:
PhpBrowser:
url: 'http://test.dev'
and my acceptance test:
$I = new AcceptanceTester($scenario);
$I->wantTo('check Codeception');
$I->amOnPage('/');
$I->see('hello');
The index page is a simple html output with 'hello world'
The error I'm getting is as follows:
1) Failed to check codeception in testTestCept (/vagrant/test/tests/acceptance/testTestCept.php)
Sorry, I couldn't am on page "http://test.dev/":
GuzzleHttp\Exception\RequestException: cURL error 6: Couldn't resolve host 'test.dev'
Scenario Steps:
1. I am on page "http://test.dev/"
#1 /vagrant/fmx/vendor/guzzlehttp/guzzle/src/Adapter/Curl/CurlAdapter.php:91
#2 /vagrant/fmx/vendor/guzzlehttp/guzzle/src/Adapter/StreamingProxyAdapter.php:34
#3 /vagrant/fmx/vendor/guzzlehttp/guzzle/src/Client.php:186
#4 /vagrant/fmx/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:332
#5 /vagrant/fmx/tests/acceptance/AcceptanceTester.php:128
#6 /vagrant/fmx/tests/acceptance/testTestCept.php:5
#1 /vagrant/fmx/vendor/guzzlehttp/guzzle/src/Adapter/Curl/CurlAdapter.php:91
#2 /vagrant/fmx/vendor/guzzlehttp/guzzle/src/Adapter/StreamingProxyAdapter.php:34
#3 /vagrant/fmx/vendor/guzzlehttp/guzzle/src/Client.php:186
#4 /vagrant/fmx/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:332
#5 /vagrant/fmx/tests/acceptance/AcceptanceTester.php:128
#6 /vagrant/fmx/tests/acceptance/testTestCept.php:5
My functional test is just as simple:
$I = new FunctionalTester($scenario);
$I->wantTo('check Codeception');
$I->amOnPage('/');
$I->see('hello');
With this one I get the following error:
There was 1 error:
---------
1) Failed to check codeception in testTestCept (/vagrant/test/tests/functional/testTestCept.php)
#1 /vagrant/test/tests/functional/testTestCept.php:5
#2 /vagrant/test/tests/functional/testTestCept.php:5
Line 5 is specifically: $I->amOnPage('/');
I'm new to vagrant so I'm not sure if I've missed something in the setup or if I've done something else wrong. I'm running codeception through vagrant ssh.
UPDATE:
We've got the Acceptance testing working. Now just focusing on the Functional testing. As requested, below is the complete functional.suite.yml content. As I'm setting up in stages, I've not actually modified this:
# Codeception Test Suite Configuration
# suite for functional (integration) tests.
# emulate web requests and make application process them.
# Include one of framework modules (Symfony2, Yii2, Laravel4) to use it.
class_name: FunctionalTester
modules:
enabled: [Filesystem, FunctionalHelper]

I had the same problem and this helped me:
unable to run codeception in virtual machine -Mink could not be found and loaded
I needed to add an entry in my /etc/hosts file for my site inside of my vm.

Related

Chromedriver not working with codeception

So I'm making an automated test with codeception. It was working well using phpbrowser, but the page I want to test uses ajax so I changed to webdriver, following the instructions on the codeception website.
I chose to use chromedriver since I'm only going to be doing this test with chrome, but it's giving me back this error
Acceptance Tests (1) ---------------------------------------------------------------------------------------------------
FirstCest: Login successfully
Signature: FirstCest:loginSuccessfully
Test: tests\acceptance\FirstCest.php:loginSuccessfully
Scenario --
ERROR
------------------------------------------------------------------------------------------------------------------------
Time: 722 ms, Memory: 8.00 MB
There was 1 error:
---------
1) FirstCest: Login successfully
Test tests\acceptance\FirstCest.php:loginSuccessfully
[Facebook\WebDriver\Exception\WebDriverException] JSON decoding of remote response failed.
Error code: 4
The response: 'unknown command: wd/hub/session'
#1 D:\Programas\xampp\htdocs\autTest\vendor\facebook\webdriver\lib\Remote\HttpCommandExecutor.php:298
#2 D:\Programas\xampp\htdocs\autTest\vendor\facebook\webdriver\lib\Remote\RemoteWebDriver.php:126
#3 D:\Programas\xampp\htdocs\autTest\vendor\symfony\event-dispatcher\EventDispatcher.php:212
#4 D:\Programas\xampp\htdocs\autTest\vendor\symfony\event-dispatcher\EventDispatcher.php:44
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
I tried changing the chromedriver.exe to the installation path of chrome but it's still the same. I really don't know how to configure the url for chromedriver since it only came as an .exe.
This is my acceptance suite in codeception just in case:
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: 'http://localhost/ASTechOLE/login.php'
window_size: false # disabled in ChromeDriver
port: 9515
browser: chrome
capabilities:
"goog:chromeOptions": # additional chrome options
- \Helper\Acceptance
Thanks in advance
Update:
I ran chromedriver with ./chromedriver --url-base=/wd/hub
Now it gives me
There was 1 error:
---------
1) FirstCest: Login successfully
Test tests\acceptance\FirstCest.php:loginSuccessfully
[Facebook\WebDriver\Exception\WebDriverException] JSON decoding of remote response failed.
Error code: 4
The response: 'unhandled request'
#1 D:\Programas\xampp\htdocs\autTest\vendor\facebook\webdriver\lib\Remote\HttpCommandExecutor.php:298
#2 D:\Programas\xampp\htdocs\autTest\vendor\facebook\webdriver\lib\Remote\RemoteWebDriver.php:126
#3 D:\Programas\xampp\htdocs\autTest\vendor\symfony\event-dispatcher\EventDispatcher.php:212
#4 D:\Programas\xampp\htdocs\autTest\vendor\symfony\event-dispatcher\EventDispatcher.php:44
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Based on your stacktrace, it seems that you are working in Windows.
Try running the chromedriver without leading trailing slash, wd/hub instead of /wd/hub.
Your command then becomes: ./chromedriver --url-base=wd/hub.
I know, Windows can be "fun" sometimes :)

php Codeception not-so-quick start: [PHPUnit\Framework\Exception] Undefined index: ELEMENT

So, I followed the Codeception Quick Start instructions faithfully. I run the first example test using the PhpBrowser...
# Codeception Test Suite Configuration
#
# [further comments omitted]
#
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: 'http://office.localhost/'
browser: 'firefox'
- \Helper\Acceptance
and the test:
<?php
class FirstCest
{
public function frontpageWorks(AcceptanceTester $I)
{
$I->amOnPage('/');
$I->see('We hope you enjoy it');
}
}
and all is well.
Then I change the configuration to this:
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: 'http://office.localhost/'
browser: 'firefox'
- \Helper\Acceptance
per the instructions, and I have Selenium installed and up and running, and away we go...
1) FirstCest: Frontpage works
Test tests/acceptance/FirstCest.php:frontpageWorks
[PHPUnit\Framework\Exception] Undefined index: ELEMENT
Scenario Steps:
2. $I->see("InterpretersOffice") at tests/acceptance/FirstCest.php:22
1. $I->amOnPage("/") at tests/acceptance/FirstCest.php:21
#1 /opt/www/court-interpreters-office/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:198
#2 Codeception\Module\WebDriver->see
#3 /opt/www/court-interpreters-office/tests/_support/_generated/AcceptanceTesterActions.php:363
#4 /opt/www/court-interpreters-office/tests/acceptance/FirstCest.php:22
#5 FirstCest->frontpageWorks
Selenium is driving Firefox, the page is loaded, the content that $I want to see() is there, so that ain't the problem. I have poked around in the source a bit, but haven't figured this out. I have tried changing $I->see() to $I->seeInSource() and found that does work, FWIW.
Any thoughts?
The problem is apparently that Facebook's php-webdriver isn't compatible with current Firefox.
These threads discuss the issue in more detail, and php-webdriver issue #469 tracks adding full W3C WebDriver support (which will fix the incompatibility).
A workaround is to add the -enablePassthrough false argument when launching Selenium. For example:
java -Dwebdriver.gecko.driver=./geckodriver -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false
Unfortunately, Selenium removed support for pass through mode in 3.9, so you'll have to use an older version.
Another workaround is to switch to Chrome.
In my case the only solution was:
Install chromedriver in a path that is in $PATH (/usr/bin or /bin)
and using in your test class:
$capabilities = DesiredCapabilities::chrome()
It works with executing Selenium standard way:
java -jar selenium-server-standalone-3.14.0.jar
There are different situations on my Ubuntu ver. 18.x:
The vendor files has been for years, I have to rm the vendor fold and rebuild it with php composer.phar require facebook/webdriver for my PHP library.
The version of selenium-server-standalone-x.jar and chromedriver doesn't match. So download more version and try, finally you will get one pair to work.

Internal server error when trying to seeInLastEmail using Mailcatcher and Codeception

I am using the captbariton mailcatcher module for Codeception (https://github.com/captbaritone/codeception-mailcatcher-module). The system
is run on a local XAMPP-installation. I have installed and configured the Mailcatcher module in accepance.suite.xml like this:
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/development/rd_dev/webroot/
- \Helper\Acceptance
- MailCatcher
config:
MailCatcher:
url: 'http://127.0.0.1'
port: '1080'
In one test I'm sending an email from the system. I print the text that has put in the email that I send on the screen and first check that this is correct. I can manually see the email appear on in the web browser on the address and port: http://127.0.0.1:1080/ and it also works out to use a method to delete all the messages on the mailcatcher smtp-server, $I->resetEmails().
However when I use methods to access the content of the email from the test, for example $I->seeInLastEmail('foo') or $text = $I->grabFromLastEmail("bar"), then I get the error message shown below:
There was 1 error:
---------
1) ACT1050-SendEmailReportReminderWhenTryingOutServiceCept: Send email with report reminder for one report for the test company to an email address specified by the user (act1050)
Test tests/acceptance/ACT1050- SendEmailReportReminderWhenTryingOutServiceCept.php
[GuzzleHttp\Exception\ServerException] Server error: `GET http://127.0.0.1:1080/messages/3.json` resulted in a `500 Internal Server Error` response: Internal server error
Scenario Steps:
36. $I->seeInLastEmail("report") at tests/acceptance/ACT1050-SendEmailReportReminderWhenTryingOutServiceCept.php:31
35. $I->see("sent 2017-07-29") at tests/acceptance/ACT1050-SendEmailReportReminderWhenTryingOutServiceCept.php:30
34. $I->see("report") at tests/acceptance/ACT1050-SendEmailReportReminderWhenTryingOutServiceCept.php:29
33. $I->see("To: carl#example.com") at tests/acceptance/ACT1050-SendEmailReportReminderWhenTryingOutServiceCept.php:28
32. $I->see("This email has been sent") at tests/acceptance/ACT1050-SendEmailReportReminderWhenTryingOutServiceCept.php:27
31. $I->click("Send email") at tests/acceptance/ACT1050-SendEmailReportReminderWhenTryingOutServiceCept.php:26
#1 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/guzzle/src/Middleware.php:65
#2 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/Promise.php:203
#3 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/Promise.php:156
#4 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/TaskQueue.php:61
#5 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/Promise.php:246
#6 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/Promise.php:223
#7 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/Promise.php:266
#8 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/Promise.php:225
#9 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/promises/src/Promise.php:62
#10 /cygdrive/c/xampp/htdocs/bin/vendor/guzzlehttp/guzzle/src/Client.php:129
I have tried to fiddle with the settings for access to directories in httpd.conf-file (and after changed back to normal), but I have not succeded.
Can someone help me with this issue?

Vagrant, Codeception & Laravel issue. NotFoundHttpException

I'm trying to get into using codeception for my acceptance testing.
I have the following for one of my tests:
<?php
use Codeception\Util\Stub;
class SomeTest extends \Codeception\TestCase\Test
{
protected $webGuy;
/**
* #test
*/
public function incorrect_login_should_redirect_back()
{
$I = $this->webGuy;
$I->wantTo('fail at logging in');
$I->amOnPage('/'); // <-- This is the line that is failing
$I->fillField('email','info#tntstudio.hr');
$I->fillField('password','pass');
$I->click('Login');
$I->see('email', 'input');
$I->seeCurrentUrlEquals('/login');
}
}
Initially the tests ran OK, however after adding Laravel4 to the acceptance.suite.yml file and running build, the test now fails with the following:
1) SomeTest::incorrect_login_should_redirect_back
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
#1 /Applications/MAMP/htdocs/hired/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1021
#2 /Applications/MAMP/htdocs/hired/vendor/laravel/framework/src/Illuminate/Routing/Router.php:989
#3 /Applications/MAMP/htdocs/hired/vendor/laravel/framework/src/Illuminate/Routing/Router.php:968
#4 /Applications/MAMP/htdocs/hired/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:738
#5 /Applications/MAMP/htdocs/hired/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:708
#6 /Applications/MAMP/htdocs/hired/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
#7 /Applications/MAMP/htdocs/hired/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:325
#8 /Applications/MAMP/htdocs/hired/app/tests/acceptance/WebGuy.php:476
#9 /Applications/MAMP/htdocs/hired/app/tests/acceptance/SomeTest.php:16
I'm running my app in a virtual environment using vagrant, at http://localhost:3030/
I have set this to the url for the PhpBrowser config in acceptance.suite.yml as below:
class_name: WebGuy
modules:
enabled:
- PhpBrowser
- WebHelper
- Laravel4
config:
PhpBrowser:
url: 'http://localhost:3030/'
I'm wondering if anybody else has come across this, or has any ideas on how to get around this, I've been tearing my hair out for hours on this.
Laravel4 module will cause Codeception to use the "testing" environment in Laravel.
Laravel will disable all route filters in "testing" environment - so your filters are not working correctly and its probably calling the wrong route, causing your app to die and your test to fail.
I dont think using the Laravel4 module with "acceptance" tests is correct - it should only be for functional tests? Edit: I just found that the Codeception Laravel4 module docs actually say "This module allows you to run functional tests for Laravel 4" - so I guess it was not actually designed for Acceptance tests?
But with all the changes in Codeception 2.x - you are better off using PhpBrowser module for your acceptance tests, and Laravel4 module for your functional tests.
If you are using Homestead, I do this in my start.php file to detect if Codeception is running, and specifically put it into a 'codeception' environment, otherwise I let it run my environment detection normally
if ((gethostname() === 'homestead') && (isset($_SERVER['REMOTE_ADDR'])) && ($_SERVER['REMOTE_ADDR'] === '127.0.0.1'))
{
$env = $app->detectEnvironment(['codeception' => ['homestead']]);
}
else
{
$env = $app->detectEnvironment(['dev' => ['homestead']]);
}
Then in my 'codeception' environment, I setup a SQLite file database, and run acceptance tests against that (which is faster than mySQL testing).
You don't even have to change your start.php. You can set the environment for codecption in the laravel4 Module config. So in your acceptance.suite.yml it will look like this:
modules:
enabled: [PhpBrowser, WebHelper, Laravel4]
config:
Laravel4:
environment : 'codeception'
filters : true
Now, when you execute php codecept run acceptance in your terminal, the Laravel4 Module will use the configuration files from app/config/codeception.
Was not able to find a solution to this, so for now am going to just go without the Laravel4 module, sadly.

How can i get phpunit to run tests from all files in a folder?

From what I've read, it seems like I should be able to set up a folder, e.g. tests/ , put a few files in it with unit test classes, and then run phpunit on that file and have it find and run the tests.
For whatever reason, in my installation (on OS X), it thinks the folder tests/ is a file, or so it would seem:
$ ls tests
test1.php test2.php
$ phpunit tests/test1.php
PHPUnit 3.5.3 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 5.00Mb
There was 1 failure:
1) FailingTest::testFail
Your test successfully failed!
/Users/****/tmp/tests/test1.php:4
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
$ phpunit tests/test2.php
PHPUnit 3.5.3 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 5.00Mb
OK (1 test, 1 assertion)
$ phpunit tests
PHP Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "tests.php" nor "tests.php" could be opened.' in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php:102
Stack trace:
#0 /usr/local/PEAR/PHPUnit/TextUI/Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('tests', '')
#1 /usr/local/PEAR/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#2 /usr/local/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
thrown in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php on line 102
Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "tests.php" nor "tests.php" could be opened.' in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php:102
Stack trace:
#0 /usr/local/PEAR/PHPUnit/TextUI/Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('tests', '')
#1 /usr/local/PEAR/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#2 /usr/local/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
thrown in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php on line 102
I have what I hope is a fairly standard installation of phpunit via PEAR, following these instructions http://www.newmediacampaigns.com/page/install-pear-phpunit-xdebug-on-macosx-snow-leopard, on OS X Snow Leopard.
$ pear version
PEAR Version: 1.9.1
PHP Version: 5.3.2
Zend Engine Version: 2.3.0
Running on: **** 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386
$ phpunit --version
PHPUnit 3.5.3 by Sebastian Bergmann.
I'm hoping someone else out there ran into this issue and it's just a simple fix, or else I'm just doing something wrong?
It is not a bug, it is a feature.
You have a directory full of .php files, in your case they all contain testcases.
But as your testsuite grows, you will likely want to have other php files inside tests directory, files that do not contains tests, that exist solely to support tests. Those files should never be executed by PHPUnit itself.
This is a very common scenario.
So how would PHPUnit know which files it needs to run and which ones not?
Checking the file name suffix is one option for doing it - by default PHPUnit considers everything with name ending with Test.php as being a test and ignores everything else.
You can change that behaviour if you really want to - by creating a file named phpunit.xml in your tests directory with the following content
<?xml version="1.0" encoding="utf-8" ?>
<phpunit>
<testsuite name='Name your suite'>
<directory suffix='.php'>./</directory>
</testsuite>
</phpunit>
Once you have done that, PHPUnit will run all files with '.php' at the end of the file name
(in this context file extension is considered to be part of the file name)
But it really is better to get used to the convention and name your tests accordingly.
The simpler way to running test on folder is to add "Test.php" at the end all of your tests and run phpunit specifing your folder like this
phpunit .
or
phpunit your_test_folder/.
Annoying little quirk, but I figured it out.
At least with the default configuration, test files have to end with "Test.php", eg. fooTest.php, or they are not found by the test runner.

Categories