2

I've a project running with Behat 2.4, Mink 1.4 and Behat Page Object Extension, with this version my tests is fine, 100% passed.

But now i'm migrating to Behat 3 due to the fully integration with Browserstack, Behat 2 doesn't support BrowserStack flags and the integration is poor.

I've changed my composer file and I updated project, but when I run the tests, it is returning an Exception on Page Object Extension.

To create pages you need to pass a factory with setPageObjectFactory() (RuntimeException)

Looking at Page Object Docs I don't see anything about setPageObjectFatory, this isn't needed. In configuration section, only specifies factory if you create a custom factory or/and custom class name resolver.

My composer with all dependencies is

{
    "require-dev" : {
        "behat/behat" : "master-dev",
        "behat/mink-goutte-driver" : "master-dev",
        "behat/mink-browserkit-driver" : "master-dev",
        "sensiolabs/behat-page-object-extension" : "master-dev",
        "behat/mink-extension" : "master-dev",
        "behat/mink-selenium2-driver" : "master-dev",
        "behat/mink" : "master-dev"
    }
}

And my behat.yml bellow

default:
  suites:
    default:
      contexts:
        - FeatureContext
        - ProductDetailsContext
        - CartContext
      extensions:
        SensioLabs\Behat\PageObjectExtension:
          namespaces:
            page: [Features\Page]
            element: [Features\Page\Element]
        Behat\MinkExtension:
          sessions:
            my_session:
              browser_stack:
                username: my_username
                access_key: my_password
                capabilities: 
                  browserName: "Chrome"
                  browserVersion: "35"
                  platform: "WIN8"

My FeatureContext extending MinkContext

<?php

use Behat\MinkExtension\Context\MinkContext;

/**
 * Behat context class.
 */
class FeatureContext extends MinkContext
{
}

And ProductDetailsContext extending PageObjectContext

<?php
use SensioLabs\Behat\PageObjectExtension\Context\PageObjectContext;

/**
 *
 *
 */
class ProductDetailsContext extends PageObjectContext
{
    /**
     * @Given /^I am on product details "([^"]*)"$/
     *
     * @param string $url
     */
    public function iAmOnProductDetails($url)
    {
        $this->getPage("ProductDetails")->open(array("productUrl" => $url));
    }

    /**
     * @Given /^I am at a random product details$/
     */
    public function iAmAtARandomProductDetails()
    {
        $catalog = $this->getPage("Catalog");
        $catalog->open(array('category' => 'calcados-femininos'));
        $catalog->openRandomProduct();
    }

    /**
     * @When /^I select product size$/
     */
    public function iSelectProductSize()
    {
        $this->getPage("ProductDetails")->selectProductSize();
    }

    /**
     * @Then /^I add product to cart$/
     */
    public function iAddProductToCart() {
        $this->getPage("ProductDetails")->addProductToCart();
    }

    /**
     * @Then /^I add product to wishlist$/
     */
    public function iAddProductToWishlist()
    {
        $this->getPage("ProductDetails")->addProductToWishlist();
    }
}

I don't know how can solve this and I need help.

Thiago França
  • 1,817
  • 1
  • 15
  • 20
  • 1
    Which context throws the exception (see the stack trace)? Does it extend/implement one of the page object contexts? – Jakub Zalas Aug 22 '14 at 10:51
  • @JakubZalas I updated my question with the Contexts. – Thiago França Aug 25 '14 at 19:09
  • I though you had this fixed? What happened to that? – Ian Bytchek Aug 25 '14 at 20:02
  • @IanBytchek After I've been added `FeatureContext` in `behat.yml`, it started throws another Exception `Call to a member function getSession() on a non-object in vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 101`, i solved this then it is started throw the same Exception again `To create pages you need to pass a factory with setPageObjectFactory() (RuntimeException)`... – Thiago França Aug 25 '14 at 20:20
  • I was going to suggest changing all composer deps to `dev-master`. V3 is under heavy development as well as related packages. It helped me in the past when things weren't as stable, I looked into your issue a few days ago, didn't come up with anything better still… – Ian Bytchek Aug 25 '14 at 20:25
  • And… another idea – put your ProductDetailsContext first on the list. It might be the case that it must be the first one to get initiated to inject the `pageObject` instance. – Ian Bytchek Aug 25 '14 at 20:28
  • @IanBytchek I changed all deps to dev-master and ProductDetailsContext to the first position on the list, but i've the same Exception. – Thiago França Aug 25 '14 at 21:10

