Tuesday, February 28, 2023

Fetch Text from Image & PDF Using Selenium Java

In this blog, we will learn how we can fetch data from images and PDFs.

This Blog Contains:

  • Read Text From Image Using OCR with Tesseract (tess4j)
  • Reading PDF Text Using PDFUtil
  • Save PDF as Image Using PDFUtil
  • Extract Images From PDF Using PDFUtil

Fetch Text From Image In Selenium

To get a text from the Image in selenium, we use Optical Character Recognition (OCR) with Tesseract (tess4j). Tesseract Supports UTF-8 Unicode.

  • First, we need to create a folder with the name “tesseract” in our project and put trained data in that folder. You can find trained data for any language from the below URL:

https://github.com/tesseract-ocr/tessdata

Just Download eng. trained data for English Language and put it into Tesseract Folder for your project.

  • Add below is maven dependency for tesseract (tess4j):
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.5.4</version>
</dependency>
  • Below is the Java code to fetch text from the image:
ITesseract image = new Tesseract();
image.setDatapath(“Location for TessData Folder”);
image.setLanguage(“eng”);
String str1 = image.doOCR(new File(“Location Of Image”));

Read Also:- Process Java Script Executor in Selenium Test Automation

Fetch Text From PDF

  • Add Below Maven Dependency For PDFUtil
<dependency>
<groupId>com.testautomationguru.pdfutil</groupId>
<artifactId>pdf-util</artifactId>
<version>0.0.3</version>
</dependency>
  • Below Java Code is used to Read Text From PDF
String pdfLocation = “Location where we have PDF File”;
PDFUtil pdfUtil = new PDFUtil();
String text = pdfUtil.getText(pdfLocation);
  • Below Java Code is used to Save PDF as an Image
String folderLocation = “Location Where we need to save Image”;
String pdfLocation = “Location where we have PDF File”;
PDFUtil pdfUtil = new PDFUtil();
pdfUtil.setImageDestinationPath(folderLocation);
pdfUtil.savePdfAsImage(pdfLocation);
  • Below Java Code is used to Fetch Image From PDF
String folderLocation = “Location Where we need to save Image”;
String pdfLocation = “ Location where we have PDF File”;
PDFUtil pdfUtil = new PDFUtil();
pdfUtil.setImageDestinationPath(folderLocation);
pdfUtil.extractImages(pdfLocation);

 Original Source:-https://www.devstringx.com/using-selenium-java

Monday, February 27, 2023

How To Read Emails Using Java Imaps?

 Here we are going to see how email content can be read by Java-Selenium using Java mail API.

Method to Get the Body Content of the Mail

It requires 3 parameters username, password, and the subject of the mail. When this method will be called then first it will authenticate the Username and Password and then will call getFolder() by storing the reference variable and passing the inbox as a parameter so that it will check the inbox folder only.

All the messages are stored in an array so will iterate one by one by using For Loop. When The subject will equal to the given in parameter then it will return the body content of that message.

Java Imap

Read Also: – Process Java Script Executor in Selenium Test Automation

The authentication () method is used to get sessions like hostname, port number user name, password, etc by using Java mail API.

Java mail API

getTextFromMimeMultipart () method is used to get the text from Multipurpose Internet Mail Extensions. First, it will get the count of the MIME multipart message then it will iterate one by one to get the body part of that specific MIME part.

Read Also:- Execution Time of Multiple Methods In Selenium Java

isMimeType() – this method takes arguments like “text/plain” for plain text, “text/HTML” for normal webpages.. etc. The return type is boolean( true or false). If the return type is true then it will get the content of that message and if the return type is false then it will be ignored.

Reading E-Mail

FAQs

  • In Java, how do I read unread emails in Gmail?

Make the following code modifications:

inbox.open(Folder.READ ONLY); should be replaced with inbox.open(Folder.READ WRITE);

After reading the message, position the flag as follows: set flag(‘message’, ‘Flags.Flag.SEEN, true);

  • What is JavaMail API?

A platform- and protocol-neutral framework for creating mail and messaging applications are offered by the JavaMail API. The JavaMail API is a component of the Java EE platform as well as a package that may be used with the Java SE platform.

  • How can I read a mailbox in java?

JavaMail: How to Read a Message Import the necessary packages.

  • Search for the JavaMail session.
  • If necessary, override the JavaMail session properties.
  • Obtain a Store object from the Session, then use the connect method of the Store object to establish a connection with the mail server.
  • the INBOX folder, please.
  • What is IMAP in Java?

The term Internet Message Access Protocol (IMAP) is an acronym. It is an Internet protocol at the Application Layer that enables email clients to access email on distant mail servers. A typical IMAP server listens on the well-known port 143.

