Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the scree
$ pear channel-discover pear.everzet.com
$ pear install everzet/behat-beta
| - features
`- steps
| `- * _steps.php
`- support
`- env.php
$ pear channel-discover pear.phpunit.de
$ pear channel-discover components.ez.no
$ pear channel-discover pear.symfony-project.com
$ pear install phpunit/PHPUnit
<? php require_once 'PHPUnit / Autoload.php'; require_once 'PHPUnit / Framework / Assert / Functions.php'; ?>
<? php require_once 'PHPUnit / Autoload.php'; require_once 'PHPUnit / Framework / Assert / Functions.php'; include 'includes.php'; ?>
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told
Scenario:
Given I have an calculator
When I have entered 30 as first number
And I have entered 20 as second number
And I press 'Add'
Then the result should be 50
$ behat features 1 scenario (1 undefined)
5 steps (5 undefined)
0.091s
You can implement step definitions for undefined steps with these snippets:
$ steps-> Given ('/ ^ I have an calculator $ /', function ($ world) {
throw new \ Everzet \ Behat \ Exception \ Pending ();
});
$ steps-> When ('/ ^ I have entered (\ d +) as first number $ /', function ($ world, $ arg1) {
throw new \ Everzet \ Behat \ Exception \ Pending ();
});
$ steps-> And ('/ ^ I have entered (\ d +) as second number $ /', function ($ world, $ arg1) {
throw new \ Everzet \ Behat \ Exception \ Pending ();
});
$ steps-> And ('/ ^ I press \' ([^ \ '] *) \' $ / ', function ($ world, $ arg1) {
throw new \ Everzet \ Behat \ Exception \ Pending ();
});
$ steps-> Then ('/ ^ The result should be (\ d +) $ /', function ($ world, $ arg1) {
throw new \ Everzet \ Behat \ Exception \ Pending ();
});
class Calc {
protected $ first = 0;
protected $ second = 0;
protected $ result = 0;
public function setFirst ($ num) {$ this-> first = $ num; }
public function setSecond ($ num) {$ this-> second = $ num; }
public function add () {$ this-> result = $ this-> first + $ this-> second; }
public function getResult () {return $ this-> result; }
}
<? php
$ steps-> Given ('/ ^ I have an calculator $ /', function ($ world) {
$ world-> calc = new Calc ();
});
$ steps-> When ('/ ^ I have entered (\ d +) as first number $ /', function ($ world, $ num) {
$ world-> calc-> setFirst ($ num);
});
$ steps-> When ('/ ^ I have entered (\ d +) as second number $ /', function ($ world, $ num) {
$ world-> calc-> setSecond ($ num);
});
$ steps-> When ('/ ^ I press \' Add \ '$ /', function ($ world) {
$ world-> calc-> add ();
});
$ steps-> Then ('/ ^ The result should be (\ d +) $ /', function ($ world, $ res) {
assertEquals ($ res, $ world-> calc-> getResult ());
});
?>
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told
Scenario: # features / calc.feature: 6
Given I have an calculator # features / steps / calc_steps.php: 5
When I have entered 30 as the first number # features / steps / calc_steps.php: 9
And I have entered 20 as second number # features / steps / calc_steps.php: 13
And I press 'Add' # features / steps / calc_steps.php: 17
Then features should be 50 # features / steps / calc_steps.php: 21
1 scenario (1 passed)
5 steps (5 passed)
0.114s
<! DOCTYPE html>
<html>
<head>
</ head>
<body>
<div> <?
if ($ _GET ["submit"]) {
echo "Text =". $ _GET ['textfield']. "<br />";
echo "Checkbox =". $ _GET ['checkbox']. "<br />";
echo "Radio =". $ _GET ['radio']. "<br />";
echo "Select =". $ _GET ['selectbox']. "<br />";
}
?> </ div>
<form method = "get" action = "behat.php">
<div>
<input type = "text" name = "textfield" value = "">
</ div>
<div>
<input type = "checkbox" name = "checkbox" value = "checkbox">
</ div>
<div>
<input type = "radio" name = "radio" value = "radio1" checked = "checked">
<input type = "radio" name = "radio" value = "radio2">
<input type = "radio" name = "radio" value = "radio3">
</ div>
<div>
<select name = "selectbox">
<option value = "option1" selected = "selected"> option1 </ option>
<option value = "option2"> option2 </ option>
<option value = "option3"> option3 </ option>
</ select>
</ div>
<div>
<input type = "submit" name = "submit" value = "Submit">
</ div>
<div id = "linkdiv">
<a href="behat.php?textfield=text&checkbox=checkbox&radio=radio3&selectbox=option2&submit=Submit"> Click me </a>
</ div>
</ form>
</ body>
</ html>
Feature: tests
After submit form all values ​​of fields
Should be showed in top of page
Scenario: Fill field
Given I'm on test page
When I fill in 'textfield' with 'some text' in form 'Submit'
And I submit form 'Submit'
Then I should see 'Text = some text'
Scenario: Checking checkbox
Given I'm on test page
When I tick checkbox 'checkbox' in form 'Submit'
And I submit form 'Submit'
Then I should see 'Checkbox = checkbox'
Scenario: Selecting radio
Given I'm on test page
When I select 'radio2' in radio 'radio' in form 'Submit'
And I submit form 'Submit'
Then I should see 'Radio = radio2'
Then I should not see 'Radio = radio1'
Scenario: Selecting option in selectbox
Given I'm on test page
When I select 'option3' in selectbox 'selectbox' in form 'Submit'
And I submit form 'Submit'
Then I should see 'Select = option3'
Then I should not see 'Select = option1'
Scenario: Fill some fields
Given I'm on test page
When I fill in the following form:
| textfield | some text |
| checkbox | true |
| radio | radio3 |
| selectbox | option2 |
And I submit form 'Submit'
Then I should see 'Text = some text'
And I should see 'Checkbox = checkbox'
And I should see 'Radio = radio3'
And I should see 'Select = option2' within 'div'
Scenario: Clicking on link
Given I'm on test page
When I click on link 'Click me' within '#linkdiv'
Then I should see 'Text = text'
And I should see 'Checkbox = checkbox'
And I should see 'Radio = radio3'
And I should see 'Select = option2' within 'div'
And the 'textfield' field in the 'Submit' should be blank
Unknown path 'test page'. You can define it in [features_folder]/support/paths.php$world->paths['test page'] = "http://tests.dev/behat.php";Source: https://habr.com/ru/post/110422/
All Articles