You are on page 1of 42

TestNG

TESTNG

• TestNG is a testing framework inspired from JUnit and NUnit, but introducing
some new functionalities that make it more powerful and easier to use.
TESTNG

• TestNG is one of the most widely used open source testing framework used in
automation testing suite.
• TestNG is a testing framework developed in the lines of JUnit and NUnit, however
it introduces some new functionalities that make it more powerful and easier to
use.
• TestNG is designed to cover all categories of tests: unit, functional, end-to-end,
integration, etc., and it requires JDK 5 or higher.
TESTNG

• TestNG is an open source automated testing framework;


where NG means NextGeneration.
• TestNG is similar to JUnit (especially JUnit 4), but it is not a JUnit extension.
• It is inspired by JUnit. It is designed to be better than JUnit, especially when
testing integrated classes.
• The creator of TestNG is Cedric Beust.
TESTNG

• Eliminating most of the limitations of the older framework, TestNG gives the
developer the ability to write more flexible and powerful tests. As it heavily
borrows from Java Annotations (introduced with JDK 5.0) to define tests, it can
also show you how to use this new feature of the Java language in a real
production environment.
WHY USE TESTNG WITH SELENIUM?

• Default Selenium tests do not generate a proper format for the test results.
• Using TestNG we can generate test results. Most Selenium users use this more than Junit
because of its advantages.
• There are so many features of TestNG, but we will only focus on the most important
ones that we can use in Selenium.
FOLLOWING ARE KEY FEATURES OF TESTNG

• Generate the report in a proper format including a number of test cases runs, the number of
test cases passed, the number of test cases failed, and the number of test cases skipped.
• Multiple test cases can be grouped more easily by converting them into testng.xml file. In
which you can make priorities which test case should be executed first.

• The same test case can be executed multiple times without loops just by using keyword called
'invocation count.’

• Using testng, you can execute multiple test cases on multiple browsers, i.e., cross browser
testing.
WHY USE TESTNG WITH SELENIUM?

• The testing framework can be easily integrated with tools like Maven, Jenkins, etc.
• Annotations used in the testing are very easy to understand ex: @BeforeMethod,
@AfterMethod, @BeforeTest, @AfterTest
• WebDriver has no native mechanism for generating reports.TestNG can generate the
report in a readable format .
WHY USE TESTNG WITH SELENIUM?

• Uncaught exceptions are automatically handled by TestNG without terminating the test
prematurely.These exceptions are reported as failed steps in the report.
• TestNG simplifies the way the tests are coded.There is no more need for a static main
method in our tests. The sequence of actions is regulated by easy-to understand
annotations that do not require methods to be static.
ANNOTATIONS AVAILABLE IN TESTNG

• @Test: Marks a class or a method as a part of the test.


• @BeforeMethod:A method which is marked with this annotation will be executed
before every @test annotated method.
• @AfterMethod:A method which is marked with this annotation will be executed after
every @test annotated method.
ANNOTATIONS AVAILABLE IN TESTNG

• @BeforeClass:A method which is marked with this annotation will be executed before
first @Test method execution. It runs only once per class.
• @AfterClass:A method which is marked with this annotation will be executed after all
the test methods in the current class have been run.
• @BeforeTest: A method which is marked with this annotation will be executed before
first @Test annotated method.
ANNOTATIONS AVAILABLE IN TESTNG

• @AfterTest: A method which is marked with this annotation will be executed when all
@Test annotated methods complete the execution of those classes which are inside tag
in testng.xml file.
• @BeforeSuite:A method which is marked with this annotation will run only once before
all tests in the suite have run
• @AfterSuite:A method which is marked with this annotation will run once after
execution of all tests in the suite have run
ANNOTATIONS AVAILABLE IN TESTNG

• @BeforeGroups:This annotated method will run before the first test run of that specific
group.
• @AfterGroups:This annotated method will run after all test methods of that group
completes its execution.
GENERATING HTML REPORTS

• TestNG has the ability to generate reports in HTML format.


• Step 1: After running our FirstTestNGFile that we created in the previous section, right-click
the project name (FirstTestNGProject) in the Project Explorer window then click on the
"Refresh" option.
• Step 2: Notice that a "test-output" folder was created. Expand it and look for an index.html
file. This HTML file is a report of the results of the most recent test run.
• Step 3: Double-click on that index.html file to open it within Eclipse's built-in web browser.
You can refresh this page any time after you rerun your test by simply pressing F5 just like in
ordinary web browsers.
GENERATING HTML REPORTS

• Multiple Test Cases


• We can use multiple @Test annotations in a single TestNG file. By default, methods
annotated by @Test are executed alphabetically.
• Run this code and on the generated index.html page, click "Chronological view."
@TEST PARAMETERS

Parameters are keywords that modify the annotation's function.


• priority: If you want the methods to be executed in a different order.
@Test(priority = 0)
• Enabled:To execute the test or not.
@Test(enabled=false) not executed.
• dependsOnMethods:To execute the test mention in the dependsOnMethods first.
@Test(dependsOnMethods=“Register”)
TESTNG- PARAMETRIC TESTING

➢Parameterized tests allow developers to run the same test over and over again
using different values.
➢TestNG lets you pass parameters directly to your test methods in two different
ways −
• With testng.xml
• With Data Providers
PASSING PARAMETERS WITH TESTNG.XML

• With this technique, you define the simple parameters in the testng.xml file and
then reference those parameters in the source files.
PASSING PARAMETERS WITH TESTNG.XML

• TestNG will automatically try to convert the value specified in testng.xml to the type of your parameter. Here are the types
supported −
• String

• int/Integer

• boolean/Boolean

• byte/Byte
• char/Character

• double/Double

• float/Float

• long/Long

• short/Short
DATA PROVIDERS

• The Data Providers in TestNG are another way to pass the parameters in the
test function.
• Data Providers pass different values to the TestNG Test Case in a single
execution and in the form of TestNG Annotations.
• It is a part of the inbuilt TestNG data-driven testing for which TestNG is quite
popular.
• Data Providers help in passing the parameters in different ways.
DATAPROVIDER SYNTAX:

@DataProvider (name="name_of_dataprovider")
public Object[][] dpMethod()
{
return new Object [][] { values}
}
ASSERTIONS

• Assertions in TestNG are a way to verify that the expected result and the
actual result matched or not.
• An example of assertion can be logging into the website, checking the title of
the webpage, verifying the functionality of an input box that takes only
integers, etc.
ASSERTIONS

Syntax for TestNG Assertions:


• Assert. Method(actual,expected);
• Actual: The actual value that the tester gets like if the tester's assertion is
on the title of the page then what was the actual title of the page goes
here.
• Expected: The value that you expect like if the tester's assertion is on the
title of the page then what value of title do you expect goes here.
DIFFERENT TYPES OF ASSERTS IN TESTNG

• There are two types of TestNg Assert:


• Hard Assert
• Soft Assert
HARD ASSERTS

• Hard Asserts are those asserts that stop the test execution when an assert
statement fails, and the subsequent assert statements are therefore not
validated. It plays a vital role in projects where we have an element without
whose validation, asserting other elements is useless.
• Hard asserts are the default type of asserts in TestNG.
SOFT ASSERTS

• Soft asserts are just the opposite of hard asserts. In soft asserts, the
subsequent assertions keep on running even though one assert validation
fails, i.e., the test execution does not stop.
• Soft assert does not include by default in TestNG.
• For this, you need to include the package org.testng.asserts.Softassert.
• We use soft asserts when we do not care about the failure of specific
validations and want the test execution to proceed and also want to see the
exception errors.
CROSS BROWSER TESTING

• Cross-browser testing is the process of testing our website on different browsers


and operating systems.
• With cross-browser testing, we make sure that the site is rendered the same in
every browser.

• We can perform cross-browser testing either manually or in an automated way,


but the manual method is very tedious.

• The reason being that while performing cross-browser testing, we do not only
care about the browsers but their different versions and the operating systems
too.
PARALLEL TESTING

• Parallel testing or parallel execution, as the name suggests, is a process of


running the test case parallelly rather than one after the other.
• In parallel testing, the program's multiple parts (or modules) execute together,
saving the testers a lot of time and effort.
• The operating system's functionalities do this, but as a user, we need to trigger
parallel execution through TestNG.
• As an example, you can think of having software with two different versions and
running them in parallel with the help of TestNG. Parallel execution would give us the
correct idea of the stability and performance of the software much faster than running
serially.
ADVANTAGES OF PARALLEL TESTING

• Reduces Time: Running the tests in parallel reduces the overall execution time.
• Allow Multi-Threaded Tests: Using the parallel execution in TestNG, we can allow
multiple threads to run simultaneously on the test case providing independence in
the execution of different components of the software.
PAGE OBJECT MODEL

• Page Object is a Design Pattern which has become popular in test automation for enhancing
test maintenance and reducing code duplication.
• A page object is an object-oriented class that serves as an interface to a page of your AUT.
• The tests then use the methods of this page object class whenever they need to interact with
the UI of that page.

• The benefit is that if the UI changes for the page, the tests themselves don’t need to change,
only the code within the page object needs to change. Subsequently all changes to support
that new UI are located in one place.
PAGE OBJECT MODEL

• It is a design pattern to create Object Repository for web UI elements.


• Under this model, for each webpage in the application, there should be a corresponding
page class.
• This page class will find the WebElements of that web page and contains Page methods
which perform operations on that WebElements.
OBJECT REPOSITORY

• Object Repository is a collection of object and properties with which QTP(Quick Test
Professional) will be able to recognise the objects and act on it.
PAGE OBJECT MODEL
ADVANTAGES
• Code Resuability: We could achieve code reusability by writing the code once and use it
in different tests.
• Code Maintanability:There is a clean specification between test code and page specific
code such as locators and layout which becomes very easy to maintain code. Code
changes only on Page Object Class when a UI change occurs. It enhances test
maintanence and reduces code duplication.
• Object Repository: Each page will be defined as a Java class. All fields in the page will be
defined in an interface as members.The class will implement the interface.
PAGE OBJECT MODEL
ADVANTAGES
• Readability: Improves readability due to clean separation between test code and page
specific code.
PAGE OBJECT MODEL

Disadvantages
• There is no separation between the test method and the AUT’s locators (IDs in this
example); both are intertwined in a single method. If the AUT’s UI changes its identifiers,
layout, or how a login is input and processed, the test itself must change.
• The ID-locators would be spread in multiple tests, in all tests that had to use this login
page.
MAVEN

• Maven is a project management and comprehension tool that provides


developers a complete build lifecycle framework. Development team can
automate the project's build infrastructure in almost no time as Maven uses a
standard directory layout and a default build lifecycle.
• In case of multiple development teams environment, Maven can set-up the way to
work as per standards in a very short time. As most of the project setups are
simple and reusable, Maven makes life of developer easy while creating reports,
checks, build and testing automation setups.
MAVEN

• Maven provides developers ways to manage the following −


• Builds
• Documentation
• Reporting
• Dependencies
• SCMs
• Releases
• Distribution
• Mailing list
MAVEN

Maven projects are configured using a Project Object Model (POM), which is stored in a pom.xml-file
<project>
<!-- model version is always 4.0.0 for Maven 2.x POMs -->
<modelVersion>4.0.0</modelVersion>
<!-- project coordinates, i.e. a group of values which uniquely identify this project -->
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0</version>
<!-- library dependencies -->
<dependencies>
<dependency>
<!-- coordinates of the required library -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!-- this dependency is only used for running and compiling tests -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
MAVEN

• This POM only defines a unique identifier for the project (coordinates) and its
dependency on the JUnit framework. However, that is already enough for building
the project and running the unit tests associated with the project. Maven
accomplishes this by embracing the idea of Convention over Configuration, that
is, Maven provides default values for the project's configuration.
THE DIRECTORY STRUCTURE OF A
NORMAL IDIOMATIC MAVEN PROJECT HAS THE
FOLLOWING DIRECTORY ENTRIES:
Directory name Purpose
project home Contains the pom.xml and all subdirectories.

Contains the deliverable Java sourcecode for the


src/main/java
project.

Contains the deliverable resources for the project,


src/main/resources
such as property files.

Contains the testing Java sourcecode (JUnit or


src/test/java
TestNG test cases, for example) for the project.

src/test/resources Contains resources necessary for testing.


MAVEN

The command mvn package will compile all the Java files, run any tests, and
package the deliverable code and resources into target/my-app-1.0.jar

You might also like