Behat Mink: 05 - Scenario Outlines

 

Scenario outlines enumerate methods from the Examples table:


@tag
Feature: Title of your feature
  I want to use this template for my feature file

  #@tag1
  #Scenario: Data Driven Testing1
    #Given I have data
#        When I use data
#        Then I receive data

  @tag2
  Scenario Outline: Data Driven Testing
    Given I have data for <name> field
        When I use data <value> for entry
        Then I receive data <status> back from for
        
    Examples:
      | name  | value | status  |
      | name1 |     5 | success |
      | name2 |     7 | Fail    |
      | name3 |     8 | success |

FeatureContext.php

  •  
<?php

use Drupal\DrupalExtension\Context\RawDrupalContext;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Tester\Exception\PendingException;
use PHPUnit\Framework;
use phpDocumentor\Reflection\Types\Boolean;
use PHPUnit\Framework\Assert;
use Symfony\Component\CssSelector\XPath\XPathExpr;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext {

  /**
   * Initializes context.
   *
   * Every scenario gets its own context instance.
   * You can also pass arbitrary arguments to the
   * context constructor through behat.yml.
   */

    private $shelf;
    private $basket;
    private $driver;

    
      public function __construct() {
          $this->shelf = new Shelf();
          $this->basket = new Basket($this->shelf);
      }
    
    /**
     * @Given there is a :product, which costs $:price
     */
    public function thereIsAWhichCosts($product, $price)
    {
        $this->shelf->setProductPrice($product, floatval($price));
    }

    /**
     * @When I add the :product to the basket
     */
    public function iAddTheToTheBasket($product)
    {
        $this->basket->addProduct($product);
    }

    /**
     * @Then I should have :count product in the basket
     */
    public function iShouldHaveProductInTheBasket($count)
    {
        
    $basketCount = $this->basket->count();
    $result = boolval(intval($count) == $basketCount);
     Assert::assertTrue($result);
    }

    /**
     * @Then the overall basket price should be $:price
     */
    public function theOverallBasketPriceShouldBe($price)
    {
        Assert::assertSame(floatval($price),$this->basket->getTotalPrice());
    }

    /**
     * @Then I should have :count products in the basket
     */
    public function iShouldHaveProductsInTheBasket($count)
    {
        $basketCount = $this->basket->count();
        $result = boolval(intval($count) == $basketCount);
        Assert::assertTrue($result);
    }

    /**
     * @Then PageTitle should be :pagetitle
     */
    public function pagetitleShouldBe($pagetitle)
    {
        $element = null;
        $element = $this->getSession()->getPage()->find('xpath','//a[contains(.,"Log")]');
        print "Getting locator for: '" . $pagetitle . "' : | getXpath = " . $element->getXpath() . "\xA";
        //$result = null;
        Assert::assertNotEmpty($element);
        print "Locator found = : ". $element->getXpath();
        //Assert::assertTrue(true);
    }
    
    /**
     * @Given I have data for :name field
     */
    public function iHaveDataForNameField($name)
    {
        print "Name: " . $name;
        Assert::assertTrue(true);
    }
    
    /**
     * @When I use data :value for entry
     */
    public function iUseDataForEntry($value)
    {
        print "Value: " . $value;
        Assert::assertTrue(true);
    }
    
    /**
     * @Then I receive data :status back from for
     */
    public function iReceiveDataSuccessBackFromFor($status)
    {
        print "Status: " . $status;
        Assert::assertTrue(true);
    }

}

Behat Runner Results

T:\git\behatmink (master -> origin)
λ bin\behat
Feature: Product basket
  In order to buy products
  As a customer
  I need to be able to put interesting products into a basket

  Rules:
  - VAT is 20%
  - Delivery for basket under $10 is $3
  - Delivery for basket over $10 is $2

  Scenario: Buying a single product under $10               # features\basket.feature:11
    Given there is a "Sith Lord Lightsaber", which costs $5 # FeatureContext::thereIsAWhichCosts()
    When I add the "Sith Lord Lightsaber" to the basket     # FeatureContext::iAddTheToTheBasket()
    Then I should have 1 product in the basket              # FeatureContext::iShouldHaveProductInTheBasket()
    And the overall basket price should be $9               # FeatureContext::theOverallBasketPriceShouldBe()

  Scenario: Buying a single product over $10                 # features\basket.feature:17
    Given there is a "Sith Lord Lightsaber", which costs $15 # FeatureContext::thereIsAWhichCosts()
    When I add the "Sith Lord Lightsaber" to the basket      # FeatureContext::iAddTheToTheBasket()
    Then I should have 1 product in the basket               # FeatureContext::iShouldHaveProductInTheBasket()
    And the overall basket price should be $20               # FeatureContext::theOverallBasketPriceShouldBe()

  Scenario: Buying two products over $10                     # features\basket.feature:23
    Given there is a "Sith Lord Lightsaber", which costs $10 # FeatureContext::thereIsAWhichCosts()
    And there is a "Jedi Lightsaber", which costs $5         # FeatureContext::thereIsAWhichCosts()
    When I add the "Sith Lord Lightsaber" to the basket      # FeatureContext::iAddTheToTheBasket()
    And I add the "Jedi Lightsaber" to the basket            # FeatureContext::iAddTheToTheBasket()
    Then I should have 2 products in the basket              # FeatureContext::iShouldHaveProductsInTheBasket()
    And the overall basket price should be $20               # FeatureContext::theOverallBasketPriceShouldBe()

