I have created a sample project using the page factory. Now I want to use the data provider in the code and this is how I have done below. Is the approach correct? I have defined CommonElements class to initialise the WebElements:

    public class CommonElements {
        public static WebElement element;

        WebDriver driver;
        public WebElement firstname;
        public WebElement lastname;
        public WebElement gender;
        public WebElement experience;
        public WebElement date1;
        public WebElement prof;
        public WebElement tool;
        public WebElement contain;
        WebElement selcom;
        WebElement submit;      

        public CommonElements(WebDriver driver) {
           PageFactory.initElements(driver, this);

        public static Object[][] dataProviderMethod() {
            return new Object[][] { {"rutvi"}};

        public void setfirstname(String fname) {

        public void setlastname(String lname) {

And defined another class which contains all the tests.

public class Validdata {
    Setup s1=new Setup();

    public void valid() throws InterruptedException {
        CommonElements c1=new CommonElements(s1.driver);
        String s=c1.firstname.toString();

  • 1
    Really could not understand your design considerations. Your question is also not clear. However if you want to make use of a data provider of one class in another, just add another attribute 'dataProviderClass=className.class' in the @Test annotation and make the data provider method as static. testng.org/doc/documentation-main.html#parameters-dataproviders -- documentation for reference. Dec 26, 2018 at 16:40

1 Answer 1


If I have understood your question correctly, then following are the changes that you need to do:

1. The method setfirstname is not a Test method, so remove the @Test annotation.
2. The method valid under the Validdata class is a Test method so add the annotation @Test(dataProvider="first", dataProviderClass=CommonElements.class)

So your test method looks like this:

    @Test(dataProvider="first", dataProviderClass=CommonElements.class)
    public void valid(String fname) throws InterruptedException {
        CommonElements c1=new CommonElements(s1.driver);

Your firstname is set as rutvi and lastname to Soni on your web browser.

On a side note, the below statement doesn't make sense.

String s=c1.firstname.toString();

Here, you are trying to convert firstname (which is a WebElement) to String. It wouldn't give you any meaningful name.

  • If we have to pass the multiple parameter in the data provider then we can not pass if we call data provider above the valid method.
    – Rutvi Soni
    Dec 27, 2018 at 7:21
  • Agree, but for that you can use different test data for multiple parameter. Dec 27, 2018 at 8:00
  • Ya but we can pass multiple data in the data provider but how we can call that methodpublic void valid() throws InterruptedException { CommonElements c1=new CommonElements(s1.driver); c1.setfirstname("s"); c1.setlastname("Soni"); } if we are create object and call the method through the object then how can we pass the parameter through data provider
    – Rutvi Soni
    Dec 27, 2018 at 14:03
  • You don't have to create object and invoke the valid() method explicitly, it is invoked by the test framework. Since you have specified the dataProvider for the test (with @Test annotation), the method dataProviderMethod() provides data to the test method valid().
    – Shamanth
    Dec 28, 2018 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.