How to pass different data providers for single method and controlled by parameters

140 views
Skip to first unread message

Om Prakash

unread,
Jun 6, 2014, 3:19:53 AM6/6/14
to seleniu...@googlegroups.com
Hi 

I want to use the data provider based on passing the parameter  through XML on single test method. 

for example 

@Test(dataprovider='')
@Parameters("usertype")
method(){

}

if i pass data provider as admin it should take DataProvider1 and if i pass user type as sub level it should take DataProvider2 .

Can any one help to me.

Thanks & Regards
OmPrakash

Krishnan Mahadevan

unread,
Jun 6, 2014, 8:51:17 AM6/6/14
to Selenium Users

This is how you can do it :
1 You would need to ensure that your test method now basically accepts a parameter of type Object
2. You would be providing the data provider of your choice via the JVM argument [ -Ddp in this case]
3. You need an annotation transformer to get the job done.

Code :

package org.rationale.emotions.testng;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.annotations.DataProvider;
import org.testng.annotations.ITestAnnotation;
import org.testng.annotations.Test;

public class ParameterizedDataProvider {
    @Test(dataProvider = "dates")
    public void f(Object data) {
        if (data instanceof MyDate) {
            System.out.println("Printing date");
            System.out.println(((MyDate) data).getDateInformationToPrint());
        }
        if (data instanceof Student) {
            System.out.println("Printing student information");
            System.out.println(((Student) data).getStudentInformation());
        }
    }

    @DataProvider(name = "dates")
    public Object[][] dp() {
        return new Object[][] { { new MyDate(1, 1, 2011) }, { new MyDate(1, 1, 1989) } };
    }

    @DataProvider(name = "students")
    public Object[][] anotherdp() {
        return new Object[][] { { new Student("Bob", 1) }, { new Student("Jimmy", 2) } };
    }

    public static class MyDate {
        private int day, month, year;

        public MyDate(int day, int month, int year) {
            this.day = day;
            this.month = month;
            this.year = year;
        }

        public String getDateInformationToPrint() {
            return day + "-" + month + "-" + year;
        }
    }

    public static class Student {
        private String name;
        private int rollNumber;

        public Student(String name, int rollNumber) {
            this.name = name;
            this.rollNumber = rollNumber;
        }

        public String getStudentInformation() {
            return name + "," + rollNumber;
        }
    }

    public static class MyParser implements IAnnotationTransformer {

        @SuppressWarnings("rawtypes")
        @Override
        public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,
                Method testMethod) {
            System.out.println("Invoked");
            String dataProviderName = System.getProperty("dp");
            System.out.println("Data Provider Name " + dataProviderName);
            if (dataProviderName == null || dataProviderName.trim().isEmpty()) {
                return;
            }
            annotation.setDataProvider(dataProviderName);
        }
    }
}


Suite XML File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<listeners>
<listener
class-name="org.rationale.emotions.testng.ParameterizedDataProvider$MyParser" />
</listeners>

<test name="Test" verbose="2">
<classes>
<class name="org.rationale.emotions.testng.ParameterizedDataProvider" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->




 mvn test -DtestSuite=parameterizedDataProvider.xml [ Using the default data provider]

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Invoked
Data Provider Name null
Printing date
1-1-2011
Printing date
1-1-1989
PASSED: f(org.rationale.emotions.testng.ParameterizedDataProvider$MyDate@255e4373)
PASSED: f(org.rationale.emotions.testng.ParameterizedDataProvider$MyDate@4f9e462f)

===============================================
    Test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

mvn test -DtestSuite=parameterizedDataProvider.xml -Ddp=students [ Changing the data provider via JVM arguments]

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Invoked
Data Provider Name students
Printing student information
Bob,1
Printing student information
Jimmy,2
PASSED: f(org.rationale.emotions.testng.ParameterizedDataProvider$Student@2011e07f)
PASSED: f(org.rationale.emotions.testng.ParameterizedDataProvider$Student@75a9c24b)

===============================================
    Test
    Tests run: 2, Failures: 0, Skips: 0
===============================================






Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/96e0001c-f935-46eb-9bcc-06ab88023323%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Om Prakash

unread,
Jun 6, 2014, 9:41:38 AM6/6/14
to seleniu...@googlegroups.com
Thank you Krishnan for your reply.
I have one more idea for selection of data provider .
It is almost like u told but we are passing all data providers in to methods and using our required by passing a key value.
I hope this will work.

Thank you.

Om Prakash

unread,
Jun 6, 2014, 9:44:48 AM6/6/14
to seleniu...@googlegroups.com


Thank you Krishnan for your reply.
I have one more idea for selection of data provider .
It is almost like u told but we are passing all data providers in to methods and using our required by passing a key value.
I hope this will work.
Like bellow 
@Test(dataprovider=dp1,dp2,dp3'')
@Parameters("usertype")
method(dp1,dp2,dp3){
if(usertype.contains("A")){
dp=dp1;
}
if(usertype.contains("B")){
dp=dp2;
}

Om Prakash

unread,
Jun 9, 2014, 8:29:30 AM6/9/14
to seleniu...@googlegroups.com
Like this possible?

Om Prakash

unread,
Jun 13, 2014, 9:13:04 AM6/13/14
to seleniu...@googlegroups.com
can any one help to this this .
here data providers are contain same type.

Regards
omprakash

Krishnan Mahadevan

unread,
Jun 13, 2014, 9:59:36 AM6/13/14
to Selenium Users
You were already given one solution here right ? Does that not work for you ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Om Prakash

unread,
Jun 14, 2014, 6:54:45 AM6/14/14
to seleniu...@googlegroups.com
Its not working because we can't pass more than single data provider .

Regards
Omprakash

Om Prakash

unread,
Jun 17, 2014, 2:08:09 AM6/17/14
to seleniu...@googlegroups.com
I am trying in this way but unable to execute.

private static final String logindata = null;
@BeforeClass(alwaysRun=true) 
@Parameters({"browser","user"})
public void setUp(@Optional("firefox") String browser, @Optional ("admin") String user) {
if(userType.equalsIgnoreCase("admin")){
final String logindata = "loginData";
}
else if(userType.equalsIgnoreCase("superviser")){
final String logindata = "superviser";
}
}
}
@Test(dataProviderClass = LoginDP.class, dataProvider = logindata)
public void login(LoginDP logindp) throws InterruptedException {
//Login code

Krishnan Mahadevan

unread,
Jun 17, 2014, 2:11:25 AM6/17/14
to Selenium Users
Why do you believe that this query has anything to do with WebDriver/Selenium ?

I suggest that you please post this query on the TestNG forum.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Reply all
Reply to author
Forward
0 new messages