Using CSV in WebDriver

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Using CSV in WebDriver

lernselenium
Hi,

In the Selenium training videos, Tarun has explained using CSV with maven dependency. Can we use CSV without maven dependency and how?
Reply | Threaded
Open this post in threaded view
|

Re: Using CSV in WebDriver

lernselenium
Hi,

I asked above question because my script was giving error for CSVReader, It said either create enum/class/interface named CSVReader or change it to ClassReader when i moved and highlighted CSVReader. (I am not sure what to choose from above or how to proceed)

I am able to find an answer on my own after trial and error and referring below thread:
http://stackoverflow.com/questions/14135286/read-data-from-csv-file-using-java-in-webdriver

So the solution is if you are not using CSVReader and following the framework Tarun has explained, use below code to read from CSV file:

@DataProvider(name = "regCSVdata")
        public static Object[][] getlogindata() throws IOException {
               
                int i=0;
                File file = new File("<path for CSV file>");
               
        //CSVReader reader new CSVReader(new FileReader (WellbeingData.class.getResource("/regdata.csv").getPath()));
               
                BufferedReader bufrder = new BufferedReader(new FileReader(file));
                String line = null;
               
                List<WellbeingData> reglist = new ArrayList<WellbeingData>();
               
                while((line = bufrder.readLine()) != null) {
                        StringTokenizer st = new StringTokenizer(line,",");
                               
                        while(st.hasMoreTokens()) {
                               
                                WellbeingData data = new WellbeingData();
                               
                                data.setusername(st.nextToken());
                                data.setpassword(st.nextToken());
                                reglist.add(data);
                                i++;
                        }
                }
                Object[][] data1 = new Object[i][1];
                for(int j=0; j<data1.length; j++) {
                        for(int k=0; k<data1[j].length; k++) {
                                data1[j][k] = reglist.get(j);
                        }
                }
                bufrder.close();
                return(data1);
        }

I hope this will help somebody. Let me know anybody has any comments.

Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: Using CSV in WebDriver

softwaretestingforum
Administrator
thanks for sharing solution with us.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Using CSV in WebDriver

Bhaskar
In reply to this post by lernselenium
Hi,
Thanks for your post.
I was struggling with the same CSVReader problem. Your post helped me a lot. But still I am getting
some
SKIPPED: testNewRegistration2
java.lang.RuntimeException: java.lang.NullPointerException
        at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161)
        at org.testng.internal.Parameters.handleParameters(Parameters.java:429)

......
something like this