It is a very common requirement in automated testing to get the list of all links, the instance when this is generally required is:
- To see if all desired links are present in a web page.
- In case of multiple links having same text, there would be necessity to click on a specific link though the link texts are same.
Oracle Application Testing Suite also provides ways to get the list of all links or buttons or any UI components in a web page, before we deep diving in to details, we have to understand something called DOM – Document Object Model.
Lets now look at 3 different options of getting list of all links or buttons or UI Components from a web page:
- Using getElementByTagName with just the name.
- Using getElmenetByTagName with name and with selection criteria.
Using getElementByTagName with just the name
For example for link it would be “a”, for button it could be “button” / “input” with type as button or submit, for image it would be “img”, we need to supply this as an input. We should be able to get all links from the web page document with following line of code.
List list = web.document("xpath for document").getElementsByTagName("a");
Make sure appropriate packages are loaded to have DOMElement and List, in eclipse you can press a short cut Ctrl + Shift + o it will include the dependent packages automatically.
Now we can iterate through the complete list and reach to your desired link and perform any action.
DOMLink link = (DOMLink) list.get(i);
System.out.println("link name" + link.getAttribute("text"));
link.click(); //clicking the link at index of i, as we are iterating.
As we see this is one way to iterate through all the links and access them.
Using getElmenetByTagName with name and with selection criteria
this is an another tricky way to directly get all links which are with a specific name like “Home”, as if we need to loop through all links and find out the link it can have performance hit on the automation script.
look at the function implemented it can be used by any one their projects 🙂
Below method assumes that there is only one link in the web page with a specific name, in case there are multiple method needs an enhancement accordingly.
public void clickLink(String linkname, String docpath) throws Exception
PropertyTestList ptl = new PropertyTestList();
List list = web.document(docpath).getElementsByTagName("a",ptl);
// assuming single object
DOMLink link = (DOMLink) list.get(0);
System.out.println("Link name:" + link.getAttribute("text"));
link.click(); // this would click the object in the web page.
System.out.println("Link not found with name:" + linkname);
hope this gives you an understanding how to get list of objects and the internal mechanism for the same. please do ask your queries in form of comments I would be more than happy to help.