How to handle Web tables using Java and Selenium Webdriver

How to handle Web tables using Java and Selenium Webdriver

What are Web Tables?

Web tables, also known as HTML tables, are a widely used format for displaying data on web pages. They allow for a structured representation of information in rows and columns, making it easy to read and manipulate data. Selenium WebDriver, a powerful tool for web browser automation, provides the functionality to interact with these tables programmatically. This capability is beneficial for tasks like web scraping, automated testing, and data validation. In this blog, we will see how to extract data from Web tables in Java-Selenium.

Identify web table from your webpage:

To effectively identify and interact with web tables using Selenium, it’s crucial to understand the HTML structure of tables and the specific tags used. Here’s an overview of the key table-related HTML tags

A typical HTML table consists of several tags that define its structure:

  • <table>: The main container for the table.
  • <thead>: Defines the table header, which contains header rows (<tr>).
  • <tbody>: Contains the table body, which includes the data rows.
  • <tr>: Defines a table row.
  • <th>: Defines a header cell in a table row.
  • <td>: Defines a standard data cell in a table row.

How to Identify Web Tables?

As we have got  an idea of what is Web Table and how to identify WebTables on the webpage, now we will see how to extract the table data.
We will be using “https://www.globalsqa.com/angularJs-protractor/WebTable/

As a demo website, here you will get a sample WebTable with fields like first name, last name, email, etc. Here we have applied a filter for email to minimize the size of the table.

We will be starting by launching the browser and navigating to the webpage. We have applied a filter for the email “PolGermain@whatever.com”, you can change it as per your requirement.

Once we get the filtered data from the table, now we need to locate the table and get the number of rows. The table will have multiple rows so, we need to use a list to store all the rows.

As we have stored all the rows in the list, now we need to iterate through each rows to fetch the columns and store the column data in another list.

Example :

Abc1
Xyz2
table has 2 rows and 2 columns

When we are iterating through the 1st row we will get data as Abc and 1 and store it in the list ’as rowdata[Abc, 1] similarly data from the 2nd row will be stored as rowdata[Xyz, 2].When we are iterating through the 2nd row the data from the 1st row will be overwritten.
That’s why we will need one more list ‘webRows ’ to store all the rows.
In the below code snippet, here we are iterating through all the columns from each row one by one and finally storing all the rows in the list WebRows.

How to access table data with the column index?

We have successfully extracted the table data now you can use this data as per your requirement

To do this we need to iterate through the list ‘webRows’ where we have our table data stored. We will be accessing all the columns by their index. In this case, you should know the column index you want to access.  The column index always starts from 0.

Below is the complete code snippet for the above-mentioned steps. You need to update related Xpaths in case you are not able to access the rows and columns with the given Xpaths.

web tables in selenium

When you execute the above code, you will get output in the below format

[Pol, Germain, 49, PolGermain@whatever.com, 1020.1597184937436]
Germain
[Pol, Germain, 62, PolGermain@whatever.com, 911.4520444579008]
Germain
[Pol, Germain, 10, PolGermain@whatever.com, 2809.911328973954]
Germain

Instead of accessing data by the index, you can access it using the column index also, and to do that you need to use the HashMaps instead of lists. HashMap will help to store column headers as keys and column data as values

Example:

NameId
Abc1
Xyz2
Table has 3 rows and 2 columns

Here Name and ID will be your keys and Abc, 1 and Xyz, 2 will be the values.

How to store and access table data using HashMap?

The code snippet below shows how to use HashMap to store data in key-value format.

web tables in selenium

Output-

size-4
Germain
{firstName=Pol, lastName=Germain, balance=1527.3558523201625, age=28, email=PolGermain@whatever.com}
Germain
{firstName=Pol, lastName=Germain, balance=250.18122282042322, age=20, email=PolGermain@whatever.com}
Germain
{firstName=Pol, lastName=Germain, balance=274.9486946306141, age=3, email=PolGermain@whatever.com}
Germain
{firstName=Pol, lastName=Germain, balance=1176.6629976866143, age=10, email=PolGermain@whatever.com}

