The WebTable API

Hi There...


This API introduces a WebTable class (The first utility), that will help in dealing with an HTML table

Little story - workaround , done smartly :)

While I come across a Table in an HTML page, accessing the table cell content using Web Driver script makes the code look silly with lots of XPATH content (Most of the times ID is not available for cell, hence we prefer XPATH ). Do you had this experience ? if not , lets take a sample example :)

A Sample Table as shown in image : from WordPress


If you want to get the content of the first and second cell from the Header Section then the Web Drvier code would be something like

    String firstCellOfHeader= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/thead/tr/th[2]/a/span")).getText();

  String secondCellOfHeader= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/thead/tr/th[3]/a/span")).getText();




If you want to get the content of the first and second cell from the first row of Body Section then the Web Drvier code would be something like



String firstCellOfBody= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/tbody/tr/td/strong")).getText();

String secondCellOfBody= driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/tbody/tr/td[2]")).getText();

The same code, when used with the Utility API, there is no need for having XPATH or any Locator to identify the cell of a table.
The equivalent code would be ...

/*Step1: Identify the Table Element*/
WebElement postsTable = driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table"));


/*Step2: Pass the Table Element to the WebTable Utility Class provided in the API*/
WebTable table = WebTable.getTable(postsTable);

/*Once the above two steps are done, now you can access any cell by passing its cell index : starts from 0th Index*/

String firstCellOfHeader= table.getTHead().getRow(0).getHeaderCell(1).getText();
String secondCellOfHeader= table.getTHead().getRow(0).getHeaderCell(2).getText();


String firstCellOfBody= table.getTBody().getRow(0).getCell(0).getText(); 
String secondCellOfBody= table.getTBody().getRow(0).getCell(1).getText();


This approach Benefits few things
  • Code looks more clean and readable
  • No need for having locator to identify the Cell
  • Easy scripting and debuging

Download Locations:


Download ATU Selenium Utilities - WebTable API (included from version1 onwards)


A lot more utilities will be added to this API. Happy Scripting :)

LinkedIn Group:

you can join our LinkedIn group here for more collaborative discussions: ATU LinkedIn Group

Give our tools a Feedback:

 We are constantly working on several projects, we would like to have a feedback on all the tools so that we can prioritize, introduce new features, enhancing them etc. This will also help us in understanding the usage of these tools and lift us to develop further more utilities.

 Feedback page : ATU Utilities User Feedback


We love Automation; we love to keep its utilities Open Source.
                                                                                                – Automation Testing Utilities

22 comments:

  1. Hi,

    Can we get the table row by one of its cell value instead of index?

    Regards,

    ReplyDelete
    Replies
    1. Excellent blog, good to see someone is posting quality information. Thanks for sharing this useful information. Keep up the good work diamond drilling contractors

      Delete
  2. Its not implemented in this api. Imagine 10 different cells have similar content, in this case we should return more than one row isn't it.

    Regards
    AT

    ReplyDelete
  3. yes, with one parameter, there will be a problem but what if it take 2 parameters, index and cell value to uniquely identify it.
    Anyways nice efforts indeed.

    Regards,

    ReplyDelete
  4. with two parameters, by index I mean the "column index" as I have unique values in that particular column.

    ReplyDelete
    Replies
    1. You can create a method which can return row num. This api can be used to develop that method.

      Regards
      AT

      Delete
  5. Hi,

    Can you please tell me that how can we click an element/link inside the web table using this api ?

    ReplyDelete
    Replies
    1. As explained in the post, find the table element, pass it to the webtable api method. Find the desired row and retrieve the element.

      #AT

      Delete
  6. I need to click on an element inside the Cell, I can get the row element , but how can click on specific cell

    ReplyDelete
    Replies
    1. Karuna,

      getRow(0).getCellElement(index) will return the Web Element.
      pass the index of the column

      #AT

      Delete
  7. 1) can you please tell how can we get total columns ?
    2) Is there any way to get the total rows if my table results has pagination ?

    ReplyDelete
    Replies
    1. Hi,

      1) Use the API method "getCoulmnCount()" to get count of columns.
      2) Not sure with pagination, we will be glad to know if you can check it to see if works

      #AT

      Delete
  8. how to configure this API , if i want use it

    ReplyDelete
  9. can't access the cell value by using table.getTHead().getRow(0).getHeaderCellElement(4).getText() or even table.getTHead().getRow(0).getCellElement().getText(). Column size is 8 but its showing 2 . what to do ?
    when i am using table.getTHead().getRow(0).getHeaderCellElement(0).getText() its working but when increasing cell index its not working

    ReplyDelete
  10. How to get the value of textbox from webtable

    ReplyDelete
  11. Engineering students who are interested in best inplant training in chennai can contact us. We provide real time best inplant training in chennai for aspiring students.

    ReplyDelete
  12. Hello Admin, thank you for the informative post. At Fita academy, we provide the best IT trainings for various courses. You can check our Fita Chennai complaints page to know more about our quality and standards.

    ReplyDelete
  13. Hi, nice site you have here! Keep up the excellent work! best university in punjab

    ReplyDelete