TestNG Parameters using XML file for Data Driven Testing
File: VerifyTitle.java
package tests;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class VerifyTitle
{   
    WebDriver driver;
    
    @Test
    @Parameters("browser")
    public void OpenBrowser(String bname)
    {
        if(bname.equals("firefox"))
        {
            System.setProperty("webdriver.gecko.driver", "/home/kodingwindow/drivers/geckodriver");
            driver = new FirefoxDriver();
        }
        else if (bname.equals("chrome")) 
        {
            System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
            driver = new ChromeDriver();
        }
        else
        {
            System.out.println("Browser not found");
        }
        
        driver.navigate().to("http://newtours.demoaut.com");
        if(driver.getTitle().equals("Welcome: Mercury Tours"))
        {
            System.out.println("Test Passed");
        }
        else
        {
            System.out.println("Test Failed");
        }
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.close();
    }
}
File: testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test">
    <test name="VerifyTitleUsingChrome">
    <parameter name="browser" value="chrome"/>
        <classes>
            <class name="tests.VerifyTitle"/>
        </classes>
    </test>
    
    <test name="VerifyTitleUsingFirefox">
    <parameter name="browser" value="firefox"/>
        <classes>
            <class name="tests.VerifyTitle"/>
        </classes>
    </test>   
</suite>
Output
[RemoteTestNG] detected TestNG version 7.0.0

Test Passed
Test Passed

===============================================
Test
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
TestNG Data Provider Annotation for Data Driven Testing
File: VerifyTitle.java
package tests;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class VerifyTitle
{   
    WebDriver driver;
    @Test (dataProvider="browser")
    public void OpenBrowser(String browser)
    {
        if(browser.equals("firefox"))
        {
            System.setProperty("webdriver.gecko.driver", "/home/kodingwindow/drivers/geckodriver");
            driver = new FirefoxDriver();
        }
        else if (browser.equals("chrome")) 
        {
            System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
            driver = new ChromeDriver();
        }
        else
        {
            System.out.println("Browser not found");
        }
        
        driver.navigate().to("http://newtours.demoaut.com");
        if(driver.getTitle().equals("Welcome: Mercury Tours"))
        {
            System.out.println("Test Passed");
        }
        else
        {
            System.out.println("Test Failed");
        }
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.close();
    }
    
    @DataProvider(name="browser")
    public Object[] browserName()
    {
        return new Object[][] { {"firefox"}, {"chrome"} };
    }
}
Output
[RemoteTestNG] detected TestNG version 7.0.0

Test Passed
Test Passed
PASSED: OpenBrowser("firefox")
PASSED: OpenBrowser("chrome")

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
Advertisement