——————————————————————————————————————————————————

Our software test engineers have written some other detailed blogs on Java  you might find of interest

Sunday, February 26, 2023

Windows Application Driver (WinAppDriver) With C#

 

What Is Windows Application Driver (WinAppDriver)?

WinAppDriver is a service to support Selenium-like UI Test Automation on Windows Applications. This service supports testing Universal Windows Platform (UWP), Windows Forms (WinForms), Windows Presentation Foundation (WPF), and Classic Windows apps on Windows 10 PC.

How to Install & Run WinAppDriver?

Winappdrvier Window setting

Open WinAppDriver.exe from the installation directory (C:\Program Files (x86)\Windows Application Driver)

Installation directory in WinAppDriver

After That, WinAppDriver will be running on the test machine listening to requests on the default IP address and port (127.0.0.1:4723).

Driver listening for request

Now you are ready to run any of your Tests Scripts.

Read Also:- Most Popular Tools for Automation Testing

Required Packages in Project’s Solutions

We need to add packages ‘Microsoft.WinAppDriver.Appium.WebDriver’ in the current solution from the Nugget as below I have attached the screenshot of the packages.

WinAppDriver webdriver Appium

How to Inspect Element on Windows Application?

First, we need to Download the Win SDK file from https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/

Download Win SDK file

By using ‘inspect.exe’ we can inspect the element present in the windows application

windows application

FOR EXAMPLE, I HAVE WRITTEN A DEMO SCRIPT TO PERFORM ACTION ON THE OUTLOOK APP:

OUTLOOK APP

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<TestRunParameters>
<Parameter name="AppId" value="C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"/>
<Parameter name="sessionSingletonUrl" value="http://127.0.0.1:4723"/> //When open WinAppDriver You will get this value.
</TestRunParameters>
</RunSettings>

TestRunParameter

using NUnit.Framework;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System;
using System.Threading;
namespace WinAppDriver
{
internal class OutlookDemo
{
private static WindowsDriver<WindowsElement> session;
[Test]
public void OpenOutlook() {
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", TestContext.Parameters["AppId"]); //Getting AppId from test.runsettings.
session = new WindowsDriver<WindowsElement>(new Uri(TestContext.Parameters["sessionSingletonUrl"]), appCapabilities);// Getting sessionSingletonUrl from test.runsettings
session.SwitchTo().Window(session.WindowHandles[0]);
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
session.Manage().Window.Maximize();
session.FindElementByName("Home").Click();
session.FindElementByName("Send / Receive").Click();
session.FindElementByName("View").Click();
session.FindElementByName("Help").Click();
session.Close();
}
}
}

NOTE: Before executing this script you need to select test.run settings to your current project and WinAppDriver c# should be open.

test run setting

Windows Application Driver (WinAppDriver) Pros

This is an open-source tool. It uses the web driver protocol. It’s free and developed by Microsoft. WinAppDriver can integrate with selenium and Appium projects in the C# language.

Conclusion

WinAppDriver is a very easy-to-use free tool and it is the best option for Automation on Windows applications rather than a paid one, which is easy to integrate and implement with existing automation projects.

FAQs

  1. How do I download the window application driver(WinAppDriver)?

Configuring and Installing WinAppDriver

  • Go to the windows application driver download Settings and select Developer Mode from the Developer menu.
  • Download Windows Application Driver (WinAppDriver) installer.
  • Download the .msi file and install it on your system.
  1. How do I set up WinAppDriver?
  • Install and run WinAppDriver
  • Run the installer on your Windows 10 computer to install and test the application you want to test on this computer.
  • Enable Developer Mode in Windows Settings.
  • Run WinAppDriver.exe from the installation directory (for example, C:\Program Files (x86)\Windows Application Driver).
  1. Does WinAppDriver support Windows 11?

WinAppDriver is a free tool (the code for WinAppDriver is not currently open source, but the FAQ says they are considering an option to make it open source). Supported on computers running WinAppDriver windows 11 (Home and Professional edition) and Microsoft Winappdriver Server 2021 operating system, it supports all Windows applications based on Windows Forms (WinForms).

  1. What is the difference between Winium and WinAppDriver?

Winium is an open-source test automation framework for testing WPF and WinForms applications. Like WinAppDriver, Winium is also based on the WebDriver protocol. On the other hand, Winium is not comparable to WinAppDriver in several respects.

To read more software testing-related articles and information from us here at Devstringx, kindly check out our other software testing-related blog.

Original Source:- Click Here

Is Chrome Developer Tool a Future For Test Automation?

  Devtools (Chrome Developer Tool)  is a powerful set of tools that helps web developers to build better applications. In this blog, we will...