0

I'm trying to use data in multiple test data files in the same script here. Suppose there are 2 data files with login credentials (Login1.csv and Login2.csv). I need to use credentials in Login1.csv in one environment (on-premise) and credentials in Login2.csv in another environment (cloud) and need to use data in both files in the same script. I have created 2 test cases there (test1 and test2) but I have about 50 test cases in the real project.

The script below is working fine for the first csv file (Login1.csv) but I'm not sure if it's possible to use credentials in Login2.csv in same script (in test1 and test2).

public class Test_Login2 {

    @Test(dataProvider = "dp")
    public void test1(HashMap<String, String> b) {
        System.out.println(b.get("Username"));
        System.out.println(b.get("Password"));
        System.out.println("*************************");
        // -------------other scripts----------------
    }

    @Test(dataProvider = "dp")
    public void test2(HashMap<String, String> b) {
        System.out.println(b.get("Username"));
        System.out.println(b.get("Password"));
        System.out.println("*************************");
        // -------------other scripts----------------
    }

    @DataProvider(name = "dp")
    public Object[][] dpmethod() throws Exception {

        // test data file for one environment (on-premise)
        String csvfile = "C:\\Users\\.....\\Login1.csv";
        CSVReader reader1 = new CSVReader(new FileReader(csvfile));
        //----------script for reading data in login1 file

        // test data file for another environment (cloud)    
        String csvfile = "C:\\Users\\.....\\Login2.csv";
        CSVReader reader2 = new CSVReader(new FileReader(csvfile));
        //----------script for reading data in login2 file
    }
}
  • Could you please elaborate the problem? If you are seeing any issues, could you please share it here? – Shamanth Jan 15 at 17:08
  • How to use data in Login2.csv file in Test1 and Test2 testcases ? – Anu Jan 15 at 17:53
  • create a separate class that calls the methods of Test_Login2 and pass data as required to that methods from the new class. – Prasad_Joshi Jan 16 at 4:38
1

You can pass the environment through command line using the -DEnv="cloud" option and read it in your code. Based on the environment, you can use a particular filepath to generate the dataprovider and pass it to your test cases.

@DataProvider(name="dp")
public Object[][] dpmethod()throws Exception{
   // Reading the environment variable here.
   String env = System.getProperty("Env");
   String csvFile = null;
   if(env.equals("on-premise")){
      // test data file for one environment (on-premise)
      csvFile = "C:\\Users\\.....\\Login1.csv";
   }else if(env.equals("cloud")){
      //test data file for another environment (cloud)
      csvfile = "C:\\Users\\.....\\Login2.csv";
   }else{
      System.out.println("Unsupported environment.");
      System.exit(0);
   }
   CSVReader reader = new CSVReader(new FileReader(csvfile));
   // code for reading data 
   // from one of the files 
   // and create the data object

   // return the data object created above.
}

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.