Fetching single user data for testng dataprovider

574 views
Skip to first unread message

Aswathy

unread,
Aug 2, 2014, 8:33:17 AM8/2/14
to testng...@googlegroups.com
Hi
I am a novice in selenium webdriver.

I need some help in @dataprovider

Scenario: I have one excel file which has 5 different users. I have written one dataprovider to to fetch the data and pass to @test which tests for the login feature.

I wanted to use same dataprovider and pass to another @test, but here i need only one user out of 5 users to verify functionality of application.
Is there any way to implement it.

Thanks in advance


Aswathy

Krishnan Mahadevan

unread,
Aug 3, 2014, 12:01:32 AM8/3/14
to testng...@googlegroups.com
Aswathy,

Here’s what should help you get past your predicament.

  1. Define a custom annotation which has the ability to take in the row number which needs to be executed.
  2. Enhance your data provider to inspect the incoming test method to check for the presence of the new annotation. If found, retrieve row numbers as specified by the new annotation, if not revert back to the default behaviour of providing back all the rows.
Here’s a sample that should help you get started.

The custom annotation would look like this.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.METHOD)
public @interface RowNumber {
    String rowNumber () default "";
}


The test code would look like this.

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import rationale.emotions.annotations.RowNumber;

import java.lang.reflect.Method;

public class SampleClass {
    private static Object[][] testData = {
        {1},
        {2},
        {3},
        {4},
        {5}
    };

    @RowNumber (rowNumber = "2")
    @Test (dataProvider = "getData")
    public void specialTestMethod (int value) {
        System.out.println ("specialTestMethod() Incoming value :" + value);

    }

    @Test (dataProvider = "getData")
    public void ordinaryTestMethod (int value) {
        System.out.println ("ordinaryTestMethod() Incoming value :" + value);
    }

    @DataProvider (name = "getData")
    public Object[][] getData (Method method) {
        RowNumber rows = method.getAnnotation (RowNumber.class);
        if (rows == null) {
            return testData;
        }
        int whichRow = Integer.parseInt (rows.rowNumber ());

        return new Object[][] {testData[whichRow-1]};
    }
}


Hope that helps you get started !


Thanks & Regards
Krishnan Mahadevan

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


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Aswathy

unread,
Aug 3, 2014, 11:57:53 PM8/3/14
to testng...@googlegroups.com
Krishnan,

Thanks alot for the detailed explaination with code snippets. 

I have done the modification in my code based on input above.

Once again Thank you :)


Regards,

Aswathy
Reply all
Reply to author
Forward
0 new messages