Behat Testing Classes

Basically, there are two main files  DOMAIN_name.feature  and FeatureContext.php. DOMAIN_name.feature file describes how your application behaves and FeatureContext.php class is all about how to test it. Context classes are the main point of a testing environment in Behat. The context class is based on Plain Old PHP Object.

DOMAIN_name.feature
Feature: Whole Website Testing
  As a website visitor
  I want to see the homepage and other pages
  So that I can understand what the website offers and how it can benefit me
@javascript
Scenario: Testing Homepage
  Given I am on "Home Page"
  And wait for the page to load



When you run above code then compiler give a suggestion of function iAmOn and waitForThePageToLoad that is to be written in Context classes.
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\RawMinkContext;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends RawMinkContext implements Context, SnippetAcceptingContext {
     public function __construct() {  
      }
    public function getElementsByClassName($elements, $className) {
        $matches = array();
        foreach ($elements as $element) {
            if (!$element->hasAttribute('class')) {
                continue;
            }
            $classes = preg_split('/\s+/', $element->getAttribute('class'));
            if (!in_array($className, $classes)) {
                continue;
            }
            $matches[] = $element;
        }
        return $matches;
    }

    public function iAmOn($arg1){
        $this->visitPath('/');
        echo 'I am able to see homepage.';
    }
    public function waitForThePageToLoad() {
      $this->getSession()->wait('10000');
    }

}
Now You can write definition iAmOn and waitForThePageToLoad .
Login By Behat:
DOMAIN_name.feature
Then I want to login as "loginid" and "loginpassword" 

FeatureContext.php
public function iWantToLoginAsAnd($email, $password) {
        $page = $this->getSession()->getPage();

        $page->find('css', '#loginBoxPopUpId')->click();
        $this->getSession()->wait(2000, "document.readyState === 'complete'");

        if ($page->find('css', '#newLoginPopupid')->isVisible()) {
            $this->getSession()->getDriver()->setValue('//*[@id="login-email"]', $email);
            $this->getSession()->getDriver()->setValue('//*[@id="pass"]', $password);
            $page->find('css', '#loginbtnid')->click();
            $this->getSession()->wait(10000);

            if ($page->find('css', '#popup-error span') && $page->find('css', '#popup-error span')->getHtml()) {
                throw new \LogicException($page->find('css', '#popup-error span')->getHtml());
            } else {
                echo 'I am able to login with the given credential user: ' . $email . " and password: " . $password;
            }
        }
    }

Comments