Author - StudySection Post Views - 284 views
TestNG

Dependency In TestNG

Test dependency is one of the beneficial features of the TestNG framework that allows us to make one test method dependent on one or multiple other test methods. Tests dependency only works if the dependent method is part of the same class or any of the inherited base classes.

TestNG allows you to specify dependencies in the following ways:

  1. Using attribute dependsOnMethods in @Test annotations.
  2. Using attribute dependsOnGroups in @Test annotations.

Some points to remember about depends on the method:

  1. On an execution test class or test suite, the parent test method will run before the dependent test.
  2. If we run the dependent test only even then the parent test method will run before the dependent test.
  3. If the parent test method gets failed then the dependent test method will not run and will be marked as skipped.

Here we are taking an example:

Firstly, a user login into the application and get the title of the homepage. Then we assert the home page title with the expected homepage title.

Code:
package Practise;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class dependencyTesting {
public WebDriver driver;
@Test (priority=1)
public void openBrowser() throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
// Open browser window in maximize mode
options.addArguments("--start-maximized");
// Add page load strategy
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
driver = new ChromeDriver(options);
driver.get("https://www.studysection.com");
Thread.sleep(3000);
}
@Test(priority=2)
public void loginPage() throws InterruptedException {
driver.get("https://www.studysection.com/users/login");
driver.findElement(By.id("UserEmail")).sendKeys("email");
driver.findElement(By.id("UserPassword")).sendKeys("password");
driver.findElement(By.xpath("//*[@id=\"User/loginForm\"]/div[4]/div")).click();
String actualTitle = driver.getTitle();
System.out.println(actualTitle);
}
@Test(dependsOnMethods = {"loginPage"})
public void Homepage() {
String expectedTitle = "Your Attempts and Certificates";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
}

Result:

In this example, If a user login credential is not matched with the system then the Login page method will not execute and the dependent Homepage method will also not execute.

English is a commonly used language in almost all fields today. Having a certification in the English language can enhance your career aspects. StudySection offers both basic level and advanced level English Certification Exams to test and certify the English language skills.

Leave a Reply

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

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri