Search This Blog

Breaking

Saturday, 26 March 2022

March 26, 2022

Azure -> Java -> How to read a file without downloading present inside a azure container?

 If you want to read a file from a Azure container folder without downloading to it, you can easily do it by using azure API's. The correct way to download a file from azure container folder is to first ensure that the container is present or not.

So if you want to check that the container is available or not, please check this post











Once you have ensured that the container is present, you can use below method:

  public static void readFile(String containerName, String fileName) throws IOException {
        BlobServiceClient blobServiceClient = createBlobServiceClient();
        BlobContainerClient containerClient =blobServiceClient.getBlobContainerClient(containerName);
        BlobClient blob = containerClient.getBlobClient(fileName);
        BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(blob.openInputStream()));
        String line;
        while ((line = bufferedreader.readLine()) != null)
        {
            System.out.println(line);

        }

    }

Here we are reading the file after converting it into a stream.

March 26, 2022

Azure -> Java -> How to download a file present inside a azure container folder ?

If you want to download a file from a Azure container folder, you can easily do it by using azure API's. The correct way to download a file from azure container folder is to first ensure that the container is present or not.

So if you want to check that the container is available or not, please check this post










Once you have ensured that the container is present, you can use method:

 public static void downloadFile(String containerName, String fileName) {
        String fileFromContainer = "";
        BlobServiceClient blobServiceClient = createBlobServiceClient();
        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
        for (BlobItem blobItem : containerClient.listBlobs()) {
            if(blobItem.getName().contains("/")) {
                fileFromContainer = StringUtils.substringAfterLast(blobItem.getName(), "/");
            } else {
                fileFromContainer = blobItem.getName();
            }

            try {
                if (fileFromContainer.contains(fileName)) {
                    BlobClient blobClient = containerClient.getBlobClient(blobItem.getName());
                    File downloadedFile = new File(downloadedPath + fileFromContainer);
                    System.out.println("\nDownloading blob to\n\t " + downloadedFile);
                    blobClient.downloadToFile(downloadedFile.toString());
                }
            } catch (Exception e) {
            }
        }
    }

downloaded file path will be your local folder path.

static String downloadedPath = "data\\downloaded\\";


March 26, 2022

Azure-> Java-> How to download a file present inside a container?

If you want to download a file from a Azure container, you can easily do it by using azure API's. The correct way to download a file from azure container is to first ensure that the container is present or not.

So if you want to check that the container is available or not, please check this post









Once you have ensured that the container is present, you can use below method:

 public static void downloadFiles(String containerName) {
            BlobServiceClient blobServiceClient = createBlobServiceClient();
            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
            for (BlobItem blobItem : containerClient.listBlobs()) {
                BlobClient blobClient = containerClient.getBlobClient(blobItem.getName());
                File downloadedFile = new File(downloadedPath + blobItem.getName());
                System.out.println("\nDownloading blob to\n\t " +  downloadedFile);
                blobClient.downloadToFile(downloadedFile.getPath());
            }
        }

where the downloadedPath is your local path from where you want to store the file.

static String downloadedPath = "data\\downloaded\\";

Once the file is downloaded, you can read the content from it also.
March 26, 2022

Azure- > Java -> How to upload a file inside a container?

 If you want to upload a file inside a Azure container, you can easily do it by using azure API's. The correct way to upload a file to azure container is to ensure always that the container is present or not.

So if you want to check that the container is available or not, please check this post











Once you have ensured that the container is present, you can use method:

blobClient.uploadFromFile(uploadPath + fileName);


this method takes your file path from local.

upload path can be :  static String uploadPath = "data\\upload\\";

where data is a folder in your projecct root directory.

and file will be your file name present inside the path.

The method is :


public static void uploadFile(String containerName, String fileName) {

            BlobServiceClient blobServiceClient = createBlobServiceClient();

            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);

            BlobClient blobClient = containerClient.getBlobClient(fileName);

            System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl());

            blobClient.uploadFromFile(uploadPath + fileName);

        }


Once this is done, you can ensure by listing files present inside the container and validate that your file is present inside it or not.




Thursday, 17 March 2022

March 17, 2022

Azure- > Java -> How to connect with azure to verify that your container exist or not?

 Currently, I am working on a project where we are migrating to the Azure cloud. To test some of the functionality with automation, I need to connect my java code to azure so that I can view, upload

 and download all the files from the container.


To perform this activity, first I created a trial account in Azure. After that, I created a container inside my azure account.

Now, My use case was to access the container first with java. To perform this operation, I first need to add a jar :




 







