Generate IDs in all components in ADF Faces pages and fragments - to facilitate automated Selenium based testing startup 594127 1280

Generate IDs in all components in ADF Faces pages and fragments – to facilitate automated Selenium based testing

Lately, we have been introducing Selenium based testing for many our ADF Faces based web applications. Or rather, our testers created the tests for the web pages using Selenium. And did so quite successfully.

However, certain test scripts seemed to fall apart after having been run only a few times. That was rather annoying. Upon closer inspection, we noticed that the scripts that started failing had one thing in common: the Selenium scripts contained references to element id values that started with j_id. I realized that these id values are not defined in the pages by the developer, but instead assigned at run time by the ADF Faces framework. That in turn means that these ID values can easily change, between two test sessions using the same test script for example.

In order to have test scripts that continue functioning – it was required to have fixed id values. Besides, it is a best practice to assign id values to all components at design time rather than having them generated dynamically. Unfortunately, as found out later, we had over 2000 components lacking an id attribute with developer defined value. Going through all files and assigning ID values manually was not a task anyone on the team was really looking forward to.

We then decided to write a small Java utility that processes all JSPX and JSFF files, locates elements of specified types without and ID attribute and assigns a unique ID attribute value. Running this utility took all of 45 seconds and corrected 2186 components.

You can download the AddIDsToADFPage.zip file that contains a JDeveloper 11g application with a single class – JSPXProcessor. To correct your JSPX and JSFF files – simply load the application, open the class, specify the directory to (recursively) process in the main method and run the class. The private List variable elementNamesThatNeedId contains a List of element names for which ID attributes should be added. Some components, such as f:facet and f:param, do not require an ID attribute. You may want to extend this list to better suit your needs.

Note: the structure of the JSPX and JSFF files will change when even a single ID attribute is added, as the XML is written again to the file system in potentially a different layout than before. This may (and did on our site) confuse diff-and-merge utilities.

Download JDeveloper application: AddIDsToADFPage.