Refer to the following GitHub repository for How to automate web tables in Java-Selenium.
https://github.com/priyanka1970/WebTables_with_Java_Selenium

Conclusion-

In this blog, we’ve delved into the powerful capabilities of Selenium WebDriver for handling web tables in Java. WebTables are a crucial part of web applications, often used to display large amounts of data in an organized manner. In Java Selenium, handling these WebTables efficiently is a key skill for any test automation engineer. Throughout this blog, we’ve explored various techniques to interact with WebTables, including locating tables, accessing rows and cells, iterating through table data, and performing actions like sorting and filtering.

Click here for more blogs on software testing and test automation.

Converting PDF to JSON in Java for Test Automation

Converting PDF to JSON in Java for Test Automation

Working with PDF documents programmatically can be a challenging task, especially when you need to extract and manipulate text content. However, with the right tools and libraries, you can efficiently convert PDF text to a structured JSON format.

Converting PDF to JSON programmatically offers flexibility and customization, especially in dynamic runtime environments where reliance on external tools may not be feasible. While free tools exist, they may not always cater to specific runtime requirements or integrate seamlessly into existing systems.

Consider scenarios like real-time data extraction from PDF reports generated by various sources. During runtime, integrating with a specific tool might not be viable due to constraints such as security policies, network connectivity, or the need for real-time processing. In such cases, a custom-coded solution allows for on-the-fly conversion tailored to the application’s needs.

For Example:

  • E-commerce Invoice Processing: Extracting invoice details and converting them to JSON for real-time database updates.
  • Healthcare Records Management: Converting patient records to JSON for integration with EHR systems, ensuring HIPAA compliance.
  • Legal Document Analysis: Extracting specific clauses and dates from legal documents for analysis.

Free tools are inadequate for real-time, automated, and secure PDF to JSON conversion. Coding your own solution ensures efficient, scalable, and compliant data handling.

In this blog, we’ll walk through a Java program that accomplishes using the powerful iTextPDF and Jackson libraries. Screenshots will be included to illustrate the process in Testing.

Introduction for Converting PDF to JSON in Java

PDF documents are ubiquitous in the modern world, used for everything from reports and ebooks to invoices and forms. They provide a versatile way to share formatted text, images, and even interactive content. Despite their convenience, PDFs can be difficult to work with programmatically, especially when you need to extract specific information from them.

Often, there arises a need to extract text content from PDFs for various purposes such as:

  • Data Analysis: Extracting textual data for analysis, reporting, or further processing.
  • Indexing: Creating searchable indexes for large collections of PDF documents.
  • Transformation: Converting PDF content into different formats like JSON, XML, or CSV for interoperability with other systems.

JSON (JavaScript Object Notation) is a lightweight data interchange format that’s easy for humans to read and write, and easy for machines to parse and generate. It is widely used in web applications, APIs, and configuration files due to its simplicity and versatility.

In this guide, we will explore how to convert the text content of a PDF file into a JSON format using Java. We’ll leverage the iTextPDF library for PDF text extraction and the Jackson library for JSON processing. This approach will allow us to take advantage of the structured nature of JSON to organize the extracted text in a meaningful way.

Prerequisites for Converting PDF to JSON in Java

Before we dive into the code, ensure you have the following prerequisites installed and configured:

  1. Java Development Kit (JDK)
  2. Maven for managing dependencies
  3. iTextPDF library for handling PDF documents
  4. Jackson library for JSON processing

Step-by-Step Installation and Setup for Converting PDF to JSON in Java

Install Java Development Kit (JDK)

The JDK is a software development environment used for developing Java applications. To install the JDK:

  • Visit the Oracle JDK download page.
  • Download the appropriate installer for your operating system (Windows, macOS, or Linux).
  • Follow the installation instructions provided on the website.

