1

Codeception has documentation for functional tests at: https://codeception.com/docs/05-UnitTests

So following that on my laravel/homestead project I do as follows:

in functional.sute.yml:

class_name: FunctionalTester
modules:
    enabled:
    - Laravel5
    - \Helper\Functional

My test:

  <?php


    class LoginCest
    {
        public function _before(FunctionalTester $I)
        {
        }

        public function _after(FunctionalTester $I)
        {
        }

        // tests
        public function tryLogin (FunctionalTester $I)
        {
            $I->amOnPage('/login');
            $I->fillField('email', 'someemail');
            $I->fillField('password', 'somepw');
            $I->click('Login');
            $

    I->see('some text');
            }
        }

So when I run the test, it fails:

There was 1 error:



 ---------
    1) LoginCest: Try login
     Test  tests/functional/LoginCest.php:tryLogin

      [ExternalUrlException] Codeception\Module\Laravel5 can't open external URL: http://myapp.test/login  


    Scenario Steps:

     4. $I->click("Login") at tests/functional/LoginCest.php:20
     3. $I->fillField("password","somepw") at tests/functional/LoginCest.php:19
     2. $I->fillField("email","someemail") at tests/functional/LoginCest.php:18
     1. $I->amOnPage("/login") at tests/functional/LoginCest.php:17

    #1  Codeception\Lib\InnerBrowser->click
    #2  /home/vagrant/Code/my-app/tests/_support/_generated/FunctionalTesterActions.php:1114
    #3  /home/vagrant/Code/my-app/tests/functional/LoginCest.php:20
    #4  LoginCest->tryLogin

    ERRORS!
    Tests: 1, Assertions: 0, Errors: 1.

My app url is someapp.test which is running on homestead.

In looking at my LoginController I see:

$this->redirectTo();

at the very end.

Now I understand that functional tests do not require a webserver and I could probably make it work using the acceptance test. But really having a hard time understanding on why anyone would use codeception to do functional tests if you cant even specify a url. Also why would codeception use a login example for functional test when others may face similar issues?

1 Answer 1

2

Some background first.

URL consists of these main parts: PROTOCOL://DOMAIN:PORT/URI?QUERY_STRING#HASH HASH is only used in client side and not passed to webserver, so it can't be used for routing. PROTOCOL and PORT can be used for routing, but that is very unusual. Some websites display different contents depending on DOMAIN which is used to access them, but most only use URI and/or QUERY_STRING parts for routing and displaying the right page.

The main distinction of functional testing using Codeception is that it doesn't require webserver and because of that mostly doesn't care about domain names.

Website code usually doesn't care if it is accessed using http://myapp.test/ or http://google.com/ and happily returns your front page for either. Though if a click on http://google.com/ link rendered your front page it almost certainly would be wrong. To prevent that, external domain check was implemented years ago.

All internal links in your website must have no domain component or it must match the one passed using Host header. Exception is made for domains that are using for domain based routing, such domains can be used in tests.

Sign up to request clarification or add additional context in comments.

2 Comments

How do we set the Host header in codeception?
You can use haveHttpHeader function, but it doesn't work in expected way since Symfony changed handling of Host header in 2019: github.com/symfony/symfony/issues/32791

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.