Deal with Randomized UUID
By default, a desktop's ID and a component's UUID are randomized for preventing Cross-Site Request Forgery (CSRF) and allowing multiple desktops to coexist on the same web page (such as Portlet). A component's UUID is auto-generated by ZK and different from its ID. The UUID is used as a component DOM elements' id in a browser. However, it also means the DOM element's IDs will change from one test run to another.
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 in a browser, you have to locate an element with one of the following approaches:
- Not to depend on a DOM element's ID. Rather, use a component's ID and/or component's parent-child-sibling relationship.
- Implement IdGenerator to generate UUID in a predictable and repeatable way
Let me explain them in detail.
Approach 1: Locate by a component's ID
This is a suggested approach since it is much easier to test an application at the same abstract level -- the component level, aka., the widget level (rather than the DOM level).
jq('@window[border="normal"]') //returns a list of window whose border is normal jq('$x'); //returns the widget whose component ID is x, <div id="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 a widget's Widget.$n().
Approach 2: Use ID Generator
Since ZK 7.0.0, ZK provides 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>
To implement a custom ID generator, you have to do the following:
- Implement a Java class that implements IdGenerator.
- Specify the Java class FQCN at id-generator-class element in
zk.xml. For example,
<system-config> <id-generator-class>my.IdGenerator</id-generator-class> </system-config>
- ComponentIdFirstGenerator. Unlike StaticIdGenerator, component creation order doesn't affect id generation.
- StaticIdGeneratorExt. It generates desktop id in a predictable way.
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 conditions, you might also want to disable UUID recycling. ( It will reuse all the UUIDs from removed components.) You could set the properties org.zkoss.zk.ui.uuidRecycle.disabled in zk.xml.