automation testing with drupal 8

24
Automated Testing Abhishek Anand Technical Architect & TAM ACQUIA Prachi Nagpal Drupal Developer ACQUIA

Upload: nagpalprachi

Post on 12-Feb-2017

822 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Automation testing with Drupal 8

Automated TestingAbhishek AnandTechnical Architect & TAM

ACQUIA

Prachi NagpalDrupal Developer

ACQUIA

Page 2: Automation testing with Drupal 8
Page 3: Automation testing with Drupal 8

TEST DRIVEN DEVELOPMENT

1. Write a test2. Run the test3. Let the test fail4. Write enough code for the test to pass5. Run your test again6. Refactor/ clean up the code7. Run test again8. Repeat

Page 4: Automation testing with Drupal 8

WHY USE TDD

1. Better understanding of what you're going to write2. Enforces the policy of writing tests a little better3. Speeds up development

Page 5: Automation testing with Drupal 8

BENEFITS OF TDD

1. Testable code2. Clean design3. Able to be refactored with confidence4. The minimal code necessary to satisfy the story card5. A living specification of how the code works6. Able to support a sustainable pace of new features

Page 6: Automation testing with Drupal 8

TOOLS

Page 7: Automation testing with Drupal 8

WHY USE CODECEPTION

1. Powered by PHPUnit2. Any type of test

a. Acceptanceb. Functionalc. Unit

3. Easy to extend4. Lot of test suits already available for Drupal5. Reusable code6. Run all types of test from one place

Page 8: Automation testing with Drupal 8

INSTALLATION

Page 9: Automation testing with Drupal 8

INSTALLATION

Page 10: Automation testing with Drupal 8

GETTING STARTED

By default AcceptanceTester relies on PhpBrowser module, which is set intests/acceptance.suite.yml

It can also be set to use WebDriver

class_name: WebGuyTestermodules: enabled: - WebDriver: url: http://drupal812 browser: chrome wait: 2000 - \Helper\WebGuy

Page 11: Automation testing with Drupal 8

➜ php codecept.phar generate:cept acceptance SigninTest was created in /Users/prachi.nagpal/Sites/drupal8/tests/acceptance/SigninCept.php

<?php$I = new AcceptanceTester($scenario);$I->wantTo('Test index page');$I->amOnPage('/');$I->see('Site-Install','#header #site-name');$I->amGoingTo('login in the test app');$I->fillField('Username','admin');$I->fillField('Password','1234');$I->click('Log in');$I->see('Log out');$I->click('Log out');$I->see('User login');?>

Page 12: Automation testing with Drupal 8

EXAMPLE

<?php$I = new AcceptanceTester($scenario);$I->wantTo('ensure that frontpage works');$I->amOnPage('/'); $I->see('Home');?>

<?php$I = new AcceptanceTester($scenario);$I->amOnPage('/');$I->click('Sign Up');$I->submitForm('#signup', ['username' => prachi.nagpal, 'email' => '[email protected]']);$I->see('Thank you for Signing Up!');?>

Page 13: Automation testing with Drupal 8

EXAMPLE

Codeception can even 'naturalize' this scenario, converting it into plain English:

I WANT TO SIGN INI am on page '/user'I fill field 'username', 'prachi.nagpal'I fill field 'password', '1234'I click 'Log in'I see 'Welcome, prachi.nagpal!'

Page 14: Automation testing with Drupal 8

RUNNING TESTS

Tests can be started with the run command.

➜ php codecept.phar run ➜ php codecept.phar run acceptance ➜ php codecept.phar run acceptance SigninCept.php ➜ php codecept.phar run tests/acceptance/SigninCept.php ➜ php codecept.phar run tests/acceptance/SignInCest.php:anonymousLogin ➜ php codecept.phar run tests/acceptance/backend

Page 15: Automation testing with Drupal 8

SELENIUM WEBDRIVERWAIT

<?php$I->waitForElement('#agree_button', 30); // secs$I->click('#agree_button');?>

SESSION SNAPSHOTS

function test_login($I) { // if snapshot exists - skipping login if ($I->loadSessionSnapshot('login')) return; // logging in $I->amOnPage('/login'); $I->click('Login'); // saving snapshot $I->saveSessionSnapshot('login');}// in test:$I = new AcceptanceTester($scenario);test_login($I);

Page 16: Automation testing with Drupal 8

MULTI SESSION TESTING

$I = new AcceptanceTester($scenario);$I->wantTo('try multi session');$I->amOnPage('/messages');$nick = $I->haveFriend('nick');$nick->does(function(AcceptanceTester $I) { $I->amOnPage('/messages/new'); $I->fillField('body', 'Hello all!') $I->click('Send'); $I->see('Hello all!', '.message');});$I->wait(3);$I->see('Hello all!', '.message');

Page 17: Automation testing with Drupal 8

CLEANING UP / DB SNAPSHOTS→ Db module→ Load a database dump after each passed test.→ SQL dump to be put in /tests/_data directory. → Set the database connection and path to the dump in the global Codeception config.

# in codeception.yml:modules: config: Db: dsn: '[set pdo dsn here]' user: '[set user]' password: '[set password]' dump: tests/_data/dump.sql

Page 18: Automation testing with Drupal 8

DEBUGGING

➜ php codecept.phar run --debug

<?php codecept_debug($I->grabTextFrom('#name')); ?>

Page 19: Automation testing with Drupal 8

REUSING TEST CODE

StepObjects

➜ php codecept.phar generate:stepobject acceptance Admin

➜ php codecept.phar generate:stepobject acceptance AdminAdd action to StepObject class (ENTER to exit): loginAsAdminAdd action to StepObject class (ENTER to exit):StepObject was created in /tests/acceptance/_support/Step/Acceptance/Admin.php

Page 20: Automation testing with Drupal 8

Step Object<?phpnamespace Step\Acceptance;class Member extends \AcceptanceTester{ public function loginAsAdmin() { $I = $this; $I->amOnPage('/admin'); $I->fillField('username', 'admin'); $I->fillField('password', '123456'); $I->click('Login'); }}?>

Actual Test<?phpuse Step/Acceptance/Admin as AdminTester;$I = new AdminTester($scenario);$I->loginAsAdmin();?>

Page 21: Automation testing with Drupal 8

PageObjects

$ php codecept.phar generate:pageobject Login

Defining PageObjects<?phpnamespace Page;

class Login{ public static $URL = '/login';

public static $usernameField = '#mainForm #username'; public static $passwordField = '#mainForm input[name=password]'; public static $loginButton = '#mainForm input[type=submit]';}?>

Page 22: Automation testing with Drupal 8

Using PageObjects

<?phpuse Page\Login as LoginPage;

$I = new AcceptanceTester($scenario);$I->wantTo('login to site');$I->amOnPage(LoginPage::$URL);$I->fillField(LoginPage::$usernameField, 'bill evans');$I->fillField(LoginPage::$passwordField, 'debby');$I->click(LoginPage::$loginButton);$I->see('Welcome, bill');?>

Page 23: Automation testing with Drupal 8

PHPUNIT AND DRUPAL 8

...to be continued by Abhishek Anand

Page 24: Automation testing with Drupal 8

DEATH BY POWERPOINT!!!

THANK YOU!THANK YOU!

DEATH BY POWERPOINT!!!