1

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;
        @FindBy(name="firstname")
        public WebElement firstname;
        @FindBy(name="lastname")
        public WebElement lastname;
        @FindBy(name="sex")
        public WebElement gender;
        @FindBy(name="exp")
        public WebElement experience;
        @FindBy(id="datepicker")
        public WebElement date1;
        @FindBy(name="profession")
        public WebElement prof;
        @FindBy(name="tool")
        public WebElement tool;
        @FindBy(id="continents")
        public WebElement contain;
        @FindBy(id="selenium_commands")
        WebElement selcom;
        @FindBy(id="submit")
        WebElement submit;      

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

        @DataProvider(name="first")
        public static Object[][] dataProviderMethod() {
            return new Object[][] { {"rutvi"}};
        }

        @Test(dataProvider="first")
        public void setfirstname(String fname) {
           firstname.clear();
           firstname.sendKeys(fname);
        }

        public void setlastname(String lname) {
           lastname.clear();
           lastname.sendKeys(lname);
        }
   }

And defined another class which contains all the tests.

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

    @Test
    public void valid() throws InterruptedException {
        CommonElements c1=new CommonElements(s1.driver);
        String s=c1.firstname.toString();
        System.out.println("s->"+s);

        c1.setfirstname("s");
        c1.setlastname("Soni");
    }
}
  • 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. – Kshetra Mohan Prusty Dec 26 '18 at 16:40
2

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);
        c1.setfirstname(fname);
        c1.setlastname("Soni");
    }

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 '18 at 7:21
  • Agree, but for that you can use different test data for multiple parameter. – NarendraC Dec 27 '18 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 '18 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 '18 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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