Actions

Testing Area/Behat Testing/Steps

From Mahara Wiki

< Testing Area‎ | Behat Testing


Use these steps to create Behat tests
Copy and paste the steps in the order your need into a text editor (ie gedit or vim) to create your tests.

Note: Replace the "And" with "Given, When, Then" where necessary. Replace Selectors and Elements with one from the list of Selectors and Elements

The fastest way to write test is using parts of previous tests. In the terminal type the following command to search through all the feature files for what you are looking for:

 grep --color=auto -RniP -C 3 --include *.feature '(given|when|then|and).*delete.*activity' *

Replace delete and activity with the words that you specifically need

To see the list of current tests from your Mahara directory

grep -RiPoh 'Scenario:(\s+)\K(.*)$' test/

There are test steps provided with Behat in the MinkContext.php file and other customs steps written for Mahara. For a quick and dirty way to create a file with an up to date list of all the steps available, run the following three lines of code:

egrep -r '@(Given|When|Then)' ~/code/mahara/external/vendor/behat/mink-extension/src/Behat/MinkExtension/Context/MinkContext.php > behat_steps.txt
egrep -rh '@(Given|When|Then)' ~/code/mahara/htdocs/testing/frameworks/behat/classes >> behat_steps.txt
perl -i -pe "s/[?\"\'\*\(\)\[\]^\\]\|\\\\]//g" behat_steps.txt

This creates a file called behat_steps.txt in your current directory which contains the list. It will confirm for you that a step exists in the code. Variables in the step are indicated by P<> or :. You can ignore / and /$ at the beginnings and ends of lines. There is often specific information about how to use the steps in the code comments. If you don't feel comfortable trawling through the code yourself, chat to a developer about updating this page with examples of the steps.

Backgrounds/ Way to create heaps of users, groups, institutions:

Given the following "users" exist:
     | username | password | email | firstname | lastname | institution | authname | role |
     | UserA | Kupuhipa1 | [email protected] | Angela | User | mahara | internal | member |
     | UserB | Kupuhipa1 | [email protected] | Bob    | User | mahara | internal | member |
Given the following "groups" exist:
     | name | owner | description | grouptype | open | invitefriends | editroles | submittableto | allowarchives | members | staff |
     | GroupA | UserA | GroupA owned by UserA | standard | ON | OFF | all | ON | OFF | UserB, UserC |  |
     | GroupB | UserA | GroupB owned by UserA | standard | ON | OFF | all | OFF | OFF | UserB, UserC |  |
Given the following "institutions" exist:
     | name | displayname | registerallowed | registerconfirm |
     | instone | Institution One | ON | OFF |
     | insttwo | Institution Two | ON | OFF |

Create heaps of empty pages for users, groups, institutions and sites.

Given the following "pages" exist:
     | title | description| ownertype | ownername |
     | Page UserA_01 | Page 01 | user | UserA |
     | Page UserA_02 | Page 02 | user | UserA |
     | Page GroupA_01 | Group page 01 | group | GroupA |
     | Page GroupA_02 | Group page 02 | group | GroupA |
     | Page Institution_01 | Institution page 01 | institution | instone |
     | Page Site_01 | Site page 01 | institution | mahara |

Create heaps of collections

Given the following "collections" exist:
     | title | description| ownertype | ownername | pages |
     | Collection UserA_01 | Collection 01 | user | UserA | Page UserA_06, Page UserA_12 |
     | Collection UserA_02 | Collection 02 | user | UserA | Page UserA_07 |
     | Collection GroupA_01 | Group collection 01 | group | GroupA | Page Group_01, Page GroupA_02 |

Create permissions

   And the following "permissions" exist:
     | title | accesstype |
     | Page UserA_01 | public |
     | Collection UserA_01 | public |
   And the following "permissions" exist:
    | title | accesstype | accessname | allowcomments | approvecomments |
    | Page GroupA_01 | loggedin | loggedin | 1 | 1 |

Create journals and journal posts

   And the following "journals" exist:
    | owner | ownertype | title | description | tags |
    | celeste | user | Mars journal | Recording my Mars Mission | Mars |
   And the following "journalposts" exist:
    | owner | ownertype | title | entry | blog | tags | draft |
    | celeste | user | I'm going to Mars! | I just passed my exam and am approved for a Mars Mission | Mars journal | Mars | 0 |
    | celeste | user | Spacefood | Spacefood is kind of gross if you don't cook it right | Mars journal | Mars,food | 0 |

Logging In

Logs you in as Admin for testing purpose

 Given I log in as "userA" with password "Kupuhipa1"



Note: The users will be given an automatic PW of Kupuhipa1 and the email account will be [email protected].
If you want to set a user up with aditional admin permissions you have to do that manually.

Mahara menu navigation

Choose a main menu item:

 Given I choose "Main menu item" from main menu
 Given I choose "Admin menu item" from administration menu
 Given I choose "User menu item" from user menu
 Given I choose "User menu item" from user menu by id

E.g:
And I choose "Admin home" from administration menu
"by id" means find the menu item based on an html id, for instance "mail" for "? unread" messages link to inbox:
And I choose "mail" from user menu by id

Choose a sub menu in a main menu item:

 When I choose "Sub-menu item" in "Main menu item" from main menu
 When I choose "Sub-menu item" in "Admin menu item" from administration menu
 When I choose "Sub-menu item" in "User menu item" from user menu

E.g:
And I choose "Settings" in "Setting" from user menu

And I choose "Pages and collections" in "Portfolio" from main menu

Mahara pages and blocks

Open the configuration of a block

 And I configure the block "Block title"

Delete a block

 And I delete the block "Block title"

Expanding the blocks when adding content to a page

 And I expand "text" node

E.g:
And I expand "General" node

Opening/Loading/Closing Pages

Checks, that current page PATH is equal to specified. eg; And I should be on "Profile" would be written as; And I should be on "artefact/internal/index.php"

 And I should be on "page path"


Opens user profile.

 And I am on user profile


Opens homepage.

 And I go to the homepage
 And I am on homepage

Checks, that current page is the homepage.

 And I should be on the homepage

Reloads the current page.

 And I reload the page

Waits until the page is completely loaded. This step is auto-executed after every step.

 And I wait until the page is ready
    

Take you to a specific page using the relative path:

 And I am on "page"
 And I go to "page"

Example: When I go to "admin/site/options.php" //opens the admin settings for your site.

Opens named Group page

 And I go to group "name"

Moves backward one page in history.

 And I move backward one page

Moves forward one page in history

 And I move forward one page
   

Checks, that current page response status is equal to specified.

 And the response status code should be 404
   
   

Checks, that current page response status is not equal to specified.

 And the response status code should not be 404

Switching Frames/Windows

Switches to the specified iframe.

 And I switch to "$iframename" iframe


Switches to the main Mahara frame.

 And I switch to the main frame


Switches to the specified window. Useful when interacting with popup windows.

 And I switch to “windowname” windows


Switches to the main Mahara window. Useful when you finish interacting with popup windows.

 And I switch to the main window

Actions Requiring Waiting

Waits X seconds. Required after an action that requires data from an AJAX request.

 And I wait "$numberofseconds" seconds


Waits until the provided element selector exists in the DOM

 And I wait until "$element" "$selector" exists


Waits until the provided element does not exist in the DOM

 And I wait until "element_string" "selector_string" does not exist


Click Actions

There are several options for clicking an element in a page. The key difference is in the html tags it has. The first two built in steps 'I press' and 'I follow' take slightly different html tags.

Clicks link with specified id|title|alt|text.

 And I follow "id|title|alt|text"

E.g.
And I follow "Lost username / password"

Presses button with specified id|name|title|alt|value.

 And I press "id|name|title|alt|value"

E.g:
And I press "Save"

Click on is a mahara specific step intended for a link or button. The tags it targets are not documented.

 And I click on "element"

E.g:
And I click on "Help"

If the element you want to click doesn't have an obvious tag, you may need to specify a css or xpath element to be able to click it. As these are not as user-friendly to read, we now have a properties file in mahara (/htdocs/testing/frameworks/behat/classes/properties.php) which converts the css or xpath element to a name which is more human-readable. Use the following property steps in preference to any that directly use a css or xpath element. If you need to target an element not included in the properties file, that element should be added to the properties file.

 And I press "search text" in the "property name" property
 And I follow "search text" in the "property name" property
 And I click on "search text" in the "property name" property

E.g.
And I press "Close" in the "Upload dialog" property
And I follow "Note" in the "blocktype sidebar" property
And I click on "Settings" in the "Toolbar buttons" property

These steps replace the following steps, which should no longer be used:

 And I press "button" in the "$element" "$selector"
 And I follow "link" in the "$element" "$selector"
 And I click on "link or button" in the "$element" "$selector"

There are a couple(?) of steps which take a css/xpath locator that haven't been replaced by a properties step. They are not currently used in any Mahara tests, but are included for completeness:

Generic mouse over action. Mouse over a element of the specified type.

 And I hover "$element" "$selector"

Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.

 And I drag "$element" "$selector" and I drop it in "$containerelement" ”$selector"

Clicking on table elements

Click on the link or button inside a list/table row containing the specified text.

 And I click on "link or button" in "row_text_string" row

Click on the delete button inside a list/table row containing the specified text.

 And I delete the "text string" row

Click on the specified element inside a table row containing the specified text.

 And I click on "$element" "$selector" in the table row

Filling in Forms

Attaches file to field with specified id|name|label|value. eg; "Mahara Testing notes.odt.tar.gz" to "Upload file"

 And I attach the file "File name" to "Field"

Checkboxes

Checks checkbox with specified id|name|label|value.

 And I check "option"

Unchecks checkbox with specified id|name|label|value.

 And I uncheck "option"

Checks, that checkbox with specified in|name|label|value is checked.

 And the "checkbox" checkbox should be checked

Checks, that checkbox with specified in|name|label|value is checked.

 And the checkbox "checkbox" is|should be checked

Checks, that checkbox with specified in|name|label|value is unchecked.

 And the "checkbox" checkbox should not be checked

Checks, that checkbox with specified in|name|label|value is unchecked.

 And the checkbox "checkbox" should be unchecked|not be checked

Checks, that checkbox with specified in|name|label|value is unchecked.

 And the checkbox "checkbox" is unchecked|not checked)

Checking fields for values

Checks, that form field with specified id|name|label|value has specified value.

 And the "field" field should contain "value"

Checks, that form field with specified id|name|label|value doesn't have specified value.

 And the "field" field should not contain "value"

Checks, the field matches the value.

 And the field "field_string" matches value "field_value_string"

Checks, the field does not match the value.

 And the field "field_string" does not match value "field_value_string"

Checks, the provided field/value matches.

 And the following fields match these values:

Checks that the provided field/value pairs don't match.

 And the following fields do not match these values:

Entering Text

Fills in a form field with specified text

 And I fill in "Field" with "Text"
 And I set the field "Field" to "Text"

E.g:

And I fill in "Title" with "Site blog"
And I set the field "field_string" to "field_value_string"

Fills in multiple form fields with field/value data.

 And I set the following fields to these values:
   | Field name | Field value |    
 And I fill in the following:
   | Field name | Field value |  

E.g:
And I fill in the following:

| firstname | Jen |
| lastname | Jenny |
| email | [email protected] |
| username | jen |
| password | mahara1 |


And I set the following fields to these values:

| addeducationhistory_startdate | 1 Jan 2017 |
| addeducationhistory_institution | Mail-order PhD |
| addeducationhistory_institutionaddress | 45 Empty St |

Using select boxes

Checks, that given select box contains the specified option.

 And the "select_string" select box should contain "option_string"

Checks, that given select box contains the specified option.

 And the "select_string" select box should not contain "option_string"

Selects option in select field with specified id|name|label|value.

 And I select "option" from "select"

Selects additional option in select field with specified id|name|label|value.

 And I additionally select "option" from "select"

TinyMCE

Our WYSIWYG rich text editor, TinyMCE, behaves a bit strangely, so it requires its own special behat step to fill in a TinyMCE rich text field. You must identify the editor by the ID of the iframe that TinyMCE generates.

 And I fill in text "Testing 123" in WYSIWYG editor "add_feedback_from_form_message_ifr"

Or you can leave out the iframe name, and Behat will find and use the first TinyMCE editor on the page. (This is useful when the TinyMCE editor's ID is assigned dynamically.)

And I fill in text "Testing 123" in WYSIWYG editor

Checking For Specific Visible Elements

Checks, that (?P<num>\d+) CSS elements exist on the page

 And I should see (?P<num>\d+) "element" elements

Checks, that the specified element is visible. Only available in tests using Javascript.

 And I should see the element "element" "selectortype"
  

Checks, that field with specified identifier exists on page.

 And I should see the field "field"
     

Checks, that current page PATH matches regular expression.

 And the url should match "pattern" 
   

Prints current URL to console.

 And print current URL
   

Checks, that the specified element is visible. Only available in tests using Javascript.

 And "$elements" "$selector" should be visible
    
 And "$element" "$selector" should not be visible


Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.

 And "$element" "$selector" in the "$elementcontainer" "$selector" should be visible     


Checks, that the specified element is not visible inside the specified container. Only available in tests using Javascript.

 And "$element" "$selector" in the "$elementcontainer" "$textselector" should not be visible
         

Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.

 And I should see "$text"
    

Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.

 And I should not see "$text"


Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.

 And I should see "$text" in the "$element" “$sector”

Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.

 And I should not see "$text" in the “$element" "$selector”


Checks, that the first specified element appears before the second one.

 And "$element" "$selector" should appear before "$element" "$selector"
    


Checks, that the first specified element appears after the second one.

 And "$element" "selector" should appear after $"element"  $"selector"


Checks, that the first specified element appears after the second one.

 And "following_element_string" "selector1_string" should appear after "preceding_element_string" "selector2_string"
   

Checks, that element of specified type is disabled.

 And the "$element" "$selector" should be disabled
    

Checks, that element of specified type is enabled.

 And the "$element" "$selector" should be enabled
    

Checks the provided element and selector type are readonly on the current page.

 And the "$element" "$selector" should be readonly

Checks the provided element and selector type exists in the current page. -This step is for advanced users, use it if you don't find anything else suitable for what you need.

 And "$element" "$selector" should exists

Checks that the provided element and selector type not exists in the current page. -This step is for advanced users, use it if you don't find anything else suitable for what you need.

 And "$element" "$selector" should not exists


Checks that an element and selector type exists in another element and selector type on the current page. -This step is for advanced users, use it if you don't find anything else suitable for what you need.

 And "$element" "$selector" should exist in the "$element" "$selector"


Checks, that page contains text matching specified pattern.

 And I should see text matching "pattern"

Checks, that page doesn't contain text matching specified pattern.

 And I should not see text matching "pattern"
   
   

Checks, that HTML response contains specified string.

 And the response should contain "text"


Checks, that HTML response doesn't contain specified string.

 And the response should not contain "text"
   

Checks, that element with specified CSS contains specified text.

 And I should see "text" in the "element" element


Checks, that element with specified CSS doesn't contain specified text.

 And I should not see "text" in the "element" element


Checks, that element with specified CSS contains specified HTML.

 And the "element" element should contain "value"
   


Checks, that element with specified CSS doesn't contain specified HTML.

 And the "element" element should not contain "value"


Checks, that element with specified CSS exists on page.

 And I should see an "element" element
   

Checks, that element with specified CSS doesn't exist on page.

 And I should not see an "element" element

Printing/Display

Prints last response to console.

 And print last response

Prints current URL to console.

 And print current URL

Debugging Tests

Pauses the test and waits for you to manually press the enter key. NB: Don't commit this step!!

 And I insert breakpoint

Prints a line of your choosing to the console so you can confirm the test is reaching that point

 And I echo the line "text"


Ungrouped Steps

Opens last response content in browser.

 And show last response
   

Internal step definition to find exceptions, debugging() messages and PHP debug messages.

 And I look for exceptions


Follows the page redirection. Use this step after any action that shows a message and waits for a redirection

 And I wait to be redirected
    

Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.

 And I accept the currently displayed dialog


This step triggers cron like a user would do going to admin/cron.php.

 And I trigger cron