Search code examples
behatminkpageobjects

Behat/Mink/PageObjects - page object class not found


Setting up testing with behat for my project... I got most of the things working - except the page objects. Here's my behat.yml

default:
  autoload: [%paths.base%/boostrap]
  suites:
    web:
      paths: [%paths.base%/features/web]
      contexts:
        - Web\LoginContext
    api:
      paths: [%paths.base%/features/api]
      contexts:
        - Api\ApiContext
  extensions:
    Behat\MinkExtension:
      base_url: http://my.url.com
      selenium2: ~
    SensioLabs\Behat\PageObjectExtension:
      namespaces:
        page: [Page]
        element: [Page\Element]

I am following the PageObjectExtention documentation. I have created LoginPage class, which extends from Page and updated my context file to use page injection.

My LoginPage class looks like this:

<?php

namespace Page;

use SensioLabs\Behat\PageObjectExtension\PageObject\Page;

class LoginPage extends Page
{

}

and my Context file looks like this:

namespace Web;

use Behat\Behat\Tester\Exception\PendingException;
use Page\LoginPage;

class LoginContext extends MednavMinkContext
{
    private $loginPage;

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

    ...
}

Yet, when I try to run it, I get

[ReflectionException]                
Class Page\LoginPage does not exist  

Where should these page objects be stored? I tried all possible locations - under Page inside boostrap, under Page in the root, and a couple of other - no luck.


Solution

  • I did eventually resolved the issue, using the autoload section in my composer.json, which now looks like this:

    {
        "require": {
            "behat/behat": "3.*@stable",
            "behat/mink": "1.6.*@stable",
            "behat/mink-extension": "@stable",
            "behat/mink-selenium2-driver": "@stable",
            "sensiolabs/behat-page-object-extension": "@stable"
        },
        "require-dev": {
            "phpunit/phpunit": "@stable",
            "bossa/phpspec2-expect": "@stable"
        },
        "config": {
            "bin-dir": "bin/"
        },
        "autoload": {
            "psr-4": {
                "": "lib/"
            }
        }
    }
    

    Now, inside my lib directory, I have the usual directory structure based on namespaces:

    lib/
        MyCompany/
            Util/
            Page/
            Config/
    

    and so on. Finally, in my behat.yml file, I specify the namespace for the page objects:

    SensioLabs\Behat\PageObjectExtension:
      namespaces:
        page: [MyCompany\Page]
        element: [MyCompany\Page\Element]