Verify the installation by opening a command prompt or terminal and typing:

java -version

You should see output indicating the version of Java installed.

Convert pdf to json - 1

Install Maven

Maven is a build automation tool used primarily for Java projects. It helps manage project dependencies and build processes. To install Maven:

  • Visit the Maven download page.
  • Download the appropriate archive file for your operating system.
  • Extract the archive to a directory of your choice.
  • Add the bin directory of the extracted Maven folder to your system’s PATH environment variable.

Verify the installation by opening a command prompt or terminal and typing:

mvn -version

maven version

Download IntelliJ IDEA

  1. Visit the Official Website: Go to the JetBrains IntelliJ IDEA download page.
  2. Step 2: Install IntelliJ IDEA on Windows
  3. Start IntelliJ IDEA: Open from the start menu (Windows).
  4. Complete Initial Setup: Import settings or start fresh.
  5. Start a New Project: Begin a new project or open an existing one.

Open IntelliJ IDEA:

Launch IntelliJ IDEA on your computer

Create or Open a Project

  • If you already have a project, open it. Otherwise, create a new project by selecting File > New > Project….
  • Name your project and select the project location
  • Choose Java from Language.
  • Choose Maven from the Build systems.
  • Select the project SDK (JDK) and click Next.
  • Choose the project template (if any) and click Next.
  • Then click Create.
Open project to convert pdf to json

Create a New Java Class

  • In the Project tool window (usually on the left side), right-click on the (src → test → java) directory or any of its subdirectories where you want to create the new class.
  • Select New > Java Class from the context menu.

Name Your Class

  • In the dialog that appears, enter the name of your new class. For example, you can name it PdfToJsonConversion.
  • Click OK/Enter.
pdf to json conversion
java file

Add the following dependencies to your pom.xml file for Converting PDF to JSON in Java:

json file

Write Your Code to Convert PDF to JSON in Java

  • IntelliJ IDEA will create a new .java file with the name you provided.
  • You can start writing your Java code inside this file. 

The Java Program to Covert PFT to JSON

Here is the complete Java program that converts a PDF file to JSON:

testing.json file

Explanation

Let’s break down the code step by step:

1. Dependencies

Jackson Library:

ObjectMapper, SerializationFeature, ArrayNode, ObjectNode: These are from the Jackson library, used for creating and manipulating JSON objects.

iText Library:

PdfDocument, PdfPage, PdfReader, PdfTextExtractor: These classes are from the iText library, used for reading and extracting text from PDF documents.

TestNG Library:

@Test: An annotation from the TestNG library, used for marking the convertPdfFileToJson method as a test method.

Java Standard Library:

File, IOException, ArrayList, List: Standard Java classes for file operations, handling exceptions, and working with lists.

2. Test Annotation

The class PdfToJsonConversion contains a static method convertPdfFileToJson which is annotated with @Test, making it a test method in a TestNG test class.

3. Method convertPdfFileToJson:

This method handles the core functionality of reading a PDF and converting its content to JSON.

4. Input and Output Paths:

inputPdfPath specifies the PDF file location, and outputJsonPath defines where the resulting JSON file will be saved.

5. PDF to Text Conversion:

  • Create a PdfDocument object using a PdfReader for the input PDF file.
  • Get the number of pages in the PDF.
  • Loop through each page, extract text using PdfTextExtractor, and add the text to contentList.
  • Handle any IOException that may occur.

6. Creating JSON Objects:

  • Create an ObjectMapper for JSON manipulation.
  • Enable pretty printing with SerializationFeature.INDENT_OUTPUT.
  • Create an ArrayNode to hold the content of each page.

7. Adding Page Content to JSON:

  • Iterate over contentList to process each page’s content.
  • For each page, create an ObjectNode and set the page number.
  • Split the page content into lines, then create another ObjectNode to hold each line with its number as the key.
  • Add the linesObject to the pageNode and then add the pageNode to pagesArray.

