sensiolabs / BehatPageObjectExtension

MIT License
117 stars 48 forks source link

Fatal error: Call to a member function open() on null #98

Closed nietzscheson closed 6 years ago

nietzscheson commented 6 years ago

My project works on Vagrant with Docker Engine. I have the follow configurations in behat:

###behat.yml
default:

    extensions:
        Behat\MinkExtension:
            base_url: "http://192.168.33.10/app_test.php/"
#            default_session: chrome
            javascript_session: chrome
            sessions:
                chrome:
                    selenium2:
                        wd_host: "http://192.168.33.10:4444/wd/hub/"
                        browser: chrome
                        capabilities:
                            browserName: chrome
                            browser: chrome
                            version: ""
                            chrome:
                                switches:
                                    - "start-fullscreen"
                                    - "start-maximized"
                                    - "no-sandbox"

        SensioLabs\Behat\PageObjectExtension: ~

### sign_in.feature
@security
Feature: Sign in into Application

    @ui
    Scenario: Search
        Given I am on the login
        When I fill the username with "user@usuario.com"
        And I fill the password with "password"
        When I want a login
        Then I should see "Credenciales no válidas."
class SecurityContext implements Context
{

private $loginPage;

public function __construct(LoginPage $loginPage)
{
  $this->loginPage = $loginPage;
}

/**
 * @Given I am on the login
 */
public function iAmOnTheLogin()
{
   $this->loginPage->open();
}

When I run behat it fails and throws me the error: Fatal error: Call to a member function open() on null (Behat\Testwork\Call\Exception\FatalThrowableError)

DonCallisto commented 6 years ago

Sounds weird: are you sure you're not overriding $this->loginPage somehow? Because if $loginPage in __construct was null, then type-hint should have failed

nietzscheson commented 6 years ago

The loginPage:

<?php

namespace Behat\Page\Security;

use SensioLabs\Behat\PageObjectExtension\PageObject\Page;

class LoginPage extends Page
{
  protected $path = 'login';
}
DonCallisto commented 6 years ago

@nietzscheson could you put a var_dump after $this->loginPage = $loginPage; and tell us what you get?

nietzscheson commented 6 years ago

It's the result of var_dump($this->loginPage):

class ProxyManagerGeneratedProxy\__PM__\Behat\Page\Security\LoginPage\Generated892a68bed1424ff805ea5e5652e59e48#4143 (10) {
      │   private $valueHolder59d3b5d715938297741385 =>
      │   NULL
      │   private $initializer59d3b5d715c36812164521 =>
      │   class Closure#3957 (3) {
      │     public $static =>
      │     array(2) {
      │       'pageObjectClass' =>
      │       string(29) "Behat\Page\Security\LoginPage"
      │       'decoratedFactory' =>
      │       class SensioLabs\Behat\PageObjectExtension\PageObject\Factory\DefaultFactory#3757 (3) {
      │         ...
      │       }
      │     }
      │     public $this =>
      │     class SensioLabs\Behat\PageObjectExtension\PageObject\Factory\LazyFactory#3763 (2) {
      │       private $decoratedFactory =>
      │       class SensioLabs\Behat\PageObjectExtension\PageObject\Factory\DefaultFactory#3757 (3) {
      │         ...
      │       }
      │       private $proxyFactory =>
      │       class ProxyManager\Factory\LazyLoadingValueHolderFactory#3759 (3) {
      │         ...
      │       }
      │     }
      │     public $parameter =>
      │     array(5) {
      │       '&$wrappedObject' =>
      │       string(10) "<required>"
      │       '$proxy' =>
      │       string(10) "<required>"
      │       '$method' =>
      │       string(10) "<required>"
      │       '$parameters' =>
      │       string(10) "<required>"
      │       '&$initializer' =>
      │       string(10) "<required>"
      │     }
      │   }
      │ }
DonCallisto commented 6 years ago

As you can see, LoginPage is injected in the right way. Are you sure that you're not overriding its value elsewhere?

nietzscheson commented 6 years ago

I'm not sure. I only have those classes

nietzscheson commented 6 years ago

if modify this line:

public function open(array $urlParameters = array())
{
  $url = $this->getUrl($urlParameters);

  $this->getDriver()->visit($url);

  $this->verify($urlParameters);

  return $this;
}

For this, works:

public function open(array $urlParameters = array())
{
  $url = $this->getUrl($urlParameters);

  $this->getSession()->visit($url);

  $this->verify($urlParameters);

  return $this;
}

It's as if I was not able to resolve the driver to handle the session ... with the session directly, everything is fine ...

nietzscheson commented 6 years ago

Update the composer with composer update --prefer-stable and works:

behat/behat                                     v3.4.1             Scenario-oriented BDD framework for PHP 5.3
behat/gherkin                                   v4.5.1             Gherkin DSL parser for PHP 5.3
behat/mink                                      dev-master 9ea1ceb Browser controller/emulator abstraction for PHP
behat/mink-browserkit-driver                    dev-master 1c9c8ad Symfony2 BrowserKit driver for Mink framework
behat/mink-extension                            v2.2               Mink extension for Behat
behat/mink-selenium2-driver                     dev-master 46404b0 Selenium2 (WebDriver) driver for Mink framework
behat/symfony2-extension                        dev-master 319b56c Symfony2 framework extension for Behat

New Composer:

behat/behat                                     v3.4.1             Scenario-oriented BDD framework for PHP 5.3
behat/gherkin                                   v4.5.1             Gherkin DSL parser for PHP 5.3
behat/mink                                      v1.7.1             Browser controller/emulator abstraction for PHP
behat/mink-browserkit-driver                    v1.3.2             Symfony2 BrowserKit driver for Mink framework
behat/mink-extension                            v2.2               Mink extension for Behat
behat/mink-selenium2-driver                     v1.3.1             Selenium2 (WebDriver) driver for Mink framework
behat/symfony2-extension                        2.1.1              Symfony2 framework extension for Behat
jakzal commented 6 years ago

Perhaps it's related to some non-BC changes in mink, like #92.