0

I am new to Selenium Webdriver Testng and trying to Develop Data Driven framework using CSVReader

I have 5 tests in Testng and have to run those all on different environments. So basically I have to pick a different url and client, but other pages, elements remain same.

My Question is, Where do I put while loop so that it reads first line and execute all the tests and then read second line and so on.

I tried initialize CSV reader at class level, before suite, before method etc. but it throws error, it works for a particular test if I am putting inside @Test but that doesn't resolve the purpose, I couldn't find any relevant answer on the internet.

Below is the high level code:

public class Portal1 {
    WebDriver driver;
    @BeforeSuite
    public void beforeMethod() {
    }
    @BeforeTest
    public void SetUp() {
    config = new Configreader();

    }
@DataProvider(name = "TestData") 
public Object [][] getData() { Object [][] 
data=new Object[2][2]; 
data[0][0]="url1"; 
data[0][1]="client1"; 
data[1][0]="url2"; 
data[1][1]="client2"; 
return data; 
} 

@Test(dataProvider = "TestData",priority =0) 
public void RI_Select_an_Employer_page(String url,String client) throws Exception { 
driver.get(url); 
} 
@Test(dataProvider = "TestData",priority =1) 
    public void RI_Employer_Information_page(String url,String client) {
    WebElement  linkclient = driver.findElement(By.linkText(client));
    linkclient.click();
  @AfterMethod
          public void tearDown(ITestResult result) {
        }
  @AfterSuite
  public void tearDown() {
  driver.quit();
  }
2

You shouldn't use while loops to provide test data to your tests if you use some dedicated test frameworks like TestNg.

Instead you should use the mechanisms provided by such frameworks. For TestNg you can provide test data either through xml file or though a special method providing test data for each run.

Details: http://testng.org/doc/documentation-main.html#parameters

For example if you would like your tests are fed with test data from excel, then you should implement code like this (a bit changed code from the documentation example):

@DataProvider(name = "test1")
public Object[][] createData1() {

    int n = 0; // Load the number of the excel file rows later here
    int k = 0; // Load the number of parameter later here

    // Read your excel file. Assign the corresponding values to n and k

    Object[][] testData = new Object[n][k];

    // Fill testData with values from excel

    return testData;

}

Then you just describe your test methods in the following way:

@Test(dataProvider = "test1")
public void verifyData1(Object[] params) {
    // Here you use params[0], params[1], .. to take 
    // new set of parameters for each new run of your test.
    // testNg will automatically run your single test for
    // one time for EACH test data entry
}

I would also recommend to read carefully TestNg documentation to learn what are the other interesting things you can use to make your tests more effective.

UPD: I have tested the code from your question and it works fine for me:

enter image description here

  • I think reading from Excel or CSV is the easiest way to maintain test data, we can update the CSV, Excel files very easily, in case we need to update test data. I wanted to create a framework where a novice person can just go and update data in excel sheet and run the execution. Even if I put data in XML, looping would still be required to run the same tests multiple times for different test data. Let me know what you think ? – viki Jul 30 '18 at 15:22
  • Looping is not required whatever way you want do choose. TestNg just gives you an abstraction allowing you to implement any data provider you need. If you think Excel would be more convenient for your users, then you just implement that method in the way to fetch records from excel file. – Alexey R. Jul 30 '18 at 15:35
  • Thanks Alexey, I'll try to implement Dataprovider and see if my problem is handled as I am expecting it to be, I'll respond here after I have tried it. – viki Jul 30 '18 at 16:19
  • Hi Alexey, I tried to implement @Dataprovider, Again facing the same issue. I have 5 tests and I want all the tests to execute in all the iterations instead of Test1 getting executed multiple times leaving all other tests. How to achieve that ? My first test case picks a particular url, client and all other tests are based on test1, but what I see is only Test1 is getting iterated again and again. Please help. – viki Jul 30 '18 at 21:10
  • You should annotate all your tests pointing them to the data provider. – Alexey R. Jul 30 '18 at 21:24

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.