Professional Documents
Culture Documents
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
• 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
• @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
➢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
• 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
• 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
• 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
• 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 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.
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