Introduction to Automation testing:
Testing is an essential part of a
software development process. While testing intermediate versions of
products/projects being developed, testing team needs to execute a number of
test cases. In addition, prior to release every new version, it is mandatory
that the version is passed through a set of “regression” and “smoke” tests.
Most of all such tests are standard for every new version of product/project,
and therefore can be automated in order to save human resources and time for
executing them.
Benefits of using automated testing
are the following:
- Reduction of tests’ time execution and human resources
required
- Complete control over the tests’ results (“actual
results” vs “expected results”)
- Possibility to quickly change test’s preconditions and
input data, and re-run the tests dynamically with multiple sets of data
Automation workflow for the
application can be presented as follows:
- First of all it is required to identify tasks that an
application has to accomplish.
- Second, a set of necessary input data has to be
created.
- Third, expected results have to be defined in order one
can judge that an application (a requested feature) works correspondingly.
- Fourth, Executes a test.
- Finally, Compares expected results with actual results,
and decides whether the test has been passed successfully.
Environment Specifications:
- Selenium Webdriver (Supports all major browsers, we use
Mozilla, chrome and IE)
- Eclipse IDE
- Java
- TestNG
- AutoIT Tool (Used to handle Windows popups for Document
Uploads
and Downloads.)
- JExcel or Apache
POI to perform operations with
excel like read, write and update the excel sheet
This Framework has the following
tools:
1. Selenium - Selenium is a well know open source testing framework, which
is widely used for testing Web-based applications. It has different components
and in that Webdriver has rendered the Selenium Remote Control obsolete, and is
commonly referred to as Selenium 2.0.
Selenium Webdriver supports
most of all browsers to run your test cases and many programming languages like
C#, Java, Python, Ruby, .Net, Perl, PHP, etc.. to create and modify your test
scripts.
2. Eclipse IDE: Eclipse is an integrated development environment (IDE) for
Java. The Eclipse IDE is the most known product of the Eclipse Open Source
project.
3. TestNG - Is a testing framework inspired from JUnit and NUnit. It
has extended new functionalities which made it more powerful and easier than
the other testing frameworks.
It supports ReportNG (simple HTML reporting plug-in) and XLST (Graphical / Pictorial reports) plug-ins to
customize or extend the default TestNG reporting style.
TestNG also provides ability to
implement 'IReporter' an interface which can be implemented to generate a Customized TestNG report
by users. It has 'generateReport()' method which will be invoked after all the
suite has completed its execution and gives the report into the specified
output directory.
4. AutoIT - AutoIt v3 is a freeware BASIC-like scripting language
designed for automating the Windows GUI and general scripting. It uses a
combination of simulated keystrokes, mouse movement and window/control
manipulation in order to automate tasks which is not possible with selenium.
You can also refer the other
frameworks available here Keyword Driven Framework
and Page Object Model Framework
File Formats Used in the Framework:
- Properties file
– We use properties file to store and retrieve the UI elements of an
application or a website and data set file paths. It contains id of the
elements, name, xpath or Css selector etc.
- Excel files
– Excel files are used to pass multiple sets of data to the application.
- Xml file
– Is used to execute the test scripts. Based on the package or classes or
Tests mentioned in the xml file scripts will be executed.

The Project Folder Structure: All the basic required folders are created with the sub
folders and classes under each folder:--

