Follow Us on Twitter

Testing ADF with Selenium: Accessing elements through multiple regions

by Mike Heeren on November 2, 2015 · 37 comments

During the project where I am currently working on, we use Selenium for (unit)testing the ADF user interfaces of the application. Most pages of our application contain multiple (nested) regions with elements that need to be filled or clicked using Selenium. One of the problems we ran into was that ADF does not always generate the exact same ID for an element at runtime, especially when the element is a child of multiple nested regions. In our case it was not possible to search the elements using a “simple” XPath expression like “//input[contains(@id,’it1′)]” because some IDs can be used in multiple active regions.

We were eventually able to solve our issues not by trying to access the elements by their IDs, but using XPath expressions that search through the multiple layers of the generated HTML elements. I have written a Java utility class that can be used to achieve this. This Java utility class uses the DIV components generated for each region. The IDs of this DIV end with a colon and the ID given in the ADF page/fragment for the region. With this knowledge it is possible to generate an XPath expression which “steps through” the different layers of regions in the ADF page. The final problem that occurred was that XPath does not contain an ends-with function. The private constructEndsWithXpath method in the utility class can be used to achieve the same result as expected from an ends-with function.

package nl.whitehorses.selenium;
	 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
	 
public class AdfComponentUtils {
	 
    private AdfComponentUtils() {
        super();
    }
	 
    public static WebElement findWebElement(WebDriver driver, String elementId, String... parentIds) {
        return driver.findElement(By.xpath(constructFindElementXpath("@id", new StringBuilder(":").append(elementId).toString(), parentIds)));
    }
	 
    private static String constructFindElementXpath(String locatorType, String locatorValue, String... parentIds) {
        reverseStringArray(parentIds);
	
        StringBuilder xpathBuilder = new StringBuilder();
	 
        if (parentIds != null) {
            for (String regionId : parentIds) {
                xpathBuilder.append("//*[").append(constructEndsWithXpath("@id", new StringBuilder("':").append(regionId).append("'").toString())).append("]");
            }
        }
	 
        xpathBuilder.append("//*[").append(constructEndsWithXpath(locatorType, new StringBuilder("'").append(locatorValue).append("'").toString())).append("]");
	
        return xpathBuilder.toString();
    }
	 
	 
    private static String constructEndsWithXpath(String string, String suffix) {
        return new StringBuilder("substring(").append(string).append(", string-length(").append(string).append(") - string-length(").append(suffix).append(") + 1) = ").append(suffix).toString();
    }
	 
    private static void reverseStringArray(String... array) {
        for (int i = 0; i < array.length / 2; i++) {
            String temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
    }
}

The findWebElement method can be used to find the ADF elements in the Selenium tests:

