View source
<?php
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use Behat\Behat\Context\ClosuredContextInterface, Behat\Behat\Context\TranslatedContextInterface, Behat\Behat\Event\ScenarioEvent, Behat\Behat\Exception\PendingException;
use Drupal\Component\Utility\Random;
require 'vendor/autoload.php';
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext {
public function __construct() {
}
public function iShouldSeeTheFollowingTexts(TableNode $table) {
$page = $this
->getSession()
->getPage();
$table = $table
->getHash();
foreach ($table as $key => $value) {
$text = $table[$key]['texts'];
if ($page
->hasContent($text) === FALSE) {
throw new Exception("The text '" . $text . "' was not found");
}
}
}
protected function randomString($number = 10) {
return 'abcdefghijk';
}
public function iShouldSeeTheFollowingLinks(TableNode $table) {
$page = $this
->getSession()
->getPage();
$table = $table
->getHash();
foreach ($table as $key => $value) {
$link = $table[$key]['links'];
$result = $page
->findLink($link);
if (empty($result)) {
throw new Exception("The link '" . $link . "' was not found");
}
}
}
public function iShouldNotSeeTheFollowingLinks(TableNode $table) {
$page = $this
->getSession()
->getPage();
$table = $table
->getHash();
foreach ($table as $key => $value) {
$link = $table[$key]['links'];
$result = $page
->findLink($link);
if (!empty($result)) {
throw new Exception("The link '" . $link . "' was found");
}
}
}
public function theFieldShouldBeOutlinedInRed($field) {
$page = $this
->getSession()
->getPage();
$formField = $page
->findField($field);
if (empty($formField)) {
throw new Exception('The page does not have the field with label "' . $field . '"');
}
$class = $formField
->getAttribute("class");
$class = explode(" ", $class);
if (!in_array("error", $class)) {
throw new Exception('The field "' . $field . '" is not outlined with red');
}
}
public function fetchUserDetails($type, $name) {
$property_name = $type . '_users';
try {
$property = $this->{$property_name};
$details = $property[$name];
return $details;
} catch (Exception $e) {
throw new Exception("Non-existant user/password for {$property_name}:{$name} please check behat.local.yml.");
}
}
public function iAmLoggedInAsWithThePassword($username, $passwd) {
$user = $this
->whoami();
if (strtolower($user) == strtolower($username)) {
return;
}
$element = $this
->getSession()
->getPage();
if (empty($element)) {
throw new Exception('Page not found');
}
$this
->getSession()
->visit($this
->locatePath('/user/login'));
$element
->fillField('Username', $username);
$element
->fillField('Password', $passwd);
$submit = $element
->findButton('Log in');
if (empty($submit)) {
throw new Exception('No submit button at ' . $this
->getSession()
->getCurrentUrl());
}
$submit
->click();
$user = $this
->whoami();
if (strtolower($user) != strtolower($username)) {
throw new Exception('Could not log user in.');
}
return;
}
private function whoami() {
$element = $this
->getSession()
->getPage();
$this
->getSession()
->visit($this
->locatePath('/user'));
if ($find = $element
->find('css', 'h1')) {
$page_title = $find
->getText();
if ($page_title) {
return str_replace('hello, ', '', strtolower($page_title));
}
}
return FALSE;
}
public function clickOnQuickEdit() {
$this
->getSession()
->getPage()
->clickLink('Quick edit');
$this
->getSession()
->wait(5000, 'jQuery(".entity-commerce-order").length > 0');
}
public function iWaitForAJAX() {
$this
->getSession()
->wait(5000, 'jQuery.active === 0');
}
public function iWaitForSeconds($arg1) {
sleep($arg1);
}
public function afterScenarioVariableCleanUp(\Behat\Behat\Hook\Scope\AfterScenarioScope $scope) {
variable_del('commerce_kickstart_user_breadcrumbs');
}
public function userBreadcrumbsAreEnabled() {
$this
->getDrupal();
variable_set('commerce_kickstart_user_breadcrumbs', TRUE);
}
public function iResizeTheBrowserToMobile() {
$this
->getSession()
->resizeWindow(200, 600, 'current');
}
public function iSelectFromCollectionDropdown($arg1) {
$this
->getSession()
->wait(5000, 'jQuery("#selectnav3").length > 0');
$this
->getSession()
->getPage()
->selectFieldOption("selectnav3", $arg1);
}
public function beforeScenario() {
if (!$this
->runningJavascript()) {
return;
}
$this
->getSession()
->resizeWindow(1440, 900, 'current');
}
protected function runningJavascript() {
return get_class($this
->getSession()
->getDriver()) !== 'Behat\\Mink\\Driver\\GoutteDriver';
}
public function assertSelectRadioByLabel($label, $id = '') {
$element = $this
->getSession()
->getPage();
$radiobutton = $id ? $element
->findById($id) : $element
->find('named', array(
'radio',
$this
->getSession()
->getSelectorsHandler()
->xpathLiteral($label),
));
if ($radiobutton === NULL) {
throw new \Exception(sprintf('The radio button with "%s" was not found on the page %s', $id ? $id : $label, $this
->getSession()
->getCurrentUrl()));
}
$radio_id = $radiobutton
->getAttribute('id');
$labelonpage = $element
->find('css', "label[for='{$radio_id}']");
if ($label != $labelonpage
->getText()) {
throw new \Exception(sprintf("Button with id '%s' has label '%s' instead of '%s' on the page %s", $id, $labelonpage, $label, $this
->getSession()
->getCurrentUrl()));
}
$labelonpage
->click();
}
}