symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.94k stars 222 forks source link

Use test environment when Panther running ? #357

Open bastien70 opened 4 years ago

bastien70 commented 4 years ago

Hello, it looks like Panther does not launch with the test environment.

According to the doc ', we could put this in the .env.test file:

.env.test

PANTHER_APP_ENV=test

When I restart Panther, the environment is indicated as the test environment. However, if I go to my login page to log users with Panther, if I enter credentials belonging to the dev environment, it will work, but not if I enter information from the test environment. .

abzolv commented 4 years ago

Same problem here.

The .env.test has a test-specific DATABASE_URL.

The test case retrieves an entityID from the database using the EntityManager. It then uses that ID in a route to retrieve the web page with $client->request('GET', sprintf('/object/edit/%s', $id)) and it gets a 404 NOT FOUND.

When the DATABASE_URL is removed from .env.test so that tests are run against the database in .env, then the above scenario works without any issues.

abzolv commented 4 years ago

I found a work-around for this problem.

In .env.test:

PANTHER_APP_ENV=panther

Then, in / create a new .env.panther and include the same DATABASE_URL that is in .env.test.

BTW, having PANTHER_APP_ENV=test in .env.test does not work because then you get:

Cannot set session ID after the session has started. (500 Internal Server Error)
metaer commented 3 years ago

When you run phpunit - there is test environment in the test class, but when you call real browser from your test via $client->request() - there could be another environment, for example "dev" - if in your .env or .env.local file APP_ENV is set to dev.

You can set APP_ENV=test in your .env.local or create test.php front-controller based on index.php front-controller and set env inside test.php: $_SERVER['APP_ENV'] = 'test';

ikerib commented 2 years ago

Same problem here. I tried with PANTHER_APP_ENV=panther in my .ent.test Then I created a new .env.panther file with database_url but it's not working.

I launched panther y headless mode with PANTHER_NO_HEADLESS=1 PANTHER_DEVTOOLS=0 php bin/phpunit tests/UserTest.php

If I go to the profiler I can see that Panther uses dev environment and also that the database_url is the one from the dev file instead of the test db

Any help or clue how to fix this?

mattsbennett commented 2 years ago

@metaer describes the issue well. We can't really use a test.php, nor do we want to fiddle with .env.local every time we run tests. We're using the following solution for the same issue with php-webdriver:

Works great for us.

sovetski commented 6 months ago

I found a work-around for this problem.

In .env.test:

PANTHER_APP_ENV=panther

Then, in / create a new .env.panther and include the same DATABASE_URL that is in .env.test.

BTW, having PANTHER_APP_ENV=test in .env.test does not work because then you get:

Cannot set session ID after the session has started. (500 Internal Server Error)

Thank you, PANTHER_APP_ENV=test in .env.test worked for me, maybe since 2020 there was some update related to this

necobm commented 4 months ago

I found a work-around for this problem. In .env.test:

PANTHER_APP_ENV=panther

Then, in / create a new .env.panther and include the same DATABASE_URL that is in .env.test. BTW, having PANTHER_APP_ENV=test in .env.test does not work because then you get:

Cannot set session ID after the session has started. (500 Internal Server Error)

Thank you, PANTHER_APP_ENV=test in .env.test worked for me, maybe since 2020 there was some update related to this

Same problem and this worked for me as well