File upload in Selenium,Java,Robot Class

      No Comments on File upload in Selenium,Java,Robot Class


File Upload is achieved using the Java API java.awt.Robot class where awt stands for Abstract window Toolkit.
1. Robot class in java mainly useful to generate native system input events for the purposes of test automations.
2. The primary usage of Robot class is to facilitate automated testing of Java platform implementations
Test Steps:
1. Open Chrome Browser.
2. Enter the URL https://www.zamzar.com/.
3. Click on the button -Add Files
4. Opens the window Dialog box
5. Enter the absolute path of the file : C:\SeleniumArtifacts\TestCaseData.xlsx
6. Click on Enter button in the keyboard.


Java Logic

Expected Result:
File is uploaded successfully

Keyboard Actions:
1. ctrl + v ->C:\SeleniumArtifacts\TestCaseData.xlsx
2. enter
Conclusion:
File Upload is achieved in Selenium Automation Tool using the Java API.

Loggers in Java,Selenium for Beginners

      No Comments on Loggers in Java,Selenium for Beginners


A Logger object is used to log messages for a specific system or application component.
A Logger is useful to track the sequence of actions.
Each Logger has a “Level” associated with it. This reflects a minimum Level that this logger cares about.
The log level can be configured on the Logger.setLevel method.
Refer to the API Link for different levels.
Log Levels

  • SEVERE (highest value)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)
  • OFF
  • ALL


Logic

Conclusion:
Logging using a java.util.logging.Logger class is easy to implement in Java and Selenium automation projects.
OUTPUT:

java.util.logging.Logger

java.util.logging.Logger

Write XLSX using Apache POI-Maven Project

      No Comments on Write XLSX using Apache POI-Maven Project



Apache POI (Poor Obfuscation Implementation File System) is the Java API for Microsoft Documents such as XLS,XLSX,DOC and PPT as well.
HSSF (Horrible Spreadsheet Format)
Read or write an Excel file format – XLS
XSSF (XML Spreadhsheet Format)
Read or write an Excel file format – XLSX
To download the Apache POI Library access the below link -> https://poi.apache.org/download.html
Download the Binary Distribution

  • Linux : poi-bin-xx.tar.gz
  • Windows : poi-bin-xx.zip

Apache POI
Writing the XLSX in JAVA project
1. Create JAVA Project
2. Add the dependencies of the Apache POI to the Build Path of the project.

Apache POI Referenced Libraries

Java Project – Build Path


3. Create a XLSX file with rows and columns
Apache POI-Xlsx File

Xlsx File


4. Write the logic to write the data to the XLSX.


Writing the XLSX in maven project
1. Create MAVEN Project
2. Add the following dependencies in the MAVEN pom.xml
https://mvnrepository.com/artifact/org.apache.poi/poi
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
Maven Dependency- Apache POI

Maven Dependency- Apache POI


3. Write the logic to write the data to the XLSX.

Conclusion:
Reading the XLSX is easy using the Apache POI and Java API.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Output
WriteExcel using Apache POI

WriteExcel using Apache POI

Read XLSX using Apache POI – Maven Project




Apache POI (Poor Obfuscation Implementation File System) is the Java API for Microsoft Documents such as XLS,XLSX,DOC and PPT as well.
HSSF (Horrible Spreadsheet Format)
Read or write an Excel file format – XLS
XSSF (XML Spreadhsheet Format)
Read or write an Excel file format – XLSX
To download the Apache POI Library access the below link -> https://poi.apache.org/download.html
Download the Binary Distribution

  • Linux : poi-bin-xx.tar.gz
  • Windows : poi-bin-xx.zip

Apache POI
Reading the XLSX in JAVA project
1. Create JAVA Project
2. Add the dependencies of the Apache POI to the Build Path of the project.

Apache POI Referenced Libraries

Java Project – Build Path


3. Create a XLSX file with rows and columns
Apache POI-Xlsx File

Xlsx File


4. Write the logic to read the data from XLSX.


Reading the XLSX in maven project
1. Create MAVEN Project
2. Add the following dependencies in the MAVEN pom.xml
https://mvnrepository.com/artifact/org.apache.poi/poi
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
Maven Dependency- Apache POI

Maven Dependency- Apache POI


3. Write the logic to read the data from XLSX.

Conclusion:
Reading the XLSX is easy using the Apache POI and Java API.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Output
Output of the Program

Output of the Program

Finding the Broken HyperLinks

      No Comments on Finding the Broken HyperLinks


HyperLinks in HTML useful to navigate to another web-page.
HyperLinks are defined using the tag name-a using an attribute name href.
We need to define the Link text between the tag name to get the text displayed on the web-page.
Clicking on the link redirected to a new window or in the same window. This can be defined using an attribute target.

  • target=_self -> opens the web-page in the same window
  • target=_blank -> opens the web-page in a new window

Using Locators in Selenium WebDriver, we can automate the steps to click on the link to navigate to a page defined under the attribute href as defined below:
Link_Text

href =”http://total-qa.com” target=”_blank”
Scenario:

Is there any way to identify the broken links using Selenium WebDriver?

HTTP ERROR CODE : 404

HTTP ERROR CODE : 404


The below logic helps in identifying the broken links.
Currently we are using WebDriverManager to instantiate the Driver Instance.

Conclusion:
Broken links in a web-page are identified using Selenium and Java API.

Prevent loading of stylesheets,images,subframes



Web applications accessed using the web browser connects to the server and downloads all the HTML components including images,cascading style sheets – css, frames and iframes as well via HTTP requests. All these content is downloaded from Server via internet via HTTP requests. This results in time for loading the webpages.
IS there any possibility in preventing loading of web application contents??
The answer is Yes, it is possible using Selenium WebDriver tool which helps in preventing loading of HTML components.
They are three ways of loading the Page

  • EAGER
  • NORMAL
  • NONE
Preventing Page Loading

Preventing Page Loading

Page Loading Strategy: eager
Setting the Page Loading Strategy as eager, prevents loading of CSS,images and Subframes.

Refer to Time in milliseconds:

PageLoadingStrategy in Selenium WebDriver

PageLoadingStrategy in Selenium WebDriver


Page Loading Strategy: normal
Setting the Page Loading Strategy as normal loads the CSS,images and Subframes as well. It loads all the components in the webpage.

Refer to Time in milliseconds:
PageLoadingStrategy in Selenium WebDriver

PageLoadingStrategy in Selenium WebDriver


Page Loading Strategy: none
Setting the Page Loading Strategy as none waits until the initial page is loaded.

Refer to Time in milliseconds:
PageLoadingStrategy in Selenium WebDriver

PageLoadingStrategy in Selenium WebDriver

Conclusion:
Preventing the web page from loading the html components is achieved using the Selenium WebDriver API.

Validate tabular data against Excel Data

      No Comments on Validate tabular data against Excel Data


Map is a collection useful to store the things with a unique ID.Classes that implements Map are
HashTable,LinkedHashMap,HashMap,HashTable
.
The HashMap is unsorted and unordered Map and useful to store the null key as well.In this post we will focus on using HashMap for storing the values from the Excel and storing the values in another HashMap by reading the values using the automation tools like Selenium WebDriver. Comparing the two HashMap and display the results.

Real Time Example Scenario
Validate the stock prices displayed in the Rediff.com website
1. Read the data in the XLS file.
2. Store the data in HashMap1.
3. Read the data using Selenium Webdriver.
4. Store the data in HashMap2.
5. Compare the values stored in the two HashMaps.
Store the data in the XLS in rows and columns as mentioned below:

HDFC Bank
1,031.35

SBI
184.45

Create a Maven Project in Eclipse
Add the following dependencies in the pom.xml

Expected Result:
Comparison of HashMap is successful.

@DataProvider being a MySQL DataBase- JAVA,Selenium,Real time examples



In TestNG we use the @DAtaProvider for passing the data values to the testcases. In majority of these cases the Data source used as an two dimensional Array,XLS or XLSX.
In this post we will look into the example where the Data source is MYSQL. For using the MYSQL to read the contents of the DB we have to use the following dependency in the maven pom.xml.
MYSQL Connector Dependency

Preconditions:

  1. Install MYSQL in the laptop. Refer to the

    link

    for more details on installation,configuration and examples on MYSQL.

  2. Open Workbench and make sure the MYSQL server is started.

for more information on installation.

DataProvider Example using DataSource as MYSQL

Refer to the pom.xml details as mentioned below:

OUTPUT

PASSED: login(“james”, “1000”)
PASSED: login(“john”, “2000”)

@Test Attributes TimeOut,expectedExceptions,invocationCount Attributes in Testng



TestNG is a testing framework mainly used for unit testing of the Java projects. The Testng has pre-defined annotations and attributes associated with the annotations would help to verify the application accordingly as per the expected result. The following attributes are applied for @Test attribute

  1. timeOut
  2. expectedExceptions
  3. invocationCount

timeOut Attribute:

The maximum number of milliseconds this test should take. If the execution time of the test case takes more than the timeOut value provided, the test case will Fail.

timeOut Attribute Example


timeOut,expectedExceptions Attribute

timeOut,expectedExceptions Attribute


expectedExceptions Attribute:

The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.

expectedExceptions Attribute Example:


invocationCount Attribute:

The number of times this method should be invoked.

invocationCount Attribute Example

Copy,Xcopy,RoboCopy,RmDir Commands to copy and remove files and folders in Windows



In this tutorial we will discuss about the commands to copy and remove the files and Directories in Windows with different options and examples.

  1. COPY
  2. XCOPY
  3. ROBOCOPY
  4. RMDIR

COPY Command:

copy command in windows copies the files from one directory to another directory. It also helps to combine files where multiple files as a source and single file as a destination.
Syntax:
COPY source destination
COPY source1+source2 destination

Copy a file in the Current Folder:
copy abc.txt new_file.txt

Copy from a different folder to directory:
copy C:\Dir1\abc.txt C:\Dir2\new_file.txt

Copy Multiple Files as a Source and Single file as a destination:
copy file1.txt+file2.txt file3.txt

COPY,XCOPY,ROBOCOPY,RMDIR

COPY,XCOPY,ROBOCOPY,RMDIR

XCOPY Command:

Copies the files and directories into another directory.
Syntax:
XCOPY source destination [options].

They are so many options available. We will see few of them which would be helpful

/S Copy folders and subfolders
xcopy C:\Dir1\Dir2 c:\Dir3 /S

/P Prompt before creating each file.

/C Continue copying if error occurs.

Xcopy Limitation:Insufficient Memory Error

Xcopy fails with an “insufficient memory” error when the path plus filename is longer than 254 characters. The alternative to Xcopy is Robocopy.

ROBOCOPY Command:

Its a Robust File and Folder Copy.
Syntax:
ROBOCOPY source_folder destination_folder [options]
They are so many options available. We will see few of them which would be helpful.
/E: Copy Subfolders, including Empty Folders

Skipped Files:

During the copy of files, some of the files which are empty are Skipped. To overcome this issue use the following Option.
/MIN:n : MINimum file size – exclude files smaller than n bytes.

Example:
ROBOCOPY source_folder destination_folder /E /MIN:0

RMDIR Command

Deletes files and folders.
Syntax:
RD path [options]

/S : Delete all files and subfolders

/Q : Quiet – do not display Y/N confirmation

Example:
Deletes the Folder dir2 and subfolders with-in the dir2 with out prompting the confirmation.
rmdir /S /Q C:\dir1\dir2