Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 60: Dependency in TestNG – Creating Dependency Among Test Methods – DependsOnMethod

Posted on 02/19/2025 By admin

We are going to see another important concept or feature provided by TestNG. This feature is called as Dependency.

Suppose, we need to post a status on Facebook. Let’s write a Test Case for it:

Step 1: Launch browser.

Step 2: Launch Facebook url.

Step 3: Create an account on Facebook.

Step 4: Post a status.

In above test case, Step 4 is what matters. But to get Step 4 to execute, we have three pre steps which need to be executed first in order. Step 1 is not an actual part of functionality testing. It provides a platform to test functionality. So we can keep Step 1 in @Before annotated steps. But Step 2, Step 3 and Step 4 are part of Test. We should keep them under @Test annotation. So Step 2 and Step 3 must be executed first before Step 4. We can achieve this using “priority” concept of TestNG.

There is another way of achieving this as well which is called Dependency. TestNG provides an attribute named “dependsOnMethods”. Using this attribute we can mention a method name or a list of methods with @Test annotation, on which your test method is dependent.

In above test case, Step 4 is dependent on Step 2 and Step 3. Step 2 should execute first followed by Step 3 and then Step 4. If we mention all dependent method names in Step 4, TestNG cannot guarantee of execution of Step 2 and Step 3 in order. So we will use multilevel dependency here.

An example is below:

package Dependecy;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MultiLevelDependency {
        
        @BeforeTest
        public void launchBrowser()
        {
                System.out.println("browser Launched.");
        }
        
        
        @Test
        public void loadFacebookURL()
        {
                System.out.println("Facebook URL loaded.");
        }
        
        /*
         * registerOnFacebook depends  on loadFacebookURL
         */
        @Test(dependsOnMethods = {"loadFacebookURL"})
        public void registerOnFacebook()
        {
                System.out.println("Register on Facebook.");
        }
        
        /*
         * postStatusOnFacebook depends on registerOnFacebook
         */
        @Test(dependsOnMethods = {"registerOnFacebook"})
        public void postStatusOnFacebook()
        {
                System.out.println("Post an status on Facebook.");
        }
        
        
}

Output:

[java] [RemoteTestNG] detected TestNG version 6.14.2 browser Launched. Facebook URL loaded. Register on Facebook. Post an status on Facebook. PASSED: loadFacebookURL PASSED: registerOnFacebook

PASSED: postStatusOnFacebook

Uncategorized

Post navigation

Previous Post: Data Structure For Testers
Next Post: Now You Can Minimize Browser in Selenium WebDriver

Related Posts

Postman Tutorial Part 2 – Installation of Postman Tool – Chrome App, Native App and Web app Uncategorized
Postman Tutorial Part 22 – Postman Console – Debugging And Logging Uncategorized
TestNG Tutorials – Page 2 Uncategorized
September 4, 2017 – Make Selenium Easy Uncategorized
Postman Tutorial Part 51 -Printing Request Body & Response Body in Postman Console Uncategorized
REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark