Most Powerful Open Source ERP

How To Write Zelenium Tests

How To showing how to write Zelenium integration tests for user interfaces.
  • Last Update:2017-05-15
  • Version:003
  • Language:en

Zelenium tests and common utilities are stored in erp5_ui_test business templates. Many other tests could also be found in erp5_*_ui_test. Zuites are stored in portal_tests and are organized in Zuite recursively. A zuite is an set of tests stored as Page Templates (see also HowToRunZeleniumTests).

Table of Contents

Useful resource

Writing Zelenium Tests

Zelenium, how the name hints, is Selenium test tool built into Zope itself. It is accessible at <instance>/erp5/portal_tests/manage_main. Make sure that you have business template erp5_ui_test installed.

To create a test, you need to create a Zuite first using drop-down menu on top-right corner.  Zuite is a kind of folder and contains all runnable tests. As the ID, use "<module>_ui_zuite", for example "renderjs_ui_listbox_zuite". Then navigate inside your new Zuite and create a Page Template with ID starting at "test" such as "testMyModuleFormSubmit". 

Example TestCase 

Below is an example of test code that you could save in a Page Template :

<html xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test My Module UI</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">My Module Zuite</td></tr>
</thead><tbody>
<tal:block metal:use-macro="here/Zuite_CommonTemplate/macros/init" />
<!-- Run the test as anonymous -->
<tr>
  <td>open</td>
  <td>${base_url}/logout</td>
  <td></td>
</tr>
<tr>
  <td>pause</td>
  <td>500</td>
  <td></td>
</tr>
<tr>
  <td>open</td>
  <td>${base_url}/web_site_module/my_module_website/</td>
  <td></td>
</tr>
<tr>
  <td>waitForElementPresent</td>
  <td>//a[@data-i18n='Submit']</td>
  <td></td>
</tr>
<tr>
  <td>store</td>
  <td>My cloud</td>
  <td>title_1</td>
</tr>
<tr>
  <td>click</td>
  <td>//a[@data-i18n='Submit']</td>
  <td></td>
</tr>
<tr>
  <td>verifyText</td>
  <td></td>
  <td>OK</td>
</tr>
</tbody></table>
</body></html>

Selenium User Extensions for ERP5

We have the following extensions.

  • assertPortalStatusMessage that checks the value of a portal status message.
  • assertFloat that basically acts as assertText, but converts both values to float before comparting them, with this 1 000.00 is equals to 1000.

Best Practices

Selecting in a list field

When using selectAndWait command and the element is already selected, selenium hangs. The solution is to use assertSelected before using selectAndWait, so if the selection is not what you expect it to be, selectAndWait will not be executed.

Reindex in the middle of a test

You should avoid doing useless reindex in the middle of tests. Every time you use it, this means the scenario you are checking would surely not work without reindex. It is usually better when user can do many actions without the need to wait for indexation. Though, indexing is still useful in various cases, like checking catalog search. In such case you could do as follow :

<!-- reindex -->
<tr>
  <td>store</td>
  <td>javascript{selenium.browserbot.getContentWindow().location.href}</td>
  <td>current_location</td>
</tr>
<tr>
  <td>open</td>
  <td tal:content="string:${here/portal_url}/Zuite_waitForActivities"/>
  <td/>
</tr>
<tr>
  <td>assertTextPresent</td>
  <td>Done</td>
  <td/>
</tr>
<tr>
  <td>open</td>
  <td>${current_location}</td>
  <td></td>
</tr>

Keep in Mind

  • Tests should not rely on previous test runs
  • Test should have setUp and tearDown scripts which can prepare and clean up environment respectively
  • Test must not contains any hard coded values like user names, organizations, etc ... Instead configuration of former can be moved to a Python script which can be used at test rendering time to dynamically generate test
  • Test should use a non manager account but a real ERP5 account so security is taken into account whenever test is run
  • Test must be created if feasible in a way so that they can be reused and plugged into a production instance, run in it without modifying production data
  • Common code can be grouped into a "library" of macros

Zelenium Hints

  • Take care when using 'open' rather than 'openAndWait'. This can cause random test failures whenever only 'open' (fire and go one test execution) is used.
  • When you have to use "type" in selenium always assert if all fields and the save button is present, before use any type into one page or any selection. This will prevent the tests become stopped waiting for confirmation in dialog is some action is not possible.

Compatibility Notes

Zelenium 0.8 uses Selenium 0.6, and SVN trunk of Zelenium uses Selenium 0.8.3. And there are some incompatibilities.

  • getLocation() returns full URL instead of path.
  • We define getAbsoluteLocation() in erp5_ui_test_core. If you use assertAbsoluteLocation() or verifyAbsoluteLocation() instead of assertLocation() or verifyLocation(), the test should work on both version.
  • isEditable() raises an error if the specified element isn't an input element.
  • We can't use assertNotEditable() or verifyNotEditable() for asserting "it isn't an input field".
  • Use <br/> to specify a new line in type(), assertText(), verifyText() etc. instead of a real new line in HTML code.
  • There is no way to write a test with new lines that supports both version.

Related Articles