Getting started (1) git clone [email protected]:pavlakis/phpnw15-behat-tutorial.git (2) vagrant up (3) Add the following to your hosts file: 192.168.42.200 dev.legacy.com test.legacy.com
selenium2: wd_host: "http://localhost:8643/wd/hub" capabilities: { "browser": "phantomjs" } Ensure the URL is accessible from your server. (e.g. in your hosts file) /var/www/tests/behat/behat.yml
Features & Scenarios Behat tests live inside .feature files Behat tests are called Scenarios Behat Scenarios use the Gherkin language, which allows us to write English statements.
Definitions A statement from a scenario that maps to a PHP method and identified through the method’s doc-block. For available definitions, run: $ ./behat --config=/var/www/tests/behat/behat.yml -dl
“Scenario: Page does not exist” Feature: Albums I can create, edit and delete albums
Scenario: The homepage loads Given I am on the homepage Then I should see text matching "My Album” Scenario: Page does not exist Given I … Then I … tests/behat/features/bootstrap/album.feature
“Scenario: I can create an album” Feature: Albums I can create, edit and delete albums
Scenario: The homepage loads Given I am on the homepage Then I should see text matching “My Album” Scenario: Page does not exist Given I … Then I … Scenario: I can create an album Given I am on the homepage And I follow … tests/behat/features/bootstrap/album.feature
“Scenario: I can create an album” tests/behat/features/bootstrap/album.feature Guidance. Use the following definitions: And /^(?:|I )follow "(?P(?:[^"]|\\")*)"$/ And /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P(?:[^"]|\\")*)"$/ And /^(?:|I )press "(?P(?:[^"]|\\")*)"$/
“A solution…” Feature: Albums I can create, edit and delete albums
Scenario: The homepage loads Given I am on the homepage Then I should see text matching "My Album"
Scenario: Page does not exist Given I am on "/notexist" Then I should see text matching "Page not found"
Scenario: Can create an album Given I am on the homepage And I follow "Add new album" And I fill in "artist" with "Queen" And I fill in "title" with “A kind of magic" And I press "Add" Then I should see text matching "A kind of magic" And I should be on the homepage
Scenario: Can edit an album Given I am on the homepage And I follow "Edit" And I fill in "artist" with “Billy Joel" And I fill in "title" with “An Innocent Man" And I press "Save" Then I should see text matching “An Innocent Man" And I should be on the homepage tests/behat/features/bootstrap/album.feature
Custom step $ legacybehat ..................U------ 5 scenarios (4 passed, 1 undefined) 25 steps (18 passed, 1 undefined, 6 skipped) 0m0.28s (14.25Mb) --- FeatureContext has missing steps. Define them with these snippets: /** * @Given I am on the add page */ public function iAmOnTheAddPage() { throw new PendingException(); } terminal
Custom step definition /** * @Given I am on the add page */ public function iAmOnTheAddPage() { throw new PendingException(); } tests/behat/features/bootstrap/FeatureContext.php
Custom step definition /** * @Given I am on the add page */ public function iAmOnTheAddPage() { $this->visit('/index/add'); } tests/behat/features/bootstrap/FeatureContext.php
First iteration Scenario: Can create a default album Given I am on the add page And I fill in "artist" with "test123" And I fill in "title" with "test321" And I press "Add" Then I should see text matching "test123" And I should be on the homepage tests/behat/features/bootstrap/FeatureContext.php
Second iteration Scenario: Can create a default album Given I am on the add page And I create a default album Then The album has been created tests/behat/features/bootstrap/FeatureContext.php
Exercise #6 Guidance. Use the following methods: $this->fillField($field, $value); $this->pressButton($button); $this->assertPageContainsText($text); tests/behat/features/bootstrap/FeatureContext.php
Can edit a specific album /** * @Given I go to edit album by title :title */ public function iGoToEditAlbumByTitle($title) { $page = $this->getMink()->getSession()->getPage();
Can edit a specific album /** * @Given I go to edit album by title :title */ public function iGoToEditAlbumByTitle($title) { $page = $this->getMink()->getSession()->getPage();
Before Feature use Behat\Behat\Hook\Scope\BeforeFeatureScope; … /** @BeforeFeature */ public static function prepareForTheFeature(BeforeFeatureScope $scope) {
Connect to DB public function __construct($username, $password, $dbName) { $this->db = new \PDO("mysql:host=127.0.0.1;dbname={$dbName}", $username, $password); } tests/behat/features/bootstrap/FeatureContext.php
Tags - Features and Scenarios @db Feature: Albums I can create, edit and delete albums
Scenario: Can create an album Given I am on the homepage And I follow "Add new album" And I fill in "artist" with "Queen" And I fill in "title" with “A kind of magic" And I press "Add" Then I should see text matching "A kind of magic" And I should be on the homepage @db Scenario: Can edit an album Given I am on the homepage And I follow "Edit" And I fill in "artist" with “Billy Joel" And I fill in "title" with “An Innocent Man" And I press "Save" Then I should see text matching “An Innocent Man" And I should be on the homepage tests/behat/features/bootstrap/album.feature
“A Solution…” @db Feature: Albums I can create, edit and delete albums
Scenario: Can create an album Given I am on the homepage And I follow "Add new album" And I fill in "artist" with "Queen" And I fill in "title" with “A kind of magic" And I press "Add" Then I should see text matching "A kind of magic" And I should be on the homepage @defaultAlbumFixture Scenario: Can edit an album Given I am on the homepage And I follow "Edit" And I fill in "artist" with “Billy Joel" And I fill in "title" with “An Innocent Man" And I press "Save" Then I should see text matching “An Innocent Man" And I should be on the homepage tests/behat/features/bootstrap/album.feature