3 Answers3

2

The problem is because Extensions was configured inside of suites, put the Extensions outside suites and it works.

default:
  suites:
    default:
      contexts:
        - FeatureContext
        - ProductDetailsContext
        - CartContext
  extensions:
    SensioLabs\Behat\PageObjectExtension:
      namespaces:
        page: [Features\Page]
        element: [Features\Page\Element]
      Behat\MinkExtension:
        sessions:
          my_session:
            browser_stack:
              username: my_username
              access_key: my_password
              capabilities: 
                browserName: "Chrome"
                browserVersion: "35"
                platform: "WIN8"
Thiago França
  • 1,817
  • 1
  • 15
  • 20
1

This is a long shot, but here we go… In previous Behat version you've used single / root context, which must have extended PageObjectContext. It must have been the only / first context that was initialised, it was also the right context to initialise page factory and the pages themselves. Now you have multiple contexts, the logical step would be to ensure that they all implement SensioLabs\Behat\PageObjectExtension\Context\PageObjectAwareInterface as said in the docs here.

I also don't see 'SensioLabs\Behat\PageObjectExtension' entry under extensions. I don't think Behat would initialise the extension without it being on the list (it can't just randomly guess that it must be loaded, right?). This is probably the first thing you should change. Based on the docs about configuration and given you followed the default convention, everything should work.

Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72
  • At time when I posted I was making changes trying to solve and ended up posting a misconfiguration, now updated the question with the config i'd use, so sorry. In the previous version of `Behat` i wore the same structure, with the same contexts, no changes between two versions therefore i think this is not caused by multiple contexts. About `PageObjectAware` interface, I dont need to implement it in my Contexts, because they extend `PageObjectContext` and `PageObjectContext` implement `PageObjectAware`. Nothing solved. – Thiago França Aug 25 '14 at 22:52
  • I'm not stating, but prior v3 you couldn't specify multiple contexts, only "include" them in the main context. Your `FeatureContext` extends `MinkContext`, which has nothing to do with `PageObjectContext` and doesn't implement `PageObjectAware`, but as said, I'm not 100% sure it's the cause. – Ian Bytchek Aug 25 '14 at 22:59
  • It would also help if you debugged this and posted the complete stack trace or at least described at what step of what scenario this happens. Try running that scenario in separation using config options? Does it work then? – Ian Bytchek Aug 25 '14 at 23:02
  • now I understand about the Contexts you spoke. In the previous version i'd used `useContext` method to append SubContexts to my FeatureContext and now it is on config. – Thiago França Aug 25 '14 at 23:05
  • Yep :) but I'm just random guessing, might not be the issue at all, I'm sure this would be mentioned on the docs. – Ian Bytchek Aug 25 '14 at 23:07
  • @IanBytcheck I started an empty project to trying to solve this, with only one feature, and when i call `$this->getPage` from any Context i get the Exception... If I run with Behat/Mink/MinkExtension it is ok but when i use PageObject doesn't. – Thiago França Aug 25 '14 at 23:11
  • Start a bounty to make this more interesting and share the dummy project on the Github? :) – Ian Bytchek Aug 25 '14 at 23:17
  • @IanBytcheck I figured out the error, I've configured `Extensions` inside of `suites`, when I put `Extensions` to outside it worked. Now i only have problem with the `Page` namespaces. Thanks a lot. – Thiago França Aug 26 '14 at 13:42
  • Ha, that was the obvious one after all! :) – Ian Bytchek Aug 26 '14 at 14:07
  • Yep, was the obvious and now i run my jobs perfectly! – Thiago França Aug 26 '14 at 14:21
0

Try to install

composer require --dev --ignore-platform-reqs sensiolabs/behat-page-object-extension:^2.0

In the composer I've add --ignore-platform-reqs because i'm use php 7.* for my works

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111