If you have to use one dataprovider in multiple classes of your project so for that we need to make our dataprovider static and then after making it static we can use that in every class where we need.
You have to follow this rule only just make your data provider as static and give the name to your data provider .
@Test(dataProviderClass = YourDataProviderClassname.class,dataProvider = dataprovidername)
By this Program/code you can run your dataprovider in multiple classes
import org.testng.annotations.DataProvider;
public class dataProvider {
@DataProvider(name = "logintestdataprovider")
//give the name to your dataprovider.Here your dataprovider name = logintestdataprovider
public static Object[][] getData(){
Object data[][] = new Object [3][2];
data[0][0] = "ritu7181";
data[0][1] = "password1";
data[1][0] = "ritu7180";
data[1][1] = "password2";
data[2][0] = "ritu7182";
data[2][1] = "password3";
return data;
}
}
You have to follow this rule only just make your data provider as static and give the name to your data provider .
@Test(dataProviderClass = YourDataProviderClassname.class,dataProvider = dataprovidername)
By this Program/code you can run your dataprovider in multiple classes
import org.testng.annotations.DataProvider;
public class dataProvider {
@DataProvider(name = "logintestdataprovider")
//give the name to your dataprovider.Here your dataprovider name = logintestdataprovider
public static Object[][] getData(){
Object data[][] = new Object [3][2];
data[0][0] = "ritu7181";
data[0][1] = "password1";
data[1][0] = "ritu7180";
data[1][1] = "password2";
data[2][0] = "ritu7182";
data[2][1] = "password3";
return data;
}
}
Now next step is to call this dataprovider to your test class.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Registration {
@Test(dataProviderClass = dataProvider.class,dataProvider = "logintestdataprovider")
public void Login(String username,String password) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("https://gmail.com");
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys(username);
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='next']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(password);
driver.findElement(By.xpath("//*[@id='signIn']")).click();
Thread.sleep(5000);
}
}
You can use that dataprovider in multiple classes you need.I have just use in one class you can use that in multiple classes.




This is for using the two parameters,
ReplyDeleteI want to use it for 4 or more parameters (Suppose of the contact form), Can you please tell me the steps for it.
Thanks in Advance !
This is for two rows and for 4 parameters
ReplyDeleteSo for 4 use for parameters like public static Object[][] getData(){
Object data[][] = new Object [2][4];
data[0][0] = "firstname1";
data[0][1]= "lastname1";
data[0][2]= "email";
data[0][3] = "contactno";
data[1][0] = "firstname2";
data[1][1]= "lastname2";
data[1][2]= "email2";
data[1][3] = "contactno2";
return data;
}
}
Here you can call and change the code accordingly
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Registration {
@Test(dataProviderClass = dataProvider.class,dataProvider = "logintestdataprovider")
public void Login(String firstname,String lastname,String email,String contact) throws InterruptedException{
}
}
It's very useful for beginner...
ReplyDeleteExplain briefly..
Thanks for this sharing..
http://www.besanttechnologies.com/training-courses/software-testing-training/selenium-training-institute-in-chennai
The blog gave me idea about Data provider in multiple classes Thanks for sharing it
ReplyDeleteSelenium Training in Chennai
I believe that there will be more wonderful opportunities for those who are coming into this area.
ReplyDeleteBest Online Software Training Institute | Selenium Training
Thank you for sharing such a nice and interesting blog with us. i have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information.
ReplyDeleteVMware Exam Centers in Chennai | VMware Exam Centers in Velachery
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteMultimedia Course in Chennai | Multimedia Course in Velachery
Great information, very interesting explanation. Thanks for sharing.
ReplyDeleteCCNA Training in Chennai | CCNA Training in Velachery
Great information, very interesting explanation. Thanks for sharing.
ReplyDeleteMatlab Training Institute in Chennai | Matlab Training Institute in Velachery
Great information, very interesting explanation. Thanks for sharing.
ReplyDeleteCertified Ethical Hacking Training in Chennai | Certified Ethical Hacking Training in Velachery
Great information, very interesting explanation. Thanks for sharing.
ReplyDeleteNo.1 Tally ERP 9.0 Training institute in Chennai | No.1 Tally ERP 9.0 Training institute in Velachery