0
votes

I'm new to Behat and incorporating page objects through Selenium using php facebook/webriver extension. I've used Java/testNG with selenium webdriver using the page object model to Automate web applications in the past but the setup with Behat and php I'm just having issues with.

So what I want to do is instead of using the selenium2driver with mink, I want to incorporate the the facebook/webdriver with a format similar to this for my project:

Project
|-bin(folder)
|-features(folder)
   --NewFeature.Feature
  |--bootstrap(folder)
    --FeatureContext.php
   |--Page(folder)
    ---HomePage.php
    ---RegistrationPage.php
|-vendor

I want all the pages to have their own class, and to be able to call each one from the FeatureContext.php file; so I can keep it as clean as possible.

My composer looks like this:

"require": {
    "facebook/webdriver": "~1.0",
    "behat/behat": "3.4.2",
    "behat/mink-goutte-driver" : "*",
    "behat/mink-selenium2-driver": "1.3.1",
    "sensiolabs/behat-page-object-extension": "^2.0",
    "behat/mink": "1.7.1"
},
"config": {
    "bin-dir": "bin/"
}, 

and my behat.yml is similar to this

default:
 extensions:
    SensioLabs\Behat\PageObjectExtension: ~
    Behat\MinkExtension:
        base_url: https://myurl.com
        selenium2:
            wd_host: localhost:4444/wd/hub

I'm not really sure it's it's possible to initiate webdriver(or just navigate to a page) within the pages using the page object extension or not, I can get firefox to launch through selenium-server-standalone-3.8.1.jar, but I'm not sure how to implement the page files correctly so it will read.

Featurecontext file:

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Page\Homepage;
/**
 * Defines application features from the specific context.
 */
class FeatureContext implements Context
{
private $homepage;
/**
 * Initializes context.
 *
 * Every scenario gets its own context instance.
 * You can also pass arbitrary arguments to the
 * context constructor through behat.yml.
 */
public function __construct(Homepage $homepage)
{
    $this->homepage = $homepage;
}

/**
 * @Given \/^(?:|I )visited homepage$\/
 */
public function iVisitedHomepage()
{
    $this->homepage->navigateToSite();
}

}

HomePage file setup:

use SensioLabs\Behat\PageObjectExtension\PageObject\Page;
use Facebook\WebDriver\Remote\DesiredCapabilities,
    Facebook\WebDriver\Remote\RemoteWebDriver;
use Behat\Mink\Mink,
    Behat\Mink\Session,
    Behat\Mink\Driver\Selenium2Driver;

class Homepage extends Page{

protected $path = '/';


//setup facebookwebdriver

public function navigateToSite(){
      //facebook webdriver code
}
}

Just not sure if I'm even on the right track or if I should create a new BasePage class that setups the driver separately? and how would I format behat.yml so it would know to look for the files correctly?

1

1 Answers

0
votes

Of what I know by using Mink extension you are actually using a fork of PHP Facebook\WebDriver.

If you look into Vendor folder you should see an "instalclick" folder that contains the "WebDriver" used by MinkExtension.

When you told behat in config.yaml to use "Behat\MinkExtension:" a driver is created and available in behat context base of "instalclick" and not the base of actual "Facebook\WebDriver\".

I have never used 'SensioLabs\Behat\PageObjectExtension' but my assumption is an instance of the mink session and driver are available automatically (they are initialized in base class). So you just start and code your classes implementation.