20
Automated web acceptance testing with Behat Rajesh Taneja @TanejaRaji MoodleMoot Australia 2015 #mootau15 the world’s open source learning platform

@TanejaRaji Rajesh Taneja - MoodleMoot Australia · Automated web acceptance testing with Behat Rajesh Taneja @TanejaRaji MoodleMoot Australia 2015 #mootau15 the world’s open source

  • Upload
    hakien

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Automated web acceptance testing with

Behat

Rajesh Taneja@TanejaRajiMoodleMoot Australia 2015

#mootau15

the world’s open source learning platform

the world’s open source learning platform

Who am I ?

Rajesh Taneja● @TanejaRaji, at Moodle HQ● Test Automation Developer● Moodler for last 4 years ● Aussie from the land of Bollywood

What is Behat ?

the world’s open source learning platform

● Behat tests the behavior of moodle, described in special language called Gherkin

What is Behat ?

the world’s open source learning platform

● Behat tests the behavior of moodle, described in special language called Gherkin

● Gherkin is a “Business Readable Domain Specific Language” created especially for behavior descriptions.

What is Behat ?

the world’s open source learning platform

● Behat tests the behavior of moodle, described in special language called Gherkin

● Gherkin is a “Business Readable Domain Specific Language” created especially for behavior descriptions.

Example?

Feature: DescriptionIn order to do ...As a [role] I need to do ...

Scenario:Given [initial context], When [event occurs], Then [ensure some outcomes]

the world’s open source learning platform

Why Behat ?

the world’s open source learning platform

To Run existing tests.

● Set config values for behat

● Installation

● Start Selenium server

● Running behat test

the world’s open source learning platform

Set config values

● $CFG->behat_prefix = 'b_';

● $CFG->behat_dataroot = '/path/to/your/behat/dataroot/directory';

● $CFG->behat_wwwroot = 'http://127.0.0.1/moodle';

the world’s open source learning platform

Initialise behat setup

● Single Behat run○ php admin/tool/behat/cli/init.php

● Parallel Behat run○ php admin/tool/behat/cli/init.php --parallel=4

the world’s open source learning platform

Start selenium server

● Download selenium server○ http://seleniumhq.org/download/

● Start selenium server○ java -jar /full/to/path/selenium-server.jar

the world’s open source learning platform

Run behat test

● Single run○ vendor/bin/behat --config

/path/to/behat_dataroot/behat/behat.yml○ php admin/tool/behat/cli/run.php

● Parallel run○ php admin/tool/behat/cli/run.php

DEMO

the world’s open source learning platform

Write new feature1. Create feature file under:

COMPONENTNAME/tests/behat/featurename.feature

2. Add a tag with the component

name in Frankenstyle format

3. Begin writing the user story of the feature

the world’s open source learning platform

the world’s open source learning platform

Available steps in moodle

the world’s open source learning platform

Adding new stepsCreate new context COMPONENTNAME/tests/behat/context.feature

require_once(__DIR__ . '/../../../lib/behat/behat_base.php');use Behat\Behat\Context\Step\Given as Given;use Behat\Behat\Context\Step\When as When;

class behat_auth extends behat_base {

/** * Logs in the user. There should exist a user with the same value as username and password. * * @Given /^I log in as "(?P<username_string>(?:[^"]|\\")*)"$/ */ public function i_log_in_as($username) { }}

the world’s open source learning platform

Checklist● New step should be implemented as public method● Classname should be same as filename + .php● Save file in COMPONENTNAME/tests/behat/ directory or

lib/tests/behat/● Describe step in single line in phpdoc● Provide proper regular expression input

○ Number: "(?P<info_about_what_you_expect_number>\d+)" ○ Text: "(?P<info_about_what_you_expect_string>(?:[^"]|\\")*)"○ Key/value pair: Finish your regular expression with :○ Selector: "(?P<selector_string>[^"]*)" or "(?P<text_selector_string>

[^"]*)"

the world’s open source learning platform

Running specific feature

● php admin/tool/behat/cli/run.php --tags=@mod_assign

● php admin/tool/behat/cli/run.php FULL/PATH/TO/tests/behat/featurename.feature

the world’s open source learning platform

Running behat with specific browser● Set config in config.php

● Reinitialize behat ○ php admin/tool/behat/cli/util.php --enable

● Start selenium server with required driver○ java -jar path/to/selenium-server.jar -Dwebdriver.chrome.

driver=/path/to/chromedriver● Run behat

○ php admin/tool/behat/cli/run.php

$CFG->behat_config = array( 'chrome' => array( 'extensions' => array( 'Behat\MinkExtension\Extension' => array( 'selenium2' => array( 'browser' => 'chrome',

the world’s open source learning platform

Reference links○ https://docs.moodle.org/dev/Acceptance_testing

○ http://docs.behat.org/

○ http://www.seleniumhq.org/

Thank you

Community: moodle.orgCommercial: moodle.com

@moodle

the world’s open source learning platform