The Following explains the structure
in detail:-
UIMap is a concept for defining,
storing, and serving UI elements of an application or a website. The UIMap
properties file contains a set of ‘key-value’ pairs, where key is an alias of
the UI element, and a value is the locator. Click here for more..
Data set stores the data files,
Script reads test data from external data sources and executes test based on
it. Data sets increases test coverage by performing testing with various
inputs and reduce the number of overall test scripts needed to implement all
the test cases. Click here for more..
A test is considered as a single
action or a sequence of actions, that defines whether a specific feature meets
functional requirements. It has multiple test files / packages / class
files which will be executed based on the configurations defined in testng.xml.
Click here for more..
Test report/results is a document
which contains summary of test activities. After execution is completed,
it is very important to communicate the test results and findings to the
project manager along with the screenshots for failed tests and with that decisions can be made for the release. Click here for More..
In
order to create a test suite and run separate test cases, we need framework
which drives the automation. Here testng.xml can be called as
"driver" which drives several test cases automated using selenium
code. Advantage of using TestNG with Selenium is of running multiple test
cases from multiple classes using xml configuration file
In order to create a test suite and
run separate test cases, we need framework which drives the automation. Here
testng.xml can be called as "driver" which drives several test cases
automated using selenium code.
Advantage of using TestNG with
Selenium is of running multiple test cases from multiple classes using xml
configuration file
Another advantage of testng.xml is
for entire teams that need to share their test configuration or to be able to
specify different parameters or subsets of their tests in different testng.xml
files
testng.xml file will have the
following structure:-
The
root tag of this file is <suite>. as tag <html> which is root tag
in html.
<suite>
tag can contain one or more <test> tags.
<test>
tag can contain one or more <classes> tags.
<classes>
tag can contain one or more <method> tags
Here is an example testng.xml file:
<suite name="ApplicationName Suite" verbose="1" >
<test name="testpackage" >
<classes>
<class name="testpackage.Test" />
</classes>
</test>
<test name="Regression Testing">
<classes>
<class name="testpackage.app.classSample"/>
<class name="testpackage.app.locationTest"/>
</classes>
</test>
</suite>
We can specify package names instead
of class names in testng.xml file:
<suite
name=" ApplicationName Suite" verbose="1" >
<test name="Regression1" >
<packages>
<package name="testpackage" />
</packages>
</test>
</suite>
In this example, TestNG will look at
all the classes in the package “testpackage” and will retain only classes that
have TestNG annotations.
We can also specify group tags and
method tags to be included and excluded:
<suite
name=" ApplicationName Suite" verbose="1" >
<test
name="Regression1">
<groups>
<run>
<exclude name="brokenTests" />
<include name="checkinTests" />
</run>
</groups>
<classes>
<class name="test.IndividualMethodsTest">
<methods>
<include name="testMethod" />
</methods>
</class>
</classes>
</test>
</suite>
You can also define new groups
inside testng.xml and specify additional details in attributes, such as whether
to run the tests in parallel, how many threads to use, whether you are running
JUnit tests, etc...
By
default, TestNG will run your tests in the order they are found in the XML
file. If you want the classes and methods listed in this file to be run in an
unpredictable order, set the preserve-order attribute to false
The below example program explains
how to write / set data in spreadsheet without any formatting such as fonts
etc.
In order to write anything we need
to first create a writable workbook as below which creates the workbook object.
WritableWorkbook
workbook = Workbook.createWorkbook(new File("sampletestfile.xls"));
We can also use the existing excel
sheet and write the data into excel sheet. But before doing write operations we
need to make sure to take the copy of the original file
and then perform the write
operations. The below code is the sample code.
Workbook
workbook = Workbook.getWorkbook(new File("testSampleData.xls"));
WritableWorkbook
workbookCopy= Workbook.createWorkbook(new
File("testSampleDataCopy.xls"), workbook);
Now access the sheet from the
workbook which is copied from the original.
WritableSheet
wSheet = workbookCopy.getSheet(0);
As now we are ready with the
worksheet to which we need to pass the data. We need to mention the location
(Column number and Row number) to write the data.
The below is the sample code to
write the data into excel sheet.
WritableSheet
wshTemp = wwbCopy.getSheet(strSheetName);
Label
label= new Label(iColumnNumber, iRowNumber, strData);
wshTemp.addCell(label);
Please find the below working example
program:
import
java.io.File;
import
jxl.Workbook;
import
jxl.write.WritableSheet;
import
jxl.write.WritableWorkbook;
import
jxl.write.Label;
import
jxl.write.WriteException;
public
class dataSheet {
static Workbook wbook;
static WritableWorkbook wwbCopy;
static String ExecutedTestCasesSheet;
static WritableSheet shSheet;
public void readExcel()
{
try{
wbook = Workbook.getWorkbook(new File("path\\testSampleData.xls"));
wwbCopy = Workbook.createWorkbook(new
File("path\\testSampleDataCopy.xls"), wbook);
shSheet = wwbCopy.getSheet(0);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setValueIntoCell(String strSheetName,int iColumnNumber, int
iRowNumber,String strData) throws WriteException
{
WritableSheet wshTemp = wwbCopy.getSheet(strSheetName);
Label labTemp = new Label(iColumnNumber, iRowNumber, strData);
try {
wshTemp.addCell(labTemp);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void closeFile()
{
try {
// Closing the writable work book
wwbCopy.write();
wwbCopy.close();
// Closing the original work book
wbook.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws WriteException
{
dataSheet ds = new dataSheet();
ds.readExcel();
ds.setValueIntoCell("sheet1", 5, 1, "PASS");
ds.setValueIntoCell("sheet1", 5, 2, "FAIL");
ds.setValueIntoCell("sheet1", 5, 3, "PASS");
ds.closeFile();
}
}
·
We can pass the formatting
information to Excel by overloading the constructor which takes an additional
object containing the cell format information.
·
Formatting is required when ever you
want to distinguish cell content with other cell contents. Generally, when we
update the test cases in excel sheet, we keep all "PASS" in green
color and Failed in Red color just to highlight the content.
·
The below syntax and example program
helps to write data into excel sheet using formatting styles
·
// Create a cell format for
specified font
·
WritableFont cellFont= new
WritableFont(WritableFont.TIMES, 12);
·
WritableCellFormat cellFormat = new
WritableCellFormat(cellFont);
·
and In order to define the font
color, we use the below statement.
·
cellFont.setColour(Colour.RED);
·
// Create the label, specifying
content and format
·
Label lab1 = new
Label(iColumnNumber, iRowNumber, strData, cellFormat);
·
sheetTemp.addCell(lab);
·
We can pass the same format to
different cells. We need to share the same object that is created in the above.
·
Label lab2 = new
Label(iColumnNumber, iRowNumber, strData, cellFormat);
·
sheetTemp.addCell(lab);
·
Please find the below example
program:
·
import java.io.File;
·
import jxl.Workbook;
·
import jxl.write.WritableSheet;
·
import jxl.write.WritableWorkbook;
·
import jxl.format.Border;
·
import jxl.format.BorderLineStyle;
·
import jxl.format.Colour;
·
import jxl.write.Label;
·
import jxl.write.WritableCellFormat;
·
import jxl.write.WritableFont;
·
import jxl.write.WriteException;
·
·
public class dataSheet {
·
·
static
Workbook wbook;
·
static
WritableWorkbook wwbCopy;
·
static
String ExecutedTestCasesSheet;
·
static
WritableSheet shSheet;
·
·
public
void readExcel()
·
{
·
try{
·
wbook
= Workbook.getWorkbook(new File("D:\\Dev\\SampleTest\\datasheets\\testSampleData.xls"));
·
wwbCopy
= Workbook.createWorkbook(new
File("D:\\Dev\\SampleTest\\datasheets\\testSampleDataCopy.xls"),
wbook);
·
shSheet
= wwbCopy.getSheet(0);
·
}
·
catch(Exception
e)
·
{
·
e.printStackTrace();
·
}
·
·
}
·
·
public
void setValueIntoCell(String strSheetName,int iColumnNumber, int
iRowNumber,String strData) throws WriteException
·
{
·
WritableSheet
wshTemp = wwbCopy.getSheet(strSheetName);
·
WritableFont
cellFont = null;
·
WritableCellFormat
cellFormat = null;
·
·
if(strData.equalsIgnoreCase("PASS"))
·
{
·
cellFont
= new WritableFont(WritableFont.TIMES, 12);
·
cellFont.setColour(Colour.GREEN);
·
cellFont.setBoldStyle(WritableFont.BOLD);
·
·
cellFormat
= new WritableCellFormat(cellFont);
·
cellFormat.setBorder(Border.ALL,
BorderLineStyle.THIN);
·
}
·
·
else
if(strData.equalsIgnoreCase("FAIL"))
·
{
·
cellFont
= new WritableFont(WritableFont.TIMES, 12);
·
cellFont.setColour(Colour.RED);
·
cellFont.setBoldStyle(WritableFont.BOLD);
·
·
cellFormat = new
WritableCellFormat(cellFont);
·
cellFormat.setBorder(Border.ALL,
BorderLineStyle.THIN);
·
}
·
·
else
·
{
·
cellFont
= new WritableFont(WritableFont.TIMES, 12);
·
cellFont.setColour(Colour.BLACK);
·
·
cellFormat
= new WritableCellFormat(cellFont);
·
cellFormat.setBorder(Border.ALL,
BorderLineStyle.THIN);
·
cellFormat.setWrap(true);
·
}
·
·
Label
labTemp = new Label(iColumnNumber, iRowNumber, strData, cellFormat);
·
try
{
·
wshTemp.addCell(labTemp);
·
·
}
·
catch
(Exception e)
·
{
·
e.printStackTrace();
·
}
·
}
·
·
public
void closeFile()
·
{
·
try
{
·
//
Closing the writable work book
·
wwbCopy.write();
·
wwbCopy.close();
·
·
//
Closing the original work book
·
wbook.close();
·
}
catch (Exception e)
·
{
·
e.printStackTrace();
·
·
}
·
}
·
·
public
void main(String[] args) throws WriteException
·
{
·
dataSheet
ds = new dataSheet();
·
ds.readExcel();
·
ds.setValueIntoCell("Sheet1",
5, 1, "PASS");
·
ds.setValueIntoCell("Sheet1",
5, 2, "FAIL");
·
ds.setValueIntoCell("Sheet1",
5, 3, "N/A");
·
·
}
·
}
·
The Above program will copy the
original sheet. Now in the sheet which is copied will be updated with 'PASS',
'FAIL' and 'N/A' with styles defined.
There are different ways to handle
file uploads with Selenium Webdriver.
The First and the Easy way is simple
case of just finding the element and typing the absolute path of the document
into it.
HTML code should look similar to
this :
<input
type="file" name="datafile">
Syntax:
//Find
the element of upload button and send the path
WebElement
element= driver.findElement(By.name("datafile"));
element.sendKeys("C:\Users\Easy\Desktop\testfile.txt");
NOTE: This works only when the textbox is enabled. So please make
sure that the input element is visible.
The OTHER WAY to Handle File Upload
What If there is no text box / input
tag to send the file path using Sendkeys method????
You will have only a button
(Customized button) and when click on browse button, browser opens a window
popup and to select the file from windows, And as we know selenium will not
support OS controls.
We can use AutoIt tool (open source
tool) which takes the responsibility to upload the file.
About AutoIT : AutoIt v3 is a
freeware BASIC-like scripting language designed for automating the Windows GUI
and general scripting.
We need to call the AutoIt script
after clicking on the upload button. Immediatly ater clicking on Upload button,
the control should be transfered to AutoIt which takes care of uploading the
file.
Syntax:
Runtime.getRuntime().exec("AutoIt
.exe filepath");
Example:
Runtime.getRuntime().exec("D:/fileupload.exe");
AutoIt script should have the
following:
Step 1 : First After clicking on 'Upload' button, the cursor will
move to the Window popup.
The below step should be the first
line in the script. 'File Upload' is the name of the window popup when opened
with Mozilla. The name changes depending on the browser.
Mozilla - File Upload
Chrome - Open
and for IE - Choose File to Upload
Chrome - Open
and for IE - Choose File to Upload
WinWaitActive("File
Upload")
Step 2 : Once the window popup is active, we should send the path
of the document which need to be uploaded.
Send("Full path of the document")
Step 3 : After that we need to click on Open button which will
upload the document.
Send("{ENTER}")
Comments
Post a Comment