8. Writing JSON to File

  • Create a File object for the output JSON file.
  • Use the ObjectMapper to write pagesArray to the JSON file, handling any IOException.
  • Print a confirmation message indicating the completion of the process.

9. Output

The program outputs the name of the JSON file once the conversion is complete.

Running the Program

To run this program, ensure you have the required libraries in your project’s classpath. You can run it through your IDE or using a build tool like Maven.

  1. Open your IDE and load the project.
  2. Ensure dependencies are correctly set in your pom.xml.
  3. Run the test method convertPdfFileToJson.

You should see output similar to this in your console: Content stored in What is Software Testing.json. The JSON file will be created in the specified output path.

JSON Output Example

Here’s a snippet of what the JSON output might look like.

Output

Conclusion

Converting PDF text content to JSON can greatly simplify data processing and integration tasks. With Java, the iTextPDF, and Jackson libraries, this task becomes straightforward and efficient. This guide provides a comprehensive example to help you get started with your own PDF to JSON conversion projects.
https://github.com/mangesh-31/PdfToJsonConversion

Click here to read more blog like this.

Effective Locator Strategy for Mobile App Automation using Appium

Effective Locator Strategy for Mobile App Automation using Appium

Introduction:

Mobile App Automation using Appium involves various ways to locate elements for effective testing. In this blog, we’ll se the Mobile app automation Using Appium Inspector, we can inspect elements on both iOS and Android devices.

Now we’ll go for locating the Android Element

  • Mobile App Testing tools are available in the market right now are as follows:
    • Katalon
    • Appium
    • Espresso
    • XCTest
    • Robotium
    • Selndroid
    • Flutter
    • Robot Framework
    • iOS-driver
    • Xamarin

So currently we are going to Inspecting the Locator Strategy for Mobile App Automation using Appium, For the initial setup of Appium for Android device you can refer this blog How To Configure Our System For Real Mobile App Automation. This  blog will guide you for the mobile app automation using Appium setup on Android device.

We have various ways to locate the elements for mobile app automation using Appium Inspector, mainly we have the following ways to locate the elements:

  • Id
  • Xpath
  • Customized Xpath
  • Accessible Id

First, we’ll see how to locate the specific element for Mobile App Automation using Appium

After starting the session on an Android phone you will see the below Appium inspector window

Mobile App Automation - 1

In this image, you can see the mobile screen, App source, and Selected Element tabs.

When you select the particular element on the mobile screen displayed on Appium Inspector, You will see the below image, I have selected the C button from a calculator for mobile app automation using appium.

Mobile App Automation using Appium - 2

Now we can see the DOM content is loaded in the App Source tab, and the Attributes and values will be displayed in the Selected Element tab.

Now we’ll see how to locate the element from the Selected Element tab.

Select Element

In the above image you can see the attribute and values to locate the element

Now we can see the Locator strategies to locate this element for mobile app automation using appium. First, we’ll see locating the element using the Id

First, we’ll have to see the available Attributes for that particular element and then try to locate the element. So copy the ID from given Selected Element tab as shown below

Copied-id

So now We’ll see how to check whether the Id is a valid locator or not.

  • For that first click on the Search bar
Search Bar
  • Then make sure you have selected the correct locator Strategy as shown in the below image.
search for element
  • Now after clicking on the search element, you will get to see the identified element as shown in the below image
Mobile App automation using appium. -3
  • As the element is getting highlighted it indicated that we can use that ID to locate that particular element

Now we’ll see locating elements using XPath for Mobile App Automation using Appium

In a similar way to Id we can locate the element using Xpath, So for first we need to click on the Xpath shown in the below image.

Mobile App Automation using Appium-4

Now click on the search button explained above

XPath

Make sure that you have selected the XPath as Locator Strategy as shown. Then Paste the copied XPath in the Selector Box and click on the Search Button, so then you can see the below image the element is located by the XPath

