• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / TestNG Tutorial / How to create dependency between testcases in Selenium

How to create dependency between testcases in Selenium

May 31, 2015 by Mukesh Otwani 8 Comments

Skip Test

I am sure you must be confused with Title like How to create a dependency between test cases in Selenium but in this post we will see this through example.

In real time, you will come across many situation where you have to create test cases which are dependent on the previous test case.

Take an example I have 3 test case.

First one which verify login credential and start application
Second test case check some validation  or do some activity
Third test case simply just for log-out activity.

If my first test case does not work (failed) that it does not make any sense to run second and third test case so in these type of scenarios you can use dependency between test case.

How can I do this?

TestNG already comes with this features so you can use the same with @Test annotations

Syntax- @Test(dependsOnMethods={"loginTestCase"})

public void testAccount()
{

// sample code

}

How to create dependency between test cases in Selenium

 

It means this test case name testAccount depends on test case whose name is loginTestCase so until loginTestCase will not execute successfully this test case won’t execute so if loginTestCase will pass this test case will execute and if loginTestCase will fail then this test case won’t execute and runner will skip this test cases and this test case will come under skip test cases.

Let’s implement the same

package Demo;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMultiple {

@Test
public void testLogin()
{

System.out.println("login done");

}

@Test(dependsOnMethods={"testLogin"})
public void testAccount()
{
System.out.println("Account has been created");

}

@Test(dependsOnMethods={"testLogin","testAccount"})
public void testLogout()
{
System.out.println("logout");

}

}

 

In the above scenario if testLogin will pass then only testAccount will execute and testLogout will only execute of testLogin and testAccount will  be executed

but if testLogin will fail due to some error or exception the testAccount will not be executed and this test case will skip and testLogout is dependent on testLogin and testAccount so now this will also come into skipped category

Let’s implement again

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestApplication {

@Test
public void testLogin()

{
Assert.assertEquals("Selenium", "Selnium");
System.out.println("login done");

}

@Test(dependsOnMethods={"testLogin"})
public void testAccount()

{
System.out.println("Account has been created");

}

@Test(dependsOnMethods={"testLogin","testAccount"})

public void testLogout()

{
System.out.println("logout");

}
}

 

Output

FAILED: testLogin
java.lang.AssertionError: expected [Selnium] but found [Selenium]
at org.testng.Assert.fail(Assert.java:94)
SKIPPED: testAccount
SKIPPED: testLogout

===============================================
Default test
Tests run: 3, Failures: 1, Skips: 2
===============================================

===============================================
Default suite
Total tests run: 3, Failures: 1, Skips: 2
===============================================

OUTPUT in HTML

Skip Test

 

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

Comment below for any clarification

 

 

 

Filed Under: TestNG Tutorial Tagged With: TestNG Depends

Reader Interactions

Comments

  1. Malavika says

    April 29, 2019 at 4:59 PM

    Hi Mukesh

    I tried this and i got error in @AfterTest part. Logout is failing.
    I checked my Xpath. its correct. But NullPointerException is thrown. Can you please guide?

    Reply
    • Mukesh Otwani says

      April 29, 2019 at 9:43 PM

      Hi Malavika,

      NullPointerException is most common runtime exception which usually comes when you refer any object which is not initialized or just declared only so wrong locator never throw this exception. Kindly check/debug your code once again.

      Reply
      • Malavika says

        April 29, 2019 at 10:06 PM

        HI,
        Thanks for the response. I tried again , now its showing a different error. It says the element is untraceable. but my xpath highlights the Logout button.

        Is there a way I can share screenshot with you so you can help me ?

        thanks again,
        Malavika.

        Reply
        • Mukesh Otwani says

          April 29, 2019 at 10:55 PM

          Hi Malavika, you can mail with a screenshot I will look into it.

          Reply
  2. Rajesh Kabra says

    January 20, 2017 at 11:12 AM

    What if test case 1 gets failed and test case 2 is dependent on test case 1, In that case how can we continue our execution for test case 2.
    Can we do that ?

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:06 PM

      Hi Rajesh,

      It will skip second test case.

      Reply
  3. Prashant says

    August 26, 2016 at 11:48 PM

    Hi Mukesh,

    Superb…. Superb….. explanation on dependency. Thanks a ton.

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 3:14 PM

      Thanks Prashant 🙂 Happy weekend.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?