@tag
Feature: Title of your feature
  I want to use this template for my feature file

  @tag2
  Scenario Outline: Data Driven Testing        # features\datadriven.feature:12
    Given I have data for <name> field         # FeatureContext::iHaveDataForNameField()
    When I use data <value> for entry          # FeatureContext::iUseDataForEntry()
    Then I receive data <status> back from for # FeatureContext::iReceiveDataSuccessBackFromFor()

    Examples:
      | name  | value | status  |
      | name1 | 5     | success |
        │ Name: name1
        │ Value: 5
        │ Status: success
      | name2 | 7     | Fail    |
        │ Name: name2
        │ Value: 7
        │ Status: Fail
      | name3 | 8     | success |
        │ Name: name3
        │ Value: 8
        │ Status: success

Feature: Testing Footer Objects
  As an user, I want to be able to interact
  with the footer objects

  Scenario: Home Page Title            # features\footer.feature:5
    Given I am on "/"                  # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I should see "Drupal Testing" # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

  Scenario: Footer Validation                        # features\footer.feature:10
    Given I am on "/"                                # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I should see "2018" in the "copyright"      # Drupal\DrupalExtension\Context\MinkContext::assertRegionText()
    Then I should see "Copyright" in the "copyright" # Drupal\DrupalExtension\Context\MinkContext::assertRegionText()
    When I click "Home" in the "main_menu"           # Drupal\DrupalExtension\Context\MinkContext::assertRegionLinkFollow()
    Then I should see "Home" in the "main_menu"      # Drupal\DrupalExtension\Context\MinkContext::assertRegionText()

Feature: TestIng Home Page conent
  As an user, I want to be able to test
  the home page text

  Scenario: Home Page Title            # features\home.feature:5
    Given I am on "/"                  # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I should see "Drupal Testing" # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

  Scenario: Footer Validation     # features\home.feature:10
    Given I am on "/"             # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I should see "Copyright" # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()
    And I should see "2018"       # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

Feature: Login Validation
  As an anonymouse user and
  as an authenticated user
  I want to use the login feature

  Scenario: Invalid User Cannot Login      # features\login.feature:6
    Given I am on "/user/login"            # Drupal\DrupalExtension\Context\MinkContext::visit()
    When I fill in "edit-name" with "test" # Drupal\DrupalExtension\Context\MinkContext::fillField()
    And I fill in "edit-pass" with "test"  # Drupal\DrupalExtension\Context\MinkContext::fillField()
    And I press "edit-submit"              # Drupal\DrupalExtension\Context\MinkContext::pressButton()
    Then I should see "Forgot"             # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

  Scenario: Valid User Can Login           # features\login.feature:14
    Given I am on "/user/login"            # Drupal\DrupalExtension\Context\MinkContext::visit()
    When I fill in "edit-name" with "test" # Drupal\DrupalExtension\Context\MinkContext::fillField()
    And I fill in "edit-pass" with "test"  # Drupal\DrupalExtension\Context\MinkContext::fillField()
    And I press "edit-submit"              # Drupal\DrupalExtension\Context\MinkContext::pressButton()
    Then I should see "Success"            # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()
      The text "Success" was not found anywhere in the text of the current page. (Behat\Mink\Exception\ResponseTextException)

Feature: Testing Home Page Search
  As an user, I want to be able to perform
  search on the home page

  Scenario: Search fill                              # features\search.feature:5
    Given I am on "/"                                # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I fill in "edit-keys" with "UnityConstruct" # Drupal\DrupalExtension\Context\MinkContext::fillField()
    Then I press "edit-submit"                       # Drupal\DrupalExtension\Context\MinkContext::pressButton()
    Then I should see "UnityConstruct"               # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

  Scenario: Search no result                            # features\search.feature:12
    Given I am on "/"                                   # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I fill in "edit-keys" with "zxysdfjlkwer"      # Drupal\DrupalExtension\Context\MinkContext::fillField()
    Then I press "edit-submit"                          # Drupal\DrupalExtension\Context\MinkContext::pressButton()
    Then I should see "zxysdfjlkwer"                    # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()
    Then I should see "Your search yielded no results." # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

  Scenario: Search with result                       # features\search.feature:20
    Given I am on "/"                                # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I fill in "edit-keys" with "Drupal Testing" # Drupal\DrupalExtension\Context\MinkContext::fillField()
    Then I press "edit-submit"                       # Drupal\DrupalExtension\Context\MinkContext::pressButton()
    Then I should see "Drupal Testing"               # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()

Feature: Testing Selenium2 Driver Hooks
  As an user, I want to be able to test
  Selenium2 driver hooks

  Scenario: Home Page Title                          # features\selenium2.feature:5
    Given I am on "/"                                # Drupal\DrupalExtension\Context\MinkContext::visit()
    Then I should see "Drupal Testing"               # Drupal\DrupalExtension\Context\MinkContext::assertPageContainsText()
    Then PageTitle should be "Home | UnityConstruct" # FeatureContext::pagetitleShouldBe()
      │ Getting locator for: 'Home | UnityConstruct' : | getXpath = (//html//a[contains(.,"Log")])[1]
      │ Locator found = : (//html//a[contains(.,"Log")])[1]

--- Failed scenarios:

    features\login.feature:14

16 scenarios (15 passed, 1 failed)
61 steps (60 passed, 1 failed)
0m8.30s (11.80Mb)