Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Sunday, 27 March 2016

DataProvider in Multiple Classes

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;
}

}
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.

11 comments:

  1. This is for using the two parameters,
    I 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 !

    ReplyDelete
  2. This is for two rows and for 4 parameters
    So 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{


    }

    }

    ReplyDelete
  3. It's very useful for beginner...
    Explain briefly..
    Thanks for this sharing..

    http://www.besanttechnologies.com/training-courses/software-testing-training/selenium-training-institute-in-chennai

    ReplyDelete
  4. The blog gave me idea about Data provider in multiple classes Thanks for sharing it
    Selenium Training in Chennai

    ReplyDelete
  5. I believe that there will be more wonderful opportunities for those who are coming into this area.
    Best Online Software Training Institute | Selenium Training

    ReplyDelete
  6. 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.
    VMware Exam Centers in Chennai | VMware Exam Centers in Velachery

    ReplyDelete
  7. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Multimedia Course in Chennai | Multimedia Course in Velachery

    ReplyDelete
  8. Great information, very interesting explanation. Thanks for sharing.
    CCNA Training in Chennai | CCNA Training in Velachery

    ReplyDelete

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: