TESTNG

TESTNG :- TestNG is an open source automated testing framework; where NG means Next Generation.

Let understand why we need testNG with the help of below diagram



Advantage of TESTNG:-

  • We can generate HTML report using TestNG.
  • We can achieve parallel test case execution.
  • TESTNG support parameterization.
Let start working on TestNG

Note:- 

TestNG requires JDK 7 or higher.
In TestNG we don't have main method to execute the program.
In TestNG we have annotation to work with so lets start working with eclipse.
If we don't use any annotation then test  will not going to be execute.






We will be covering annotation step by step:-

Annotation 01 :- @Test

Test is an inbuit class in TestNG library, it represent that it is our test case.

We can use different features with test annotation like :- alwaysRun , dataProvider, dependsOnGroups, dependsOnMethods etc.

We will be covering these feature in later sessions.

Example :- Lets create tests in our java file and run.


Run TESTNG File and observe the output



As we can see the results, test cases will be execute in alphabetical order.

So we can say by default, methods annotated by @Test are executed alphabetically.

Now lets understand how we can control the test execution or sequencing & priority.

Note we need to use "priority" attribute to control the execution flow.

Let’s take the same above example and execute all @Test methods in right order. Simply assign priority to all @Test methods starting from 0(Zero).


Also remember there is not hard and fast rule that we must follow any kind of sequence while assigning the priority of test case.

Whatever we assign the priority to test cases  TESTNG will execute the test cases in ascending order of priority.


Also understand we have few test cases which are having priority and for few we don't have priority set then first of all test cases which do not have priority will be executed after that only priority test cases will be executed. (But there is an exception)

Please have a look to below example.


Now lets understand what exception is if we have a test cases for which test case priority is set as 0 then first preference goes to priority 'Priority = 0' test cases, then preference goes to test cases which do not have any priority set and then preference goes to test case for which priority is set except 0.




Note:- We use TestNG assertions method for inserting verification points.
For two test case we can have common priority, in that case, test case will be executed alphabetically.(refer above example)

Now let's understand how TESTNG test annotation behavior in case of inheritance.



Now let's see the output.



As we can see that priority goes to local which is an usual case so child class A method executed and all the methods of child class are executed in alphabetical order after this parent class methods are executed in alphabetical order.

let check the same program with priority attribute.


Now lets check the output of this program.




Annotation 02 and 03  :- @BeforeMethod and @AfterMethod

@BeforeMethod: The annotated method will be run before each test cases in a class
@AfterMethod: The annotated method will be run after each test cases in a class
Now lets understand with the below example


As stated earlier we can see the flow of execution as below.


Note:-
Also can we use more than one BeforeMethod and AfterMethod annotations in a single java file although this is not recommendable but again they will be executed based on alphabetical order.

Now let see what will happen if we use inheritance and placed BeforeMethod and AfterMethod annotations.

========================================================================

package Class2;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Parent {
public WebDriver driver;
@BeforeMethod
public void Open() {
System.out.println("opening the browser again oooooo");
driver = new FirefoxDriver();
}
@AfterMethod
public void quit() {
System.out.println("Quit the browser");
driver.quit();
}
@Test
public void show()
{
System.out.println("SHOW METHOD");
}

}

--------------------------------------------------------------------------------------------------------------------------
package Class2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Day02 extends Parent {
@BeforeMethod
public void open() {
System.out.println("opening the browser OOOOOOOO");
driver = new FirefoxDriver();
}
@BeforeMethod
public void z() {
System.out.println("opening the browser z");
}
@Test
public void c() {
System.out.println("execution started in method C");
driver.get("https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1#identifier");
String title = driver.getTitle();
Assert.assertEquals(title, "Gmail");
System.out.println("method C");
}
@Test
public void B() {
System.out.println("execution started in method B");
driver.get("https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1#identifier");
String title = driver.getTitle();
Assert.assertEquals(title, "Gmail");
System.out.println("method B");
}
@AfterMethod
public void y() {
System.out.println("closing the browser y");
driver.quit();
}
@AfterMethod
public void Y() {
System.out.println("Quit the browser Y");
driver.quit();
}
}
--------------------------------------------------------------------------------------------------------------------------

RUN THE DAY2 CLASS
-------------------------------------------------------------------------------------------------------------------------
Output



Note:- So we can say that for test annotation priority goes to local however for before and after test annotation they will run alphabetically. 

Look in above example we also use Assert class which we use for assertion method.



========================================================================


Now let understand how we can run two classes without inheritance in testNG

To run multiple classes we need to use XML file.

Steps to create XML files.

Select Java project and right click
>New > other
>Enter XML and select XML File
>Enter File Name.
>Finish

Lets understand four tags

suite tag

test tag

classes tag :

class tag : we can use multiple class tags.



Now use Before and After Method in each classes and run the XML file again.


Now as stated earlier after and before method will execute before any test present in a class.

Now look at the output


Now let talk about below mention annotations.

@BeforeClass :-  This annotation will run as a pre-condition for all test cases in a class.
@AfterClass :- This annotation will run as a post-condition for all test cases in a class.

@BeforeTest :- This annotation will run as a pre-condition for all test cases in multiple class.
@AfterTest :-This annotation will run as a post-condition for all test cases in multiple class.

@BeforeSuite :- This annotation will run as a pre-condition for all AfterTest.
@AfterSuite :-This annotation will run as a post-condition for all BeforeTest.

Let take the below examples

=================================================
package testNG;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Class1 {
@Test
void A()
{
System.out.println("function of A method in class1");
}

@Test
void a()
{
System.out.println("function of a method in class1");
}
@BeforeMethod
void login()
{
System.out.println("login function method in class1");
}

@AfterMethod
void logout()
{
System.out.println("logout function method in class1");
}
@BeforeClass
void excelOpen()
{
System.out.println("excelOpen function method in class1");
}
@AfterClass
void excelClose()
{
System.out.println("excelClose function method in class1");
}
@BeforeTest
void DBOpen()
{
System.out.println("DBOpen function method in class1");
}
@AfterTest
void DBClose()
{
System.out.println("DBclose function method in class1");
}

}


========================================================================

package testNG;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Class2 {
@Test
void A()
{
System.out.println("function of A method in class2");
}

@Test
void a()
{
System.out.println("function of a method in class2");
}

@BeforeMethod
void login()
{
System.out.println("login function method in class2");
}

@AfterMethod
void logout()
{
System.out.println("logout function method in class2");
}

@BeforeClass
void excelOpen()
{
System.out.println("excelOpen function method in class2");
}
@AfterClass
void excelClose()
{
System.out.println("excelClose function method in class2");
}


}


========================================================================
Output



Now before going on further annotation first lets understand parallel test execution.

Note:-
We can run parallel test execution using TESTNG  (On single machine).
We can run parallel test execution using Selenium Grid (On multiple machine).

Let understand the parallel test execution using TESTNG.


Now using this XML file we can run our file parallel

parallel ="classes" : TestNG  will run all the methods in the same class in the same thread.
parallel ="methods" : TestNG  will run all the methods in the separate threads.
parallel ="tests" : TestNG will run all the methods in the same <test> tag in the same thread.

Now lets understand how we can group the test cases.

Grouping  Test  Cases

TestNG allows us to perform elegant groupings of test methods.

To perform grouping we use groups option with annotations, as below.

@BeforeSuite (groups = {"Sanity", "Regression"})
@Test (groups = {"Regression"})
@Test (groups = {"Regression","Sanity"})

Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

Let see the XML for grouping.



In above XML we have use include and exclude tags in run group which mean to run all the annotations which are belongs to "Sanity" group and exclude all the test which belongs to regression group.
=================================================

Now lets understand the Data Driven Testing.



Post a Comment