HomeOATS

How to get all links of a web page using OATS

Like Tweet Pin it Share Share Email

It is a very common requirement in automated testing to get the list of all links, the instance when this is generally required is:

  1. To see if all desired links are present in a web page.
  2. 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 javascript

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();
ptl.add("text", linkname);
List list = web.document(docpath).getElementsByTagName("a",ptl);
// assuming single object
if(list.size()==1)
{
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.
}
else
{
System.out.println("Link not found with name:" + linkname);
}
}

There is also an another way to acheive this by executing javascript code from Openscript, will come up with a new article to do with javascript as well, this is one of the fastest way to perform the action but needs good programming knowledge too.

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.

Comments (9)

  • Hi Srinivas,

    testingtools.co is really a usefull one .Thank you so much , Continue blogging.

    I am working using oats to automate oracle financial applications.

    1) I am taking data from csv and automating.
    2)After completion of automation I need to write back to csv ,the result of automation .

    I have number of rows in csv , representing each iterations .So at the end of each row I need to write back from oats into corresponding column.

    Please help me in this. It’ll be very needful for me .

    Reply
    • Author

      Hi Radhika,

      Thank you for reaching out to us and asking for this valuable requirement, which many of the real time automation testers are in need for 🙂 the answer is simple, you need to use POI library to interact with Excel file instead of a csv file and I think you should be able to write values to the excel file.

      Please do let me know if you are only looking for csv file

      Reply
      • Hi Srinivas ,

        Thanks for the reply.

        Ya I got the requirement not to use excel. So opted for csv. And below is the code I used to write back to csv and it is working 🙂

        public boolean update(String file, String replaceValue, int rowIndex, int columnIndex){
        log.info(“Execution of update method started”);
        boolean result = false;
        try{
        File inputFile = new File(file);
        if(inputFile.exists()){

        CSVReader reader = new CSVReader(new FileReader(inputFile), ‘,’);
        List csvBody = reader.readAll();

        csvBody.get(rowIndex)[columnIndex] = replaceValue;
        reader.close();

        CSVWriter writer = new CSVWriter(new FileWriter(inputFile), ‘,’);
        writer.writeAll(csvBody);
        writer.flush();
        writer.close();
        result = true;
        }
        log.info(“Execution of updateCSV method completed successfully”);
        }catch (Exception e) {
        log.error(“Inside the catch block. Error while Updating the CSV.”+e.getMessage());
        }
        return result;
        }

        Reply
  • Hi Srinivas,
    I need to automate scripts in OATS for theme testing (to check link’s, heading’s and button’s colors) to check whether they are according to theme (color set for these objects) or not?
    Is it possible to do it by OATS?

    Reply
  • How to find all elements on page/frame and their xpaths using oats

    Reply
    • Author

      I think you are the right page, it has the code to get all links from a web page. please do let me know if you are unable to proceed with the available code and are you getting any specific errors as such?

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *