How to use priority and dependsOnMethods parameter in TestNG
File: KWTestNG.java
package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class KWTestNG 
{
    WebDriver driver;
    @Test(priority = 0)
    public void OpenBrowser()
    {
        System.out.println("Open Block");
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }
    @Test(priority = 1)
    public void Navigate()
    {
        System.out.println("Navigation Block");
        driver.navigate().to("http://newtours.demoaut.com");
    }
    @Test(dependsOnMethods = {"Login"}, priority = 2)
    public void Logout()
    {
        System.out.println("Logout Block");
        driver.findElement(By.linkText("SIGN-OFF")).click();
        driver.close();
    }
    @Test(priority = 2)
    public void Login()
    {
        System.out.println("Login Block");
        driver.findElement(By.name("userName")).sendKeys("mercury");
        driver.findElement(By.name("password")).sendKeys("mercury");
        driver.findElement(By.name("login")).click();
    }
}
File: testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Selenium Project">
    <test name="Login">
        <classes>
            <class name="tests.KWTestNG"/>
        </classes>
    </test>
</suite>
Output
[RemoteTestNG] detected TestNG version 7.0.0
Open Block
Navigation Block
Login Block
Logout Block

===============================================
Selenium Project
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
How to use groups and dependsOnGroups parameter in TestNG
File: KWTestNG.java
package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class KWTestNG 
{
    WebDriver driver;
    @Test(groups = {"OpenBrowserNavigate"})
    public void OpenBrowserNavigate()
    {
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.navigate().to("http://newtours.demoaut.com");
    }
    @Test(dependsOnGroups = {"OpenBrowserNavigate"})
    public void LoginLogout()
    {
        driver.findElement(By.name("userName")).sendKeys("mercury");
        driver.findElement(By.name("password")).sendKeys("mercury");
        driver.findElement(By.name("login")).click();
        driver.findElement(By.linkText("SIGN-OFF")).click();
        driver.close();
    }
}
File: testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Selenium Project">
    <test name="Login">
        <classes>
            <class name="tests.KWTestNG"/>
        </classes>
    </test>
</suite>
Output
[RemoteTestNG] detected TestNG version 7.0.0

===============================================
Selenium Project
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
Advertisement