Monday, April 20, 2020

TestNG Tutorial Advanced Topics - Part 5

Tutorial Agenda

1> Exclude-Include:- TestNG provides the feature of enabling and disabling the test cases. We can disable a set of test cases from getting executed. 

For example, we have scenario where a serious bug occurs in a feature due to certain tests, so we need to disable the test cases from being executed.

You can disable or exclude the test cases by using the enable attribute to the @Test annotation and assign False value to the enable attribute.

Syntax: @Test(enabled=false)

We can achieve this thing in two way as below:

1> We can add value in the @Test annotation as below:

Example:


Result:


2> We can add Exclude tag into the testng.xml file:

Example:



Result:









😀 
TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW😀 

2> DependsOnMethods: dependsOnMethods attribute on a test method [test1 e.g.] specifies all the test methods [test2, test3,..] this test method depends on. 

It means test1 will start execution only after all the tests it depends on executed successfully. 

If any of the tests specified via dependsOnMethods attribute failed, then test1 will be skipped.

alwaysRun attribute on a test method to true forces the execution of this test even if the tests it depends on were failed.

Example: In below class we have four test method and last two depends on first two method.

1> testAdd(): Everything is fine with code, This will PASS

2> testDivide(): Intentionally we given wrong logic to divide number by 0, hence this will FAIL.

3> testProcessRealNumbers(): This method depends on both above methods to pass.

dependsOnMethods={"testAdd", "testDivide"}

So this will get SKIPPED as second method going to FAIL.

4> testProcessEvenNumbers(): This method again depends on both above methods to pass, also we have added ALWAYS RUN as True means this will pass the test method even one of the method from dependsOn is fail.

Sample Code:

Result as described above:


😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW😀 

3> TestNG Listeners: 

TestNG provides the @Listeners annotation which listens to every event that occurs in a selenium code. 

Listeners are activated either before the test or after the test case. 

It is an interface that modifies the TestNG behavior. For example, when you are running a test case either through selenium or appium and suddenly a test case fails. 

We need a screenshot of the test case that has been failed, to achieve such scenario, TestNG provides a mechanism, i.e., Listeners. When the test case failure occurs, then it is redirected to the new block written for the screenshot.

Listeners are implemented by the ITestListener interface. 

An ITestListener interface has the following methods:



Below is the description of all listeners methods:

onTestStart(): An onTestStart() is invoked only when any test method gets started.
onTestSuccess(): Method is executed on the success of a test method.
onTestFailure(): Method is invoked when test method fails.
onTestSkipped(): Method run only when any test method has been skipped.
onTestFailedButWithinSuccessPercentage():Method is invoked each time when the test method fails but within success percentage.
onStart(): Method is executed on the start of any test method.
onFinish(): Method is invoked when any test case finishes its execution.

Let's Implement this:

First, We need to create one Listener class which contains all methods definition and print line information.

Sample Definition should be as below:


Below is the definition of all method on above class.


Now, Create actual test case which contains TEST method here.
Need to call that listener as shown below:

@Listeners(All_Practicals.Listerner_ITestListener.class)


Once you run the test then it will add some extra line into the log as below.



😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW😀

Reference Site:https://www.javatpoint.com/