Skip to main content

Data Provider: AUTOMATION TESTING FACTS

=================================================
Data Provide TestNG:

Parameters from Testng.xml can be suite or test level

Parameter from DataProvider can take Method and ITestContext as the parameter.
=============================================================
Test Level TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="TestSuite" thread-count="3" >

<parameter name="author" value="Guru99" />

<parameter name="searchKey" value="India" />

<test name="testGuru">

<parameter name="searchKey" value="UK" />

<classes>

<class name="parameters.ParameterWithTestNGXML">

</class>

</classes>

</test>

</suite>
==================================
Data provider returns a two-dimensional JAVA object to the test method and the test method, will invoke M times in a M*N type of object array. For example, if the DataProvider returns an array of 2*3 objects, the corresponding testcase will be invoked 2 times with 3 parameters each time.
================================
/** Test case to verify google search box
     * @param author
     * @param searchKey
     * @throws InterruptedException
     */

    @Test(dataProvider="SearchProvider")
    public void testMethod(String author,String searchKey) throws InterruptedException{
    {
        WebElement searchText = driver.findElement(By.name("q"));
        //search value in google searchbox
        searchText.sendKeys(searchKey);
        System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
        Thread.sleep(3000);
        String testValue = searchText.getAttribute("value");
        System.out.println(testValue +"::::"+searchKey);
        searchText.clear();
        //Verify if the value in google search box is correct
        Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
    }
    }
    /**
     * @return Object[][] where first column contains 'author'
     * and second column contains 'searchKey'
     */

    @DataProvider(name="SearchProvider")
    public Object[][] getDataFromDataprovider(){
    return new Object[][]
    {
            { "Guru99", "India" },
            { "Krishna", "UK" },
            { "Bhupesh", "USA" }
        };

    }
==================================================================

Comments

  1. ================

    TestClass ParameterDataproviderWithClassLevel.java

    @Test(dataProvider="SearchProvider",dataProviderClass=DataproviderClass.class)
    public void testMethod(String author,String searchKey) throws InterruptedException{

    WebElement searchText = driver.findElement(By.name("q"));
    //Search text in google text box
    searchText.sendKeys(searchKey);
    System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
    Thread.sleep(3000);
    //get text from search box
    String testValue = searchText.getAttribute("value");
    System.out.println(testValue +"::::"+searchKey);
    searchText.clear();
    //verify if search box has correct value
    Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
    }
    }
    =================
    DataproviderClass.java

    package parameters;

    import org.testng.annotations.DataProvider;
    public class DataproviderClass {
    @DataProvider(name="SearchProvider")
    public static Object[][] getDataFromDataprovider(){
    return new Object[][] {
    { "Guru99", "India" },
    { "Krishna", "UK" },
    { "Bhupesh", "USA" }
    };
    }}

    ReplyDelete

Post a Comment