TestNG - Groups, Depends and Parameterization

 

What Are TestNG Groups?

Groups in TestNG means we can club or group multiple tests together and run group wise.
Example we have an E-commerce application in that total 100 test cases are there: 50 are electronics 25 are home and 25 are Fashion related test cases, now I want to run only Electronics related test cases in a batch so here grouping will help us to run as per our grouping.

Example:


Dependent Test In TestNG:

If you want to run your test cases in a particular order in TestNG. We may use the priority parameter for that, no doubt, but priority will run all the cases without looking for the relationship we want to define (alphabetically for the same priority). The dependent tests in TestNG determine the dependency of a test on a single or group of tests. In this case, we say that a test is dependent on another test. 

How to use Depends attribute in TestNG:
  • Using attributes dependsOnMethods in @Test annotations
Example:
public class Dependent {
 
  @Test (dependsOnMethods = { "LaunchBrowser" })
  public void LogIn() {
  System.out.println("This will execute second > LogIn");
  }
 
  @Test
  public void LaunchBrowser() {
  System.out.println("This will execute first > Launch Browser");
  }
}
 
Here,  method LogIn() is dependent upon the method LaunchBrowser().

  • Using attributes dependsOnGroups in @Test annotations.

The dependsOnGroups attribute lets us make a test depend on a whole group rather than a single test. For example, see the code below:

Here, our test method, “ViewAcc(),” depends upon the group “SignIn.”

 

Single Dependent Test Methods In TestNG

A single dependent test in TestNG is declared when a single test depends on another test. It is similar to the example we saw above. We use dependsOnMethods for the same purpose.

 Example:

 

The above code contains three functions:

  • SignIn() – This function depends on the method OpenBrowser (depicted by dependsOnMethod).
  • OpenBrowser() – This method does not depend on any method; hence it is a standard test case.
  • LogOut()-  This method depends on the method SignIn() (depicted by dependsOnMethod).


TestNG Parameters:

Parameters in TestNG is similar to annotations in TestNG in their declaration. Similar to parameters in any other programming language, they are declared to pass some values onto the function. A simple reason to use parameters is that they let us run a function many times with different values or to run different functions with the same values. Parameters pass the values in the runtime. An example of using the parameters in TestNG can be entering different values in an input box. Although you might think that you can change the variable name that is entering the value in the input box, but the test source codes are so long that you would prefer using parameters on any day.

Syntax:

@Parameters ({“a”, “b”})

where a and b are the values that pass to the function.

As we progress into this tutorial, we will see some additional characteristics of parameters in TestNG. For now, let us see how to run TestNG parameters on a simple class.

How To Run TestNG Parameters?

Let’s say we want to add two numbers using the TestNG parameters. check below code for an example:

Can we run it directly using Rus As -> TestNG Test: No it won't.

Note: TestNG Parameters are run through the TestNG XML file and not from the test case files directly.

Write the following XML code in it.

In the above XML file, we have defined a tag called parameters which work as follows:

  • name: Name of the variable that you declared in the test case file like val1 and val2 in the above example.
  • value: The value of the variable you want to insert.

Run the XML file by Rus As -> TestNG Suite.


DataProviders in TestNG?

Data providers in TestNG means we can pass different parameters to the test case in the form of TestNG annotations and it is a part of TestNG data driven testing.

DataProvider Syntax:

@DataProvider (name = “dataprovider_name”)

public Object[][] dpMethod() {

return new Object [][] { values}

}

After the introduction of this syntax, there are a few things that you should understand:

  • The TestNG DataProvider (the annotation part) contains only one single attribute, which is its name. It is always a string type in nature. For example, “dataprovider_name,” as mentioned above.
  • DataProviders are not declared on top of the functions like TestNG parameters but have a method of their own, which in regular speaking terms called a dataprovider method. For example, dpMethod here.
  • If  not specified the name of the dataprovider, then the method name becomes the dataprovider name by default.
  • The method then performs a data-driven test for each value that you have specified.
  • The dataprovider name calls the dataprovider method, and if there is no name specified by the tester, then the dataprovider method is the default name used in the receiving @Test case.

Usage of DataProvider In TestNG:

If you have understood the above-said points, using dataproviders is very easy. We will start with a straightforward and basic DataProvider test first. Observe the following code, which contains the @DataProvider.

Note: You need to import the DataProvider in TestNG by adding the line import org.testng.annotations.DataProvider;

 Run the code with Run As -> TestNG Test and see the output.



 



No comments:

Post a Comment

Scroll iOS application using Appium

When creating test scripts for iOS native app we need to perform scroll to perform any other action on particular element  Here we will get ...