1
votes

I am trying to set-up acceptance tests using Codeception in Yii2. So far so good with regards to installation, but I am having a route issue.

When I do: codeception run acceptance

I get this feedback:

1) Failed to ensure login page works in LoginCept (./acceptance/LoginCept.php)

Step  I fill field "input[name="LoginForm[username]"]",""
Fail  Form field by Label or CSS element with    'input[name="LoginForm[username]"]' was not found.

Scenario Steps:

 3. $I->fillField("input[name="LoginForm[username]"]","")
 2. // I am going to submit login form with no data
 1. $I->amOnPage("/backend/web/index-test.php/")

The input with name LoginForm[username] exists on the page, but apparantly Codeception is not getting the correct page.

Should /backend/web/index-test.php also have the approot path in it? When I request approot/backend/web/index-test.php in the browser it all works fine.

Thanks for any pointers. Alex

UPDATE: hereby the contents of acceptance.suite.yml:

# Codeception Test Suite Configuration

# suite for acceptance tests.
# perform tests in browser using the Selenium-like tools.
# powered by Mink (http://mink.behat.org).
# (tip: that's what your customer will see).
# (tip: test your ajax and javascript by one of Mink drivers).

# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

class_name: AcceptanceTester
modules:
        enabled:
    - PhpBrowser
    - tests\codeception\common\_support\FixtureHelper
# you can use WebDriver instead of PhpBrowser to test javascript and ajax.
# This will require you to install selenium. See http://codeception.com/docs/04-AcceptanceTests#Selenium
# "restart" option is used by the WebDriver to start each time per test-file new session and cookies,
# it is useful if you want to login in your app in each test.
#        - WebDriver
    config:
        PhpBrowser:
# PLEASE ADJUST IT TO THE ACTUAL ENTRY POINT WITHOUT PATH INFO
            url: http://localhost:8080
#        WebDriver:
#            url: http://localhost:8080
#            browser: firefox
#            restart: true

UPDATE BASED ON COMMENTS BELOW:

I'm lost. I tried hardcoding the path, and even tried hardcoding the localhost URL, but then I get this response:

$I->amOnPage("/backend/web/index-test.php/localhost/www/yii2KickDish/backend/web") 

which clearly is a bogus URL....so how can I get Codeception to resolve to the right location?

1
Doesn't Yii2 use URL rewriting? Your code should be $I->amOnPage("/"); or $I->amOnPage("/login"); - Naktibalda
Unfortunately same result... - Alex Greve
What url works in the browser? Please add the content of acceptance.suite.yml file to your question. - Naktibalda
approot/ and approot/sign-in/login both work from the browser approot/index-test.php as well. I will add acceptance.suite.yml to the question. - Alex Greve
Then use those urls in your test. - Naktibalda

1 Answers

0
votes

I got the same issue.

Seems like the "amOnPage" method build into the AcceptanceTesterActions trait isnt working well with yii2 url pattern.

That's how i solved this.

Create a class MainTester extending from AcceptanceTester

namespace tests\codeception\master\Step\Acceptance;

use tests\codeception\master\AcceptanceTester;

class MainTester extends AcceptanceTester
{
    public function amOnPage($url)
        {
           $page = \Yii::$app->getUrlManager()->createUrl($url);
           return parent::amOnPage($page);
        }
}

Then in my Cest class

use tests\codeception\master\Step\Acceptance\MainTester as AcceptanceTester;

class TestClassCest
{
    public function testMethod(AcceptanceTester $I)
    {
        $I->amOnPage('/example/something');
    }
}