Testing Tips"

From Documentation
m ((via JWB))
(7 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
=ID and UUID=
 
=ID and UUID=
By default, the desktop's ID and component's UUID are randomized, such that the chance to pick up a wrong component is minimized if the sever is restarted. Since component's UUID will become DOM element's ID at the browser, it means the DOM element's IDs will change from one test run to another.
 
  
If your test code runs at the server (such jUnit), it is not an issue at all (since DOM elements are available at the client only). However, if your test tool runs at the browser, you have to resolve it with one of the following solutions:
+
By default, the desktop's ID and component's UUID are randomized for preventing Cross-Site Request Forgery (CSRF) and allowing multiple desktops coexists in the same web page (such as Portlet). However, it also means the DOM element's IDs will change from one test run to another, since component's UUID will become DOM element's ID at the browser, .
 +
 
 +
If your test code runs at the server (such [[ZK_Developer's_Reference/Testing/ZATS|ZATS]] and JUnit), it is not an issue at all (since DOM elements are available at the client only). However, if your test tool runs at the browser, you have to resolve it with one of the following solutions:
  
 
# Not to depend on DOM element's ID. Rather, use component's ID and/or component's parent-child-sibling relationship.
 
# Not to depend on DOM element's ID. Rather, use component's ID and/or component's parent-child-sibling relationship.
Line 39: Line 40:
 
</system-config>
 
</system-config>
 
</source>
 
</source>
 +
 +
{{versionSince| 7.0.0}}
 +
 +
Since ZK 7.0.0, we provide a static ID generator implementation for testing, to use <javadoc type="class">org.zkoss.zk.ui.impl.StaticIdGenerator</javadoc>, simply add it to zk.xml.
 +
 +
<source lang="xml">
 +
<system-config>
 +
    <id-generator-class>org.zkoss.zk.ui.impl.StaticIdGenerator</id-generator-class>
 +
</system-config>
 +
</source>
 +
 +
=Different Configuration for Different Environment=
 +
If you prefer to have a different configuration for the testing environment (such as specifying ID generator for testing), you could put the configuration in a separated file, say, <code>WEB-INF/config/zk-testing.xml</code> with the following content.
 +
 +
<source lang="xml">
 +
<zk>
 +
  <system-config>
 +
    <id-generator-class>my.IdGenerator</id-generator-class>
 +
  </system-config>
 +
</zk>
 +
</source>
 +
 +
Then, you could you could specify -Dorg.zkoss.zk.config.path=/WEB-INF/config/zk-testing.xml as one of the arguments when starting the Web server.
 +
 +
 +
=Disabled UUID recycle=
 +
 +
If you want to generate uuid with some conditional , you might also want to disable UUID recycle. ( It will reuse all the uuids from removed components.)
 +
You could set the properties [[ZK_Configuration_Reference/zk.xml/The_Library_Properties/org.zkoss.zk.ui.uuidRecycle.disabled|org.zkoss.zk.ui.uuidRecycle.disabled]] in zk.xml .
 +
  
 
=Version History=
 
=Version History=
{{LastUpdated}}
+
 
{| border='1px' | width="100%"
+
{| class='wikitable' | width="100%"
 
! Version !! Date !! Content
 
! Version !! Date !! Content
 
|-
 
|-

Revision as of 07:37, 8 July 2022

ID and UUID

By default, the desktop's ID and component's UUID are randomized for preventing Cross-Site Request Forgery (CSRF) and allowing multiple desktops coexists in the same web page (such as Portlet). However, it also means the DOM element's IDs will change from one test run to another, since component's UUID will become DOM element's ID at the browser, .

If your test code runs at the server (such ZATS and JUnit), it is not an issue at all (since DOM elements are available at the client only). However, if your test tool runs at the browser, you have to resolve it with one of the following solutions:

  1. Not to depend on DOM element's ID. Rather, use component's ID and/or component's parent-child-sibling relationship.
  2. Implement IdGenerator to generate UUID in a predictable and repeatable way

Approach 1: Use Widget's ID

With Server+client architecture, ZK maintains an identical world at the client. If your test tool is able to access JavaScript at the client, your test code could depend on the widget's ID and widget's parent-child-relationship as your application code depends on the component's ID and component's parent-child-relationship. They are identical, except one is JavaScript and called Widget, while the other is Java and called Component.

This is a suggested approach, since it is much easier to test an application in the same abstract level -- the component level, aka., the widget level (rather than DOM level).

To retrieve widgets at the client, you could use jq and/or Widget.$(Object, Map) (they are all client-side API). jq allows your test code to access the components directly, so the test code could depend on widget's ID (Widget.id) and the widget tree (Widget.firstChild, Widget.nextSibling and so on).

jq('@window[border="normal"]') //returns a list of window whose border is normal
jq('$x'); //returns the widget whose ID is x
jq('$x $y'); //returns the widget whose ID is y and it is in an ID space owned by x

With this approach, you still can verify the DOM structure if you want, since it can be retrieved from widget's Widget.$n().

ZTL is a typical example that takes this approach. For more information, please refer to the ZTL section.

Approach 2: Implement ID Generator

If your test tool running at the client cannot access JavaScript, you could implement an ID generator to generate desktop's ID and component's UUID in a predictable and repeatable matter.

To implement a custom ID generator, you have to do the following:

<system-config>
    <id-generator-class>my.IdGenerator</id-generator-class>
</system-config>

Since 7.0.0

Since ZK 7.0.0, we provide a static ID generator implementation for testing, to use StaticIdGenerator, simply add it to zk.xml.

<system-config>
    <id-generator-class>org.zkoss.zk.ui.impl.StaticIdGenerator</id-generator-class>
</system-config>

Different Configuration for Different Environment

If you prefer to have a different configuration for the testing environment (such as specifying ID generator for testing), you could put the configuration in a separated file, say, WEB-INF/config/zk-testing.xml with the following content.

<zk>
  <system-config>
    <id-generator-class>my.IdGenerator</id-generator-class>
  </system-config>
</zk>

Then, you could you could specify -Dorg.zkoss.zk.config.path=/WEB-INF/config/zk-testing.xml as one of the arguments when starting the Web server.


Disabled UUID recycle

If you want to generate uuid with some conditional , you might also want to disable UUID recycle. ( It will reuse all the uuids from removed components.) You could set the properties org.zkoss.zk.ui.uuidRecycle.disabled in zk.xml .


Version History

Version Date Content
     



Last Update : 2022/07/08

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.