<dependency>

            <groupId>com.azure</groupId>

            <artifactId>azure-storage-blob</artifactId>

            <version>12.15.0</version>

 </dependency>

 

 With this dependency, I will be able to access the storage/ container inside the azure cloud.

 Once I added this jar, the second step was to create a connection with Azure.

 

 For this, we can create a connection string first:

 

 String connectStr = "DefaultEndpointsProtocol="+DEFAULT_PROTOCOL+";AccountName="+ ACCOUNT_NAME+";AccountKey="+YOUR_KEY+";EndpointSuffix="+END_POINT_SUFFIX+"";

 

 in this, following detail you can fill, as per your credentials:

    public static final String ACCOUNT_NAME = "javacodevalidationdeepak";

    public static final String DEFAULT_PROTOCOL =  "https";

    public static final String END_POINT_SUFFIX = "core.windows.net";

  Apart fromt this, you will need account key, which you can find from azure.

  

  Once your connection string is ready, you can create a connection with azure with this:

  

    String connectStr = createConnectionString();

    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();

    return blobServiceClient;

Once the connection is built, you can access the containers and files present inside it with various already available APIs.

For Example, if you need to check, that the container which you have created is availabe or not, you can use below code sinppet:


  BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);

        if (containerClient.exists()) {

            System.out.println("Container " + containerName + " is available");

        } else {

            System.out.println("Container " + containerName + " does not exist.");

       }


Similarly, if you need to list down all files inside the container, read the particular file or download a particular file, you can use various Microsoft APIs for that.

  

I will share further, how to upload and download files from the azure container
in the next articles.

Till then, keep learning.


Friday, 18 February 2022

February 18, 2022

[Fixed ] : JsonPath -> How do you parse JSON with a colon in the name?

 Recently, I encountered a problem where when in my JSON response, a key came with a colon in it.

When I tried to get the value of this key with the help of JSONPath, I encountered an issue as inside the string I was not able to get it.

For Example:

{ "title": "value1", "link": "value2", "media:info": "value3" }
















If you want to get the first value from it, you can extract :

$.title

It will return "value1".

But if you will try to do the same for the third key, it wouldn't work because of the colon
in it. 

So in this case, if you would like to access it:

$.['media:info']

It will return the value.

Same then you can use it in your json Path library java code also.


Tuesday, 8 February 2022

February 08, 2022

[Java] [Apache PDFBox] Create a single page with three different pdf pages

If we want to create a pdf by putting 3 pages side by side, we can use the below approach.

Here we are trying to merge first 2 pages side by side and then, we are merging the third page with this newly created page.

This code is very useful if you are performing comparison between 2 pdf's and want to show the result of that comparison also.



import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.multipdf.LayerUtility;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;

import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class pdfSideBySide {

public static void mergeFileSideBySide(String file1Path, String file2path, String mergedfilepath,
String mergedfilename, String layername1, String layername2) {
File pdf1File = new File(file1Path);
File pdf2File = new File(file2path);
File outPdfFile = new File(mergedfilepath);
PDDocument pdf1 = null;
PDDocument pdf2 = null;
PDDocument outPdf = null;
try {
pdf1 = PDDocument.load(pdf1File);
pdf2 = PDDocument.load(pdf2File);
outPdf = new PDDocument();

// Create output PDF frame
PDRectangle pdf1Frame = pdf1.getPage(0).getCropBox();
PDRectangle pdf2Frame = pdf2.getPage(0).getCropBox();
PDRectangle outPdfFrame = new PDRectangle(pdf1Frame.getWidth()+pdf2Frame.getWidth(), Math.max(pdf1Frame.getHeight(), pdf2Frame.getHeight()));

// Create output page with calculated frame and add it to the document
COSDictionary dict = new COSDictionary();
dict.setItem(COSName.TYPE, COSName.PAGE);
dict.setItem(COSName.MEDIA_BOX, outPdfFrame);
dict.setItem(COSName.CROP_BOX, outPdfFrame);
dict.setItem(COSName.ART_BOX, outPdfFrame);
PDPage outPdfPage = new PDPage(dict);
outPdf.addPage(outPdfPage);

// Source PDF pages has to be imported as form XObjects to be able to insert them at a specific point in the output page
LayerUtility layerUtility = new LayerUtility(outPdf);
PDFormXObject formPdf1 = layerUtility.importPageAsForm(pdf1, 0);
PDFormXObject formPdf2 = layerUtility.importPageAsForm(pdf2, 0);

// Add form objects to output page
AffineTransform afLeft = new AffineTransform();
layerUtility.appendFormAsLayer(outPdfPage, formPdf1, afLeft, layername1);
AffineTransform afRight = AffineTransform.getTranslateInstance(pdf1Frame.getWidth(), 0.0);
layerUtility.appendFormAsLayer(outPdfPage, formPdf2, afRight, layername2);
outPdf.save( outPdfFile + "//" + mergedfilename);

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (pdf1 != null) pdf1.close();
if (pdf2 != null) pdf2.close();
if (outPdf != null) outPdf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void moveFile(String sourcefilepath, String targetfilepath) throws IOException {
Path temp = Files.move
(Paths.get(sourcefilepath),
Paths.get(targetfilepath));

if(temp != null)
{
System.out.println("File renamed and moved successfully");
}
else
{
System.out.println("Failed to move the file");
}
}

private static void mergePDF(String filepath) throws IOException { ;
File folder = new File(filepath);
File[] listOfFiles = folder.listFiles();
PDFMergerUtility pdfMerger = new PDFMergerUtility();
pdfMerger.setDestinationFileName(filepath+ "Results.pdf");
for(File file: listOfFiles) {
pdfMerger.addSource(file);
}
pdfMerger.mergeDocuments(null);
System.out.println("PDF Documents merged to a single file");
}

public static void main(String[] args) throws Exception {
String file1path = "src//main//resources//RPT_CPSPM_18875164_7469.pdf";
String file2path = "src//main//resources//sample-pdf-file.pdf";
String layername1 = "left";
String layername2 = "right";
String mergedfilepath = "src//main//resources//";
String mergedfilename = "abc.pdf";
String totalMergedfilepath = mergedfilepath+mergedfilename;

mergeFileSideBySide(file1path,file2path,mergedfilepath,mergedfilename,layername1,layername2);

String finalReportPath = "src//main//resources//Report//";
String finalreportname = "finalymegred.pdf";
String layername3 = "top";
String layername4 = "bottom";
Thread.sleep(1000*2);
mergeFileSideBySide(totalMergedfilepath, "sample-pdf-file.pdf",finalReportPath,
finalreportname,layername3,layername4);

mergePDF(finalReportPath);

}
}

Saturday, 22 January 2022

January 22, 2022

[Java] [Apache PDFBox] Create new pdf from images and merge PDF's into a single pdf



If we want to create a new pdf with multiple images, then we can do it by using apache pdfbox library.

Here we are trying to create a pdf from an image file first. Later we will merge it for creating a single pdf.

This code is very useful if you are trying to create a report for your pdf comparison, Where you want to show case all your comparison results into a single pdf.



Check the other article on PDF: Create Single page from 3 pages in pdf




public static void main(String[] args) throws IOException {

        String filePath = "Path to \\TestImages\\";

        File folder = new File("Path to \\TestImages\\");

        File[] listOfFiles = folder.listFiles();

        List<File> pdfFileNames = new ArrayList<>();

        List<String> fileNames = new ArrayList<>();

        for (File listOfFile : listOfFiles) {

            fileNames.add(listOfFile.getName());

        }

        System.out.println(fileNames);

        for(String file : fileNames){

          String pdfFile=   createpdfFromFile(file,filePath);

          pdfFileNames.add(new File(pdfFile));

        }

        mergePDF(pdfFileNames,filePath);

    }



private static String getFileNameWithoutExtension(File file) {

        String fileName = "";

        try {

            if (file != null && file.exists()) {

                String name = file.getName();

                fileName = name.replaceFirst("[.][^.]+$", "");

            }

        } catch (Exception e) {

            e.printStackTrace();

            fileName = "";

        }

        return fileName;

    }


private static  void mergePDF(List<File> pdfFileNames, String filepath) throws IOException {

        PDFMergerUtility pdfMerger = new PDFMergerUtility();

        pdfMerger.setDestinationFileName(filepath+ "Report.pdf");

        for(File file: pdfFileNames) {

         pdfMerger.addSource(file);

        }

        pdfMerger.mergeDocuments(null);

        System.out.println("PDF Documents merged to a single file");

    }


    private static String createpdfFromFile(String filename, String filePath) throws IOException {

            String file =  filePath + filename;

            System.out.println(file);

            File fileWithoutExtention = new File(file);

            PDDocument document = new PDDocument();

            InputStream in = new FileInputStream(file);

            BufferedImage bimg = ImageIO.read(in);

            float width = bimg.getWidth();

            float height = bimg.getHeight();

            PDPage page = new PDPage(new PDRectangle(width, height));

            document.addPage(page);

            PDImageXObject pdImage = PDImageXObject.createFromFile(file, document);

            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true,true);

            contentStream.drawImage(pdImage, 20f, 20f);

            contentStream.close();

            in.close();

            String newPDFFile =filePath + getFileNameWithoutExtension(fileWithoutExtention) + ".pdf";

            document.save(newPDFFile);

            document.close();

            return newPDFFile;

    }

Thursday, 23 December 2021

December 23, 2021

How to access shadow dom [Closed] element in selenium?

Recently someone posted a challenge in linked to fill value inside a field. It looks a simple problem as from UI page, it was just a normal input field. I tried to check the dom for the element, and got to know that this is inside closed shadow dom.

So if you also find an element inside shadow dom which is closed, it means it can not be interactable. So the first approach to solve such issue is to connect with your developer teams. Yes, they can provide you an alternate way to access that element (at least in lower environment.)

But if this is not possible, then use selenium keypress method by using a common use case of an end user.

The same way I solved that challenge also. :) 

So, Just think, that this field is not accessible from mouse. In this situation, usually we interact on the page items with keyboard "TAB" key. Same approach, we can take here as well.

We just need to find an element which is accessible from selenium usual locator finding techniques, bring focus to that element, sometimes just by clicking on it or if it is an input field, just by passing a empty string, then press tab key until you reach to that element.

Bingo, you are now inside that field, now if you need to perform:

1. Click on that element, press "Enter" key

2. If it is an input field, you can simply use "send_keys" method

To demonstrate it, I created a sample page having shadow root as closed in it.











In the above page, the "Age field " is not inside any shadow dom element. And the first name and last name fields are inside the shadow dom[closed].

So to enter value inside the firstname field, I entered value or bring my focus to "Age field " first. And then I used "Tab Key" which brought me to the "First name" field. 

Once, My focus is available in the first field, I used action class to send my input to that field.

driver.get('file:///C:/Users/username/Documents/shadowDom.html')

driver.maximize_window()

ageElement = driver.find_element(By.ID, 'age')

ageElement.send_keys(30)

ageElement.send_keys(Keys.TAB)

actions = ActionChains(driver)

actions.send_keys("My name")

actions.perform()

sleep(10)

driver.quit()


Check the other post, how to interact with an element which is inside shadow dom [Open] tag.

Wednesday, 22 December 2021

December 22, 2021

How to access shadow dom [Open] element in selenium?

Recently, I saw in stack overflow that many people were posting question that they are not able to get element which is inside a shadow DOM. So I tried to solve this problem. Below is my learning:


1. If the element is inside the shadow root, first check that is that shadow root is open or close. If it is Open, than you can directly access it by using java script executor.

2. If the element is inside the shadow root which is closed, you can not use it. If you need to interact with such element, you can use keyboard key press operations.


So the first application which I got was an application where the shadow root was open. So it became easy for me. My take here is, first check your locator in developer console. I will explain how we can do it.


So lets first open a page  where this issue occurs . And the easy to accessible page is your chrome download page. If you will inspect "Search Downloads"  element on that page, you will find that it is inside shadow root.












So if we try to find element by using xpath, the xpath value is : '//*[@id="searchInput"]

But if you will try to search this XPATH, you will find it is not highlighting the element. And if you use it in your code, then you will see below error:









So the reason for it's not working is not that your xpath is not correct, it's just that this element is present inside shadow root element.

And if any element is present inside shadow root [Open], you can not access it with normal locator strategies. 

So to access this element, you will have to first find css locator for the topmost element from where the shadow root starts. In this page, it is from the beginning. So just find the locator of the element which is before the shadow root.












Here it is : body > downloads-manager

Now, you will move to find the second shadow root and you need to find the locator of element before that element.

Here it is: #toolbar


















Similar way, when we reach to the input field, we see 2 more shadow root. When we find the css locator of elements before that shadow root, it's like:

Third element : #toolbar
Fourth element: #search

And then at the end, our element appears and it's css locator is : #search

So now, we have all the locators, let's test these locators in browsers console page.

So to execute the CSS locator in browser console page, we use below method:

document.querySelector()

So now we will give our first locator to this:

document.querySelector('body > downloads-manager')

After this, we have our first shadow root. So to navigate inside the shadow root, we use below method:
shadowRoot.querySelector()

So now our locator will become:

document.querySelector('body > downloads-manager').shadowRoot.querySelector('#toolbar')

Similarly, we will have to add all our shadow root locator.

The final output will be like:

document.querySelector('body > downloads-manager').shadowRoot.querySelector('#toolbar').shadowRoot.querySelector('#toolbar').shadowRoot.querySelector('#search').shadowRoot.querySelector('#searchInput')


Now when you will hit this element in your developer console, you will see it is highlighting the element.









Now this, entire locator, we will have to use in our selenium code. (Python)

driver.get('chrome://downloads/')
driver.maximize_window()

# Using java script executor

searchDownload = driver.execute_script("return document.querySelector('body > downloads-manager').shadowRoot.querySelector('#toolbar').shadowRoot.querySelector('#toolbar').shadowRoot.querySelector('#search').shadowRoot.querySelector('#searchInput')")

searchDownload.send_keys("Order_ID_2363900762.pdf")
sleep(10)
driver.quit()

And when you will run this code, it will enter your text inside the field.













So this is how you can enter text inside a shadow root element.