soulcodex / laravel-behat

A powerfully extension to integrate laravel with behat from scratch and start writing great feature histories.
7 stars 2 forks source link

Scenario isolation does not work #1

Closed romanzks closed 1 year ago

romanzks commented 1 year ago

The new scenario saves the state of the previous one, which shouldn't be

soulcodex commented 1 year ago

Hi @romanzks can you tell me which state isn't refreshed? Database, Application kernel or any other ?

Best

romanzks commented 1 year ago

If I understand correctly, this is Application kernel.

For example, in the first scenario, I log in as a user. in the second, I have to be not logged, but I remain logged in.

soulcodex commented 1 year ago

Great at least one clue, let me check it this weekend to brings you a fast solution 😉

Are you currently extending from the package base context? 👀

Thank you very much for your great first issue

romanzks commented 1 year ago

Thanks for the great extension. After the original extension stopped supporting Laravel, your extension became a great replacement. This is my config file, maybe it will be useful.

default:
  extensions:
    Soulcodex\Behat:
      kernel:
        bootstrap_path: '/bootstrap/app.php'
        environment_path: '.env.behat'
    Behat\MinkExtension:
      base_url: https://example.test/
      default_session: laravel
      laravel: ~

  suites:
    default:
      contexts:
        - FeatureContext
      paths:
        - "%paths.base%/features"
soulcodex commented 1 year ago

One question more, can you share me your FeatureContext file here please?🙏🙏

romanzks commented 1 year ago

The bug reproduces even with an empty FeatureContext class and such a scenario.

use Behat\Behat\Context\Context;
use Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext implements Context
{
}
Feature: Sign in to the website
    I need to be able to log in to the website

    Scenario: Log in with email and password
      Given I am on "/login"
       When I fill in the following:
          | email    | admin@admin.com |
          | password | password        |
       And I press "Login"
      Then I should be on "/admin"
       And I should see "Hello Admin"

    Scenario: Log in with bad credentials
      Given I am on "/login"
       When I fill in the following:
          | email    | admin@admin.com |
          | password | wrong_password  |
        And I press "Login"
       Then I should be on "/login"

I am getting an error on the second scenario. The user does not see the login field, because already authorized.

romanzks commented 1 year ago

My guess is that the issue may lie somewhere here KernelAwareInitializer

    public function rebootKernel()
    {
        if ($this->context instanceof KernelAwareContext) {
            $kernelConfig = $this->kernelConfig();
            $this->app->flush();

            $laravel = new LaravelEnvironmentArranger(
                $kernelConfig->basePath(),
                $kernelConfig->environmentFile()
            );

            $this->context->session('laravel')
                ->getDriver()
                ->reboot($this->app = $laravel->boot($kernelConfig->toArray()));
        }
    }
romanzks commented 1 year ago

It is not possible to reset the session even in manual mode, using a hook.

/**
 * @BeforeScenario
 */
public function before(BeforeScenarioScope $event)
{
    $this->getSession()->restart();
}
soulcodex commented 1 year ago

Try to extend from Soulcodex\Behat\Addon\Context as well and run again the test to see what's happen 😁

romanzks commented 1 year ago

Can you please tell me how to use the methods of the MinkContext class in this case?

soulcodex commented 1 year ago

I have to improve this, let me work on this and I will keep you updated through this issue 😁😇

romanzks commented 1 year ago

Ok. I think i figured out how to do it

  suites:
    default:
      contexts:
        - Behat\MinkExtension\Context\MinkContext
        - FeatureContext
      paths:
        - "%paths.base%/features"

But now I am getting a new error on the second scenario.

Scenario: Log in with bad credentials # features/login.feature:17
    Given I am on "/login"              # Behat\MinkExtension\Context\MinkContext::visit()
      Target [Illuminate\Contracts\Http\Kernel] is not instantiable. (Illuminate\Contracts\Container\BindingResolutionException)
romanzks commented 1 year ago

Although no. This only allows the mink functions to be used in the scenario, but not in the context class of course. I will follow up with new information 🙏

soulcodex commented 1 year ago

Yes, I have to improve the access to the Mink instance from my base context class and add useful methods to interact with Mink along the test session 😀

soulcodex commented 1 year ago

Hi @romanzks please install dev-main and test again, I strongly advise you read again the README.md I added clarifications and new things :smile: Tell me then if you are able from your context to use the method $this->session() and get access to the Mink session also adding the new RootContext to your behat.yml file you have a lot of pre-built assertions @Given @When @Then from Mink.

$this->session('laravel') 
romanzks commented 1 year ago

Thanks for update! Looks like not everything is perfect yet... 😄

  1. I still don't understand how I can use the Mink functions in the Context file.
    
    <?php

declare(strict_types=1);

namespace App\User\Test\Feature;

use App\User\Domain\UserRepository; use Soulcodex\Behat\Addon\Context;

final class UserContext extends Context { public function __construct(private UserRepository $userRepository) { }

/**
 * @Given I send a request to :url
 */
public function iSendARequestTo(string $url): void
{
    $this->visit($url); //Fatal error: Call to undefined method UserContext::visit() (Behat\Testwork\Call\Exception\FatalThrowableError)
}

}


2. Let's look at this scenario.
```gherkin
Feature: Sign in to the website
    I need to be able to log in to the website

    Scenario: Log in with bad credentials
      Given I am on "/login"
       When I fill in the following:
          | email    | admin@admin.com |
          | password | wrong_password  |
        And I press "Login"
       Then I should be on "/login"

    Scenario: Log in with email and password
      Given I am on "/login"
       When I fill in the following:
          | email    | admin@admin.com |
          | password | password        |
       And I press "Login"
      Then I should be on "/admin"
       And I should see "Hello Admin"

In the second scenario I am getting the error:

Form field with id|name|label|value|placeholder "email" not found. (Behat\Mink\Exception\ElementNotFoundException)

When I print this page I see this:

<!-- Warning: Undefined variable $errors in /var/www/html/storage/framework/views/aa127e567f581ec5829496b762341b271c84c777.php line 58 (View: /var/www/html/resources/views/themes/tailwind/auth/login.blade.php) (500 Internal Server Error) -->

It seems that some kind of state is still saved ... If you swap the scenarios in places, then there is no error.

Thanks for your help and advice!

soulcodex commented 1 year ago

The gherkin table has an incorrect format the correct format is

|<header>|
|<value>|

Let me add more documentation about the use of Mink using the base context and check the state reset, looks weird 🤔

romanzks commented 1 year ago

But this format works. I found it here https://robinvdvleuten.nl/blog/handle-authenticated-users-in-behat-mink/

soulcodex commented 1 year ago

As far as I know the correct format is the described above but let me check too if I can reproduce your error 😉

soulcodex commented 1 year ago

Hi @romanzks

Apparently i spot the bug but i need more time to work on this, your gherkin table was correct but usually i use the columnar format which confused me but its ok. I hope back soon with good news :smile: :rocket:

soulcodex commented 1 year ago

Hi @romanzks

Please install dev-main again and test it, to detect the bug in the best way I have recreated a scenario similar to the one you show in your .feature files and apparently after several rounds of testing it works, I want to know your experience and hear your ideas for the next versions of the package. I have also added a way to access the current Mink session with the $this->session() method from the class that extends the context base class offered by the package.

image

Regards

soulcodex commented 1 year ago

@romanzks now version 1.0.3 is available on packagist with all the fixes and with support for Laravel 10. Let me know if you have any other issues with the library and I can give you support.