  • Driver: The WebDriver instance that has to be used to find the elements.
  • ElementId: The ID that is assigned in the ADF pages/fragments to the element, e.g. soc1, it1, cb1.
  • ParentIds: Optional parameter(s) for providing the parent element(s) of the element you are looking for.

The method “AdfComponentUtils.findWebElement(<driver>, “select”, “soc1”, “r3”, “r2”, “r1″);” could be used when the structure of the ADF page is something like:

<page>
    <region id="r1">
        <region id="r2">
            <region id="r3">
                <select id="soc1">
                    <option value="1">Test 1</option>
                    <option value="2">Test 2</option>
                    <option value="3">Test 3</option>
                </select>
            </region>
        </region>
    </region>
</page>

Note: One last tip, for some ADF components, such as SelectBooleanCheckbox components, a suffix is appended to the ID generated at runtime. ::content will be appended to SelectBooleanCheckbox components for example. Because we are using ­­ends-with, you should keep this in mind while using the findWebElement method. When this is the case, a statement like the following can be used: “AdfComponentUtils.findWebElement(<driver>,”input”, “sbc1::content”);”.

Good luck testing your ADF application using Selenium!

Update 05-11-2015: Like Wilfred van der Deijl mentions in the comments, is SeleniumTools an excellent toolkit for testing ADF applications using Selenium. However, we were not able to use the SeleniumTools in our project because SeleniumTools is developed for ADF 12c. In our project we have to test an ADF 11g application.

I have also made a few small modifications on the “AdfComponentUtils” class, which make it easier to use. The “elementType” parameter is removed so it is easier to write tests because you no longer have to know which HTML element is generated for a component.

Also check out my other related blogpost: Sorting ADF tables using Selenium

Testing ADF with Selenium: Accessing elements through multiple regions, 5.0 out of 5 based on 1 rating
Ratings:
VN:D [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)

{ 37 comments… read them below or add one }

Wilfred November 2, 2015 at 10:07 pm

You might also be interested in the talk we gave at openworld last week: http://www.slideshare.net/WilfredvanderDeijl/automated-testing-adf-with-selenium
It introduces, among other things, SeleniumTools a toolkit for Selenium testing ADF applications that also solves this issue by being totally aware of ADF components. Here you would have a subclass of com.redheap.selenium.page.PageFragment that represents your page fragment being tested. It would now about the af:region it lives in which is represented by com.redheap.selenium.component.AdfRegion. These classes can locate other components in the page fragment with relative JSF selectors so you don’t have to know about the dynamic parts of a client-id. It offers a bunch of other useful methods for testing an ADF application. Feel free to browse the slides at slideshare.

Reply

Egor Eremeev August 1, 2018 at 6:11 pm

Wilfred,
thank you for you work under SeleniumTools!

Recently began using it in my project and faced with a question how to manage ?

I follow Page Object pattern and use findAdfComponent() method from SeleniumTools. It wotks great for elementa outside iframe.

The problem with iframe is similar as described in this post, it is unable to locate elements inside iframe.

Could you suggest a way how to extend the SeleniumTools for working with ?

Thank you

Reply

Egor Eremeev August 1, 2018 at 6:14 pm

somewhy words was cutted from the post…

Question is how to manage af:inlineFrame

Reply

Egor Eremeev October 24, 2018 at 3:47 pm

I wanna describу solution concernig af:inlineFrame for all whom maybe interested in.
Actually, af:inlineFrame tag should be managed in other way than af:region.
In a whole, to managed inlineFrame you need to use Selenium API methods and no special things from SeleniumTools or other libs. After webdriver switches to frame all other things from SeleniumTools concerning location ADF Components works perfectly. They performs locating just relatively to document in you frame.

Reply

alaskamary November 5, 2015 at 11:25 am

wow..it’s very interesting.it’s very useful to every one..

Reply

Vikash Kumar February 2, 2016 at 1:40 pm

Really nice blog!!

It is interesting and very useful to every one..

Reply

CREDO SYSTEMZ February 6, 2017 at 11:12 am

Thanks for sharing this informative blog. I have read your blog and I gathered some valuable information from this blog. Keep posting.
Regards.

Reply

ElishaGrace April 28, 2018 at 9:02 am

Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

Reply

zasi May 2, 2018 at 3:05 pm

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

Reply

lenin September 7, 2018 at 5:52 pm

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

Reply

varshini October 12, 2018 at 11:45 am

Nice post. Thank you for sharing a good post.

Reply

Anonymous Hosting October 17, 2018 at 1:44 pm

Wow, Nice blog. Thank you so much for the efforts of this blog.

Reply

AWS Training in Chennai October 24, 2018 at 11:58 am

Informative post, thanks for sharing.

Reply

CCNA course in Chennai October 25, 2018 at 2:13 pm

Excellent and useful blog admin, I would like to read more about this topic.

Reply

UiPath Training in Chennai October 26, 2018 at 1:33 pm

Thanks for taking time to share this page admin. I learned a lot from your page, keep sharing more like this.

Reply

ReactJS Training in Chennai November 14, 2018 at 1:56 pm

I have to thank for sharing this blog, really helpful.

Reply

Blue Prism Training in Chennai November 15, 2018 at 10:50 am

Awesome post, share more like this.

Reply

Data Science Training in Chennai November 16, 2018 at 1:52 pm

Great and informative post, keep sharing.

Reply

Python Training in Chennai November 22, 2018 at 11:52 am

Learned a lot from your blog, keep sharing such kind of worthy information.

Reply

Anbarasan Perumal November 26, 2018 at 8:58 am

Excellent blog!!!Thanks for sharing. Keep doing more.

Reply

Azure Training in Chennai November 30, 2018 at 1:40 pm

Useful blog admin, this is what I have looked for.

Reply

RPA Training in Chennai December 4, 2018 at 12:13 pm

I believe that this blog is really useful for the needed ones. Keep sharing such kind of useful information.

Reply

DevOps Training in Chennai December 6, 2018 at 1:28 pm

Very informative post, I am glad that I found your post. I really admire the way your article is being written. Great work.

Reply

machine learning certification in chennai December 13, 2018 at 1:03 pm

Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.

Reply

Blueprism Training in Chennai December 15, 2018 at 6:59 am

Such a Great Article!! I learned something new from your blog. Amazing stuff. I would like to follow your blog frequently. Keep Rocking!!

Reply

Roja Priya December 15, 2018 at 12:05 pm

Given so much info in it, The list of your blogs are very helpful for those who want to learn more interesting facts. Keeps the users interest in the website, and keep on sharing more
Our Credo Systemz Which is designed to offer you OpenStack Training skills required to kick-start your journey as an OpenStack Cloud Administrator.
Please free to call us @ +91 9884412301 / 9600112302

https://www.credosystemz.com/courses/openstack-training-chennai/

Reply

Prasath DJ December 16, 2018 at 1:46 pm

Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.
best selenium training in chennai |

Reply

TOEFL Classes in Chennai December 24, 2018 at 2:23 pm

Thanks to the admin of this blog for sharing these kinds of useful information; Have been waiting for more updates.

Reply

velraj December 26, 2018 at 9:11 am

Candidates who are like to know more about the DevOps can go for Devops Training in Chennai at FITA. We providing you full placement and provides the best training for all IT courses. Trainers are full attention towards students. If students have any doubt you can contact trainers at any time. We won a large number of hearts because we providing full placement and giving knowledge about the interview process. Trainers are trained you as a perfect employee to start the career in the industry. If you attend Best Devops Training in Chennai at Fita then you got a detail ideas of all. The only right place is FITA to start the career in industry. Devops course in Chennai is the crossing technologies platform. Join with us for DevOps at FITA!

Reply

Best digital marketing company in Delhi December 27, 2018 at 7:46 am

hello sir,
thanks for giving that type of information.
HP DesignJet T120 In Delhi

Reply

DevOps Training in Chennai December 30, 2018 at 1:41 pm

Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.

Reply

jefrin adams January 10, 2019 at 2:44 pm

Very useful post thanks for sharing
best azure certification training in chennai

Reply

vijaykumar January 12, 2019 at 10:15 am

Candidates who are like to know more about the uipath can go for UiPath Training in Chennai at FITA. Trainers are trained you as a perfect employee to start the career in the industry. All Trainers are well experienced in reputed companies. Trainers are full attention to you and if you have any doubt you can contact to trainers at any time. We providing you full placement and provides best training for all IT courses. We won a large number of hearts because we providing full study resources and giving knowledge about the interview process. If you once attend UiPath Courses in Chennai you got a detail ideas of all. The only right place is FITA to start the career. UiPath Training Institutes in Chennai is the crossing technologies platform. Join with us for UiPath at FITA!

Reply

Jaya January 12, 2019 at 12:24 pm

Thanks for the info! Much appreciated.
Regards,
Data Science Course In Chennai

Reply

Akash thakur January 15, 2019 at 11:06 am

Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
home interior designer in noida

Reply

Vikash Kumar January 16, 2019 at 10:54 am

nice work keep it up thanks for sharing the knowledge.Thanks for sharing this type of information, it is so useful.
Epoxy Grout manufacturer

Reply

Unix Training in Chennai January 22, 2019 at 10:34 am

Great work! This is really helpful. I’m glad that I came across your article. Do share more such articles with us.

Reply

Leave a Comment

 

Previous post:

Next post:

About Whitehorses
Company profile
Services
Technology

Whitehorses website

Home page
Whitebooks
Jobs

Follow us
Blog post RSS
Comment RSS
Twitter