Locate XPath

The element is getting highlighted and that means we can use this XPath to locate this element

Now we’ll see how to use customized XPath for Mobile App Automation

This allows us to handle parameterization and overcome limitations when ID or XPath is not available by default. So for that, we need to know how we can create XPath

  • The first step is you need to find the class for that particular element
Mobile App AUtomation-5
  • As you can see the above image, class is present for that particular element. So first step is we need to copy this class value
  • The next step is to choose the attribute you want to use the value of.
Mobile App Automation-6
  • These are the various attributes you can use to customize XPath
  • So after that, you can create the Customized XPath, So here is a sample XPath I have used to locate the equal button from the Calculator app
  • In this XPath, I have chosen text attribute. So in the below image, you can see the combination of class and attribute and value. This is how we can create customized XPath
XPath Mobile App Automation
  • As shown in the below image you can see the Located element
Located Elements
  • So when the requirement is there to create a parameterized locator or ID is not available, at that time you can use Customized XPath

For accessibility Id you can follow similar steps like ID to locate the element. The only condition is Accessibility ID should be available for that particular element

Now we’ll go for locating the iOS element for Mobile App Automation using Appium

For iOS automation We’ll be going to see how we can locate the element. To locate elements on iOS devices following strategies are available

  • Accessibility Id
  • XPath
  • Customized XPath

Now we’ll see how to locate the element using Accessibility ID on iOS device.

For that, we’ll have to start the Appium Session on iOS. After starting the Appium session on iOS device you  will get to see the below window of Appium inspector

Appium Inspector

This will be the home page of the calculator on the iOS App. On this screen, you can see three windows Mobile screen, App Source, and Selected  Element. When you select any of the elements displayed on the Mobile screen the screen will be shown below.

Mobile Screen

In the above Image, I have selected the AC button which is for All Clear. After selecting that element the DOM content is loaded in the App Source window and in the Selected Element window we can see the attributes and values that can be used for inspecting the elements.

We have so many options to locate the element as you can see in the Selected Element window. We have accessibility ID, XPath, and customized XPath for Mobile App Automation using Appium.

Now we’ll see how to locate the element using accessibility id for Mobile App Automation using Appium

So first we’ll go to search for element as shown in the below image

Element Search

As shown in the above image you can see that I have selected Locator Strategy as the Accessibility ID and the value I have passed the accessibility ID got from the Selected Element window. Now, I’ll click on the Search button.

The system will display the result window below.

Search Button

As shown in the screenshot, the AC button is highlighted after successfully finding the window element. The count for the found element is 1, and you can use this accessibility ID to locate this specific element.

Note: So for locating the elements using XPath and customized XPath you can refer the steps mentioned for Android.

  • Preffered Locator Strategy: As you can see the Selected element window, We have multiple options to locate the element for Mobile App Automation. So there might be a confusion to select the correct locator strategy. So here are some key points which you can consider while choosing the locator strategy
    • Most preferred locator strategy will be id (Android) or accessibility id (iOS). Because id’s are designed to be unique for direct access.
    • name locator strategy can be used if the particular element have the unique name which can be used to locate element.
    • The XPath are more likely to use if id not available or we have requirement to create locator which needs to be parameterized.

Conclusion:

As we see, we have multiple ways to locate the elements on the Mobile Application. Here in this blog, we got to know the the locator strategies to locate the elements on Android and iOS Application for Mobile App Automation using Appium. So you have multiple options to locate the elements, From which you have to decide which strategy suits best for your requirements. So as mentioned above id is fastest way to locate elements, But you have choice to use XPath and customized XPath for parameterization. https://github.com/appium/appium-inspector/releases

Overall, this blog provides an overview of how to locate elements Mobile App Automation using Appium Inspector. Additionally, it explains the various locator strategies you can choose based on the requirements of your test script.