behat
play

Behat BDD, FUNCTIONAL TESTS & SELENIUM (IN DRUPAL!) s Hallo! - PowerPoint PPT Presentation

Behat BDD, FUNCTIONAL TESTS & SELENIUM (IN DRUPAL!) s Hallo! > Lead of the Symfony documentation team > KnpLabs US - Symfony consulting, training & kumbaya > Writer for KnpUniversity.com: PHP & Symfony


  1. /** * @When I run :command */ public function iRun($command) { exec($command, $output); $this->output = trim(implode("\n", $output)); } /** * @Then I should see :string in the output */ public function iShouldSeeInTheOutput($string) { if (strpos($this->output, $string) === false) { throw new \Exception(‘Did not see’.$string); ); }

  2. See the full FeatureContext class: http://bit.ly/behat-ls-feature

  3. What Behat *does* Scenario Step Given I have a fi le named “foo” pattern @Given I have a fi le named : fi le public function iHaveAFileNamed($ fi le) { De fi nition do work touch($ fi le); Pass/Fail: Each step is a “test”, which passes *unless* an exception is thrown

  4. Creating fi les and directories in FeatureContext is nice...

  5. but wouldn’t it be really cool to command a browser, fi ll out forms and check the output?

  6. Mink https://www.flickr.com/photos/15016964@N02/5696367600

  7. Mink! ‣ A standalone library to use PHP to 
 command a “browser” 
 ‣ One easy API that can be used to 
 command Selenium, Goutte, 
 PhantomJS, etc http://mink.behat.org/ @weaverryan

  8. A sample of Mink

  9. use Behat\Mink\Driver\GoutteDriver; use Behat\Mink\Session; // change *only* this line to run // in Selenium, etc $driver = new GoutteDriver(); $session = new Session($driver);

  10. // visit a page $session->visit('http://behat.org'); echo 'Status: '.$session->getStatusCode(); echo 'URL : '.$session->getCurrentUrl();

  11. $page = $session->getPage(); // drill down into the page $ele = $page->find(' css ', ' li:nth-child(4) a '); echo 'Link text is: '.$ele->getText(); echo 'href is: '.$ele->getAttribute(' href '); // click the link // (you can also fill out forms) $ele->click();

  12. Mink inside FeatureContext => Dangerous Combo for Functional Testing

  13. Integration Behat Mink MinkExtension ‣ An “Extension” is like a Behat plugin 
 ‣ The MinkExtension makes using Mink 
 inside Behat a matter of con fi guration http://mink.behat.org/ @weaverryan

  14. 
 Install Mink & MinkExtension ‣ Update composer.json to include: > Mink > MinkExtension > Goutte and Selenium2 Drivers for Mink @weaverryan

  15. composer require --dev \ behat/mink-extension \ behat/mink-goutte-driver \ behat/mink-selenium2-driver

  16. { “require-dev": { "behat/behat": "^3.1", "behat/mink-extension": "^2.2", "behat/mink-goutte-driver": "^1.2", "behat/mink-selenium2-driver": "^1.3" } } http://bit.ly/behat-mink-composer

  17. Goal: To easily use Mink inside FeatureContext

  18. Bootstrap MinkExtension # behat.yml 
 default: 
 extensions: 
 Behat\MinkExtension: 
 goutte: ~ 
 selenium2: ~ 
 # The base URL you're testing 
 base_url: http://en.wikipedia.org/ @weaverryan

  19. 
 Extend MinkContext use Behat\MinkExtension\Context\RawMinkContext; 
 /** 
 * Behat context class. 
 */ 
 class FeatureContext extends RawMinkContext @weaverryan

  20. Access to a Mink Session class FeatureContext extends RawMinkContext { public function doSomething() { $session = $this->getSession(); $session->visit('http://behat.org'); } // ... } Our custom de fi nitions can now command a browser!

  21. More! Add MinkContext # behat.yml 
 default: 
 extensions: # ... suites: 
 default: 
 contexts: 
 - FeatureContext 
 - Behat\MinkExtension\Context \MinkContext 
 Behat now parses de fi nitions from *our* class *and* this MinkContext class

  22. We inherit a pile of great de fi nitions Before adding MinkContext: the -dl option prints all current de fi nitions

  23. After adding MinkContext:

  24. In other words: We can write some tests for our app without writing any PHP code @weaverryan

  25. Suppose we’re testing Wikipedia.org

  26. # features/wikipedia.feature Feature: Search In order to see a word definition As a website user I need to be able to search for a word Scenario: Searching for a page that does exist Given I am on "/wiki/Main_Page" When I fill in " search " with " Behavior Driven Development " And I press "searchButton" Then I should see "agile software development" These 4 de fi nitions all come packaged with MinkContext

  27. Celebration!

  28. Behat in your application

  29. Behat in your App https://www.flickr.com/photos/yoanngd/10669976224

  30. Getting “under the hood” ‣ Black-box testing: the site lives out on the web ‣ Because of this, we can’t: a) access/clear/prepare the database b) use any code in our application @weaverryan

  31. When testing: you should guarantee the starting condition of your environment

  32. How can we add nodes, add users, and con fi gure permissions from inside Behat?

  33. Behat & Drupal ‣ Install Behat & Mink 
 ‣ ??? Gain access to Drupal functionality from inside FeatureContext ‣ PROFIT! ‣ Create nodes, users, etc so that you’re testing against a predictable dataset @weaverryan

  34. Fortunately... ... there’s a library made by the Drupal community ...

  35. ... which I did not help with ...

  36. DrupalExtension! jhedstrom A plugin (extension) for Behat and Drupal http://bit.ly/drupal-extension

  37. DrupalExtension 1) Even more built-in sentences/de fi nitions 2) Build nodes, add users, manage permissions inside Behat 3) Operating within Regions 4) Hooks to load more sentences/ de fi nitions from contrib modules @weaverryan

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend