Damien-Amen
Damien-Amen

Reputation: 7492

Reading data from file using DataProvider in testng

Let's say I have a text file with the following data

username=testuser
password=testpassword
[email protected]
address=testaddress
zipcode=12345

Or I have an XML with the following data

<TestData>
   <UserInfo>
       <username>testuser</username>
       <password>testpassword</password>
       <email>[email protected]</email>
       <address>testaddress</address>
   </UserInfo>
</TestData>

I have a test as below

public class DPTest {

   @Test(dataprovider="testdp")
   public void userTest_01(String username, String Password) {

   //Test goes here

   }
}

Another class

public class DPTest2 {

   @Test(dataprovider="testdp")
   public void userTest_02(String email, String address, String password) {

   //Test goes here

   }
}

Can my dataprovider read the values from the above mentioned text file or XML and supply it to the test methods?

As per my understanding the data provider is going to read all the lines in the text file and supply it to the test method and throw an error saying "data provider is trying to provide 6 parameters but Test can only accept 2 parameter" ?

Please help me.

Upvotes: 0

Views: 12457

Answers (2)

StrikerVillain
StrikerVillain

Reputation: 3776

Buddy, you are looking at data provider in the wrong way. Please refer to the testNG documentation. http://testng.org/doc/documentation-main.html

You goal can be achieved by something like the code below. Test class contains test method. it gets data from a dataprovider specified inside another class TestData. In TestData class we define method for accessing data from a file/xml and return it as an 'Object[][]' in '@DataProvider' method

public class Test {
   @Test(dataProvider="testData" dataProviderclass = TestData.class)
   public void userTest(TestData testData) {
       //Test code goes here
   }
}

public class TestData {

   private String username;
   private String password;

   public void setUsername(String username) {
       this.username = username;
   }

   public String getUsername() {
       return username;          
   }

   public void setPassword(String password) {
       this.password = password;
   }

   public String getPassword() {
       return password;
   }

   @DataProvider(name="testData")
   public static Object[][] userTestData (TestData testData) {           
       //Code to read from file/xml
       TestData testData = new TestData();
       testData.setUsername("Get from file/xml");
       testData.setPassword("Get from file/xml")

       return new Object{{testData}}
   }
}

Upvotes: 0

Tiago Engel
Tiago Engel

Reputation: 3663

Yes, that's possible. You can create an annotation to specify the parameters this DataProvider should load from your XML.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlParameters {
   String[] value();    
}

@Test(dataProvider = "XMLFileLoader")
@XmlParameters({"username", "password"})
public void testSomething(String username, String password) {
    // implementation omitted for brevity
}

@DataProvider(name = "XMLFileLoader")
public static Object[][] getDataFromXmlFile(final Method testMethod) {
    XmlParameters parameters = testMethod.getAnnotation(XmlParameters.class);
    String[] fields = parameters.value();
    //load just the fields you want
    return new Object[][] { { "user1", "pass1" } };
}

This code is not "production ready", you should check if the annotation is not null before reading the values, and is probably better move the interface and the logic to load your xml to another class, so you can reuse on another tests.

Upvotes: 4

Related Questions