Web49.4
Web – Pseudo Attributes

The idea of pseudo attributes is to simplify resolvers using JavaScript to retrieve values from a browser. You can register a pseudo attribute in QF-Test which receives its value as result of some JavaScript code execution.

In a SUT script you can fetch attribute values of HTML elements via the method getAttribute() (This is also the way the CustomWebResolver (see subsection 49.1.2) evaluates its genericClasses category). Pseudo attributes values are fetched with the same mechanism, behaving just like normal attributes in the "eyes" of QF-Test. Only, they are not defined via the HTML source code or explicitly set via node.setAttribute(), but execute a piece of JavaScript code.

When you define a pseudo attribute you can mark it as "cacheable". In that case the pseudo attribute will be evaluated the first time referenced and then its value will be saved until the next complete scan of the page. This improves the overall testing performance. Pseudo attributes should not be cached for values subject to change (e.g. the status of a check box), because then a change of the value would not be "visible" to QF-Test. Uncached pseudo attributes will not save their value, but it will be fetched (and kept shortly for processing) each time an event will be recorded, like for example the single events "moved", "pressed", "released" and "clicked" of a mouse click, or for the identification of a component during replay.

The following example defines a pseudo attribute for all HTML elements with the tag "ICON" or "IMAGE" evaluating the value of iconname via JavaScript (Technically speaking, it calls the sample method inspect defined by the framework for the HTML node).

import de.qfs.apps.qftest.client.web.dom.DomNodeAttributes
import de.qfs.apps.qftest.client.web.dom.FunctionalPseudoAttribute

def attr = new FunctionalPseudoAttribute("js_icon",
                       "try {return _qf_node.inspect('iconname')} catch(e){}", true)
DomNodeAttributes.registerPseudoAttributes("ICON", attr)
DomNodeAttributes.registerPseudoAttributes("IMAGE", attr)
Example 49.32:  Groovy SUT script registering a pseudo attribute

The pseudo attribute can then be used in a feature resolver. In comparison to a direct call of node.callJS() in the script this method takes advantage of the internal caching mechanisms of QF-Test:

def getFeature(node, feature):
    iconname = node.getAttribute("js_icon")
    return iconname

resolvers.addResolver("iconFeature", getFeature, "ICON", "IMAGE")
Example 49.33:  Using a pseudo attribute in a resolver (Jython SUT script)

The following script deregisters the pseudo attribute.

import de.qfs.apps.qftest.client.web.dom.DomNodeAttributes

DomNodeAttributes.unregisterPseudoAttributes("ICON", "js_icon")
DomNodeAttributes.unregisterPseudoAttributes("IMAGE", "js_icon")
Example 49.34:  Deregister a pseudo attribute (Groovy SUT script)

A pseudo attribute has to be defined via the following method from the module de.qfs.apps.qftest.client.web.dom.pseudo attributes:

 
 
PseudoAttribute FunctionalPseudoAttribute(String name, String javaScriptFunction, Boolean cached)
Defines a pseudo attribute.
Parameters
name The name for the pseudo attribute.
javaScriptFunction The JavaScript code to be executed within a function when referencing the pseudo attribute. Use _qf_node as the reference for the HTML element. The execution is equal to a call of DomNode.callJS.
cachedtrue, when you want to cache the value after the first reference to the pseudo attribute, otherwise false.
Returns A pseudo attribute you can then register.
 
PseudoAttribute PseudoAttribute(String name, String javaScriptCode, Boolean cached)
Defines a pseudo attribute.
Parameters
name The name for the pseudo attribute.
javaScriptCode The JavaScript code to be evaluated when referencing the pseudo attribute. Use _qf_node as the reference for the HTML element. The execution is equal to a call of DomNode.evallJS.
cachedtrue, when you want to cache the value after the first reference to the pseudo attribute, otherwise false.
Returns A pseudo attribute you can then register.
 
 

Having defined the pseudo attribute you need to register it via the following method from module de.qfs.apps.qftest.client.web.dom.DomNodeAttributes:

 
 
void registerPseudoAttributes(String tag, PseudoAttribute pseudoAttribute)
Registers a pseudo attribute for HTML elements with the given tag.
Parameters
tag The tag of the HTML elements to be registered for. When you want to register the pseudo attribute for HTML elements with different tags you need to do it for each one in turn. When you want to register it for all HTML elements, use the tag "<QF_ALL>".
pseudoAttribute The pseudo attribute previously defined.
 
 
 
 
void unregisterPseudoAttributes(String tag, String name)
Deregisters the pseudo attribute for HTML elements with the given tag. You do not need to deregister a pseudo attribute. When stopping QF-Test it will be done automatically.
Parameters
tag The tag of the HTML elements for which to deregister the pseudo attribute.
name The name of the pseudo attribute.