Search This Blog

Breaking

Monday, 5 September 2022

September 05, 2022

[Selenium UI] -> How to do file upload from System and handling InvalidElementStateException in Webdriver?

 Whenever there is a use case of file upload from the system to your application, you should always check whether the DOM is having an element of the "Input" tag whose type is "File". If there is such an element available, you need to make sure that the element is present and you just need to pass the "File path" from your system to this element as a string.








For Example: If we need to tweet an image from our system to twitter, We can check "Is there any input tag available with a type file".


<input accept="image/jpeg,image/png,image/webp,image/gif,video/mp4,video/quicktime" multiple="" tabindex="-1" type="file" class="r-8akbif r-orgf3d r-1udh08x r-u8s1d r-xjis5s r-1wyyakw" data-testid="fileInput">


You will see that, there is an element available with an input field whose type is "File". Now, in your selenium script, you need to pass the path of your file.


imagePath = driver.find_element(By.XPATH, "//*[@data-testid='fileInput']")

imagePath.send_keys("C:\\Users\\username\\Documents\\Personal\\selenium.png")

It will upload the file to Twitter for the tweet.


If while doing this activity, you see InvalidElementStateException, it means the element is not clickable.

So you need to make sure, that the element is in active/clickable status before interacting with it.

WebDriverWait(driver,20).until(EC.element_to_be_clickable("element")).send_keys("C:\\Users\\username\\Documents\\Personal\\selenium.png")


The same can be done in Java:

new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(By.xpath(""))).sendKeys("imagePath");


This will resolve the issue and will upload the file.

Note: In both cases, it will not open any windows dialogue. So don't get confused that you need to use "AutoIT", "Robot Class" or any other hack to upload files there.


#invalidSateException #fileUploadInSelenium #HowToUploadFileInSelenium 


Monday, 4 April 2022

April 04, 2022

Azure -> Parquet File-> Python -> Without download, read and query on parquet file

 Python is very rich with data analytics libraries which brought me to use it for one of the use case. The same use case initially I tried to perform with Java also, but I found that the effort is more in java side. So just for the POC purpose, I choose python.

My sue case was to read and query on parquet file content without downloading it.

So to perform this activity, I converted the parquet file to streams with the help of BytesIO.

Once, I got the stream, I used pyArrow library which helped me to read this data into python data frames directly. 














blob_service_client_instance = createBlobServiceClient()

    blob_client_instance = blob_service_client_instance.get_blob_client(

    CONTAINER_NAME, 'userdata1.parquet', snapshot=None)

    blob_data = blob_client_instance.download_blob()

    stream = BytesIO()

    blob_data.readinto(stream)

    processed_df = pd.read_parquet(stream, engine='pyarrow')

    # print(processed_df.columns)

    return processed_df 


Once you have the data inside the dataframe, you can put query into it easily.

 processed_df = donwloadParquetFile()

    setDisplayForDataFrame(None,None,1000)

    print(processed_df)

    print (processed_df.head(3))

    print("---------------------Query Output--------------------------------")

    print(processed_df.query(query))


And this query will be your query.

In my case, I was passing it from my main method, which was int he form of : 

first_name == "Amanda" and last_name == "Jordan"


To show the results in console, we can set the option in panda dataframe.

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;

    }