Behat Kickstart for Drupal Developers
Total Page:16
File Type:pdf, Size:1020Kb
BEHAT KICKSTART FOR DRUPAL DEVELOPERS Florida DrupalCamp 2016 Orlando, FL - March 5 - 6, 2016 Peter Sawczynec Engineer INTRO TO DRUPAL TESTING ECOSYSTEM Behat SimpleTest PHPUnit JMeter Drupal Extension Mink Selenium 2.0 Phantom.js Mockery Prophecy Codeception Wraith Casper.js Slimer.js Phantom.css Jenkins Travis CI Circle.ci CrossBrowserTesting.com Sauce Labs New Relic FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS INTRO TO DRUPAL/PHP TESTING ECOSYSTEM Consider using these tools in your Drupal development process: phpStorm drush composer drupal console FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS Behat INTRO TO BDD Behaviour Driven Development (BDD) FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS INTRO TO BEHAT Formal Explanation Behat is a tool that makes behavior driven development (BDD) possible. With BDD, you write human-readable stories that describe the behavior of your application. These stories can then be auto-tested against your application. FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS INTRO TO BEHAT Informal Explanation Behat is about the concept of using a simple coding language (Gherkin) with simple words to describe things that get done (behaviors) on your website that you want to test using an ecosystem of software libraries/tools. FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS Behat in Drupal BEHAT IN DRUPAL Behat usage in Drupal involves installing a set of software libraries that work quite seamlessly together FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS INSTALL WITH COMPOSER In Drupal 7 and 8 install and update Behat and all the additional libraries and components you need with Composer FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS STATE OF TESTING IN DRUPAL 8 ● Drupal 8 ships with a vendor directory that contains Behat ● Drupal 8 ships with PHPUnit ● SimpleTest module also ships in D8 core, but must be enabled FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS MINK, MINK EXTENSION, DRUPAL EXTENSION... ● Mink, Mink Extension, Drupal Extension, Selenium, Phantom.js ... ● Software libraries like the above are important adjuncts to Behat, these libraries create the bridge to your web pages and to Drupal so you can run tests on your site FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS Behat Ecosystem in Drupal DRUPAL BEHAT ECOSYSTEM Where your Behat 3.0 Behat tests get Mink written Headless Browsers Behat Feature files with steps, Browser the “Tests” Goutte Controllers Selenium2 Gherkin Behat Context Phantom.js PHP class files, the testing code PHP Website, Drupal Extension website pages (drupal and drush) Drupal EXECUTION CODE INTRO TO BDD The space where as a Drupal developer Where your you write Behat tests and supporting Behat tests get written code logic using Gherkin and PHP Behat Feature Behat Context files, class files, the “Tests” the testing code Gherkin PHP Drupal Writing Behat Testing for Drupal STEPS, SCENARIOS, BACKGROUND Comments and Annotations ● More useful and required in your code. ● Used by Drupal, Behat, Symfony and other frameworks to “discover” your code and what it does FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS STEPS, SCENARIOS, BACKGROUND Comments /** * Step function to visit the last created node of a specific type. and * Annotations * @param string $type * The type of node that should be visited. Examples: * * @Given I visit the last created :type node */ Comment /** * Views query plugin for an XML query. * Annotation * @ingroup views_query_plugins * (contains info for * @ViewsQuery( the framework) * id = "views_xml_backend", * title = @Translation("XML Query"), * help = @Translation("Query will be generated and run using the XML backend.") * ) */ FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS INTRO TO BDD Feature file # Groups: Event, search (Gherkin) Given I run drush "search-api-index" Then I login And I click "Events" Then I click "New Group Event" Behat Feature Then I should see the text "Looking for members?" Andfiles, I follow the link element with xpath "//a[contains(@href,'/group-ela')]" Giventhe “Tests” I visit the last created "article" node Context class file Gherkin /** * Step function to visit the last created node of a specific type. (PHP) * * @param string $type * The type of node that should be visited. * * @Given I visit the last created :type node Behat Context class files,*/ public function iVisitTheLastCreatedNodeByType($type) { the testing $node code = $this->getLastCreatedEntityFromDb('node', $type); $this->getSession()->visit($this->locatePath('/node/' . $node->nid)); PHP } Drupal INTRO TO BDD Feature file # Groups: Event, search Given I run drush "search-api-index" (Gherkin) Then I login And I click "Events" Then I click "New Group Event" Behat Feature Then I should see the text "Looking for members?" Andfiles, I follow the link element with xpath "//a[contains(@href,'/group-ela')]" Giventhe “Tests” I visit the last created "article" node Context class file Gherkin /** (PHP) * Step function to visit the last created node of a specific type. * * @param string $type * The type of node that should be visited. * * @Given I visit the last created :type node Behat Context class files,*/ public function iVisitTheLastCreatedNodeByType($type) { the testing $node code = $this->getLastCreatedEntityFromDb('node', $type); $this->getSession()->visit($this->locatePath('/node/' . $node->nid)); PHP } Drupal INTRO TO BDD Feature file (Gherkin) Then I login Then I should see the text "Looking for members?" Given I visit the last created "article" node Then Behat IFeature logout Context class file files, ...the “Tests” /** (PHP) * Step function to visit the last created node of a specific type. Gherkin * * @param string $type * The type of node that should be visited. * Behat * @Given Context I visit the last created :type node class files, the */testing code public function iVisitTheLastCreatedNodeByType($type) { PHP Drupal $node = $this->getLastCreatedEntityFromDb('node', $type); STEPS Steps in Gherkin When I am on the homepage Then I should get a "200" HTTP response And I should see the button 'Log in' When I go to "/admin" Then I should get a "403" HTTP response Steps And I should see "Access denied" FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS STEPS, TAGS, SCENARIO Steps in use in a Scenario: Tags @smoke @access Scenario: Anonymous User permissions When I am on the homepage Scenario Title Then I should get a "200" HTTP response And I should see the button 'Log in' When I go to "/admin" Then I should get a "403" HTTP response Steps And I should see "Access denied" FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS TAGGING @api @permissions Feature: Specific user permissions As an authenticated user Tags to identify the 1. I shouldPoint not be able to access One admin pages whole feature file So that I can verify my permissions 2. @smokePoint Two Scenario: Anonymous User permissions When I am on the homepage 3. Then IPoint should get a "200" Three HTTP response Tags by scenario 4. And I Pointshould see the button Four 'Log in' 5. Point Five FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS STEPS, TAGS, SCENARIO, SCENARIO OUTLINE, BACKGROUND @api @permissions @member @group @access @info Feature: Specific user permissions Scenario Outline: Access user info, login history As an authenticated user Given I am logged in as <user> with password <password> ISteps should not be able to access admin pages # View user login history So that I can verify my permissions And I visit the login history for <user_target> Then I should get a "<response>" HTTP response # Create standard users for all tests. Background: Examples: Given set content creation mode as "default" | user | user_target | response | password | And load the background users: | um1 | um11 | 404 | "xxx-xxx" | | user | | ug2 | um21 | 404 | "xxx-xxx" | | uorgmanager1 | | ug1 | um32 | 200 | "xxx-xxx" | | umember1b | | genl | "qam" | 200 | "xxx-xxx" | @smoke Scenario: Anonymous User permissions When I am on the homepage Then I should get a "200" HTTP response And I should see the button 'Log in' FLORIDA DRUPALCAMP 2016 | BEHAT KICKSTART FOR DRUPAL DEVELOPERS | PETER SAWCZYNEC | PETER.SAWCZYNEC@CIVICACTIONS TABLE NODE @api @permissions Feature: Specific user permissions As an authenticated user 1. I shouldPoint not be able to access One admin pages So that I can verify my permissions 2.# CreatePoint standard users forTwo all tests. Background: Table Node data Given set content creation mode as "default" Essentially how to pass 3. And loadPontPoint the background ThreeThree users: | user | an array to your Behat | uorgmanager1 | method in your context 4. | umember1bPoint | Four @smoke 5. Scenario:PointFourPoint Anonymous