0

Given my .csv data file in format name:reference :-

Johnson;464758,
Smith;839393,
Jones;940392,
Davidson;182939,
Arthurs;938494

And given my test code:

final static String DELIMITER = ",";

@DataProvider(name="references")
public Iterator<Object[]> refDataProvider(){
    try{
        Scanner scanner = new Scanner(new File(CSV_FILE)).useDelimiter(DELIMITER);
        return new Iterator<Object[]>() {
            @Override
            public boolean hasNext() {
                return scanner.hasNext();
            }

            @Override
            public Object[] next() {
                return new Object[]{scanner.next()};
            }
        };
    }catch(FileNotFoundException e){
        e.printStackTrace();
        return null;
    }
}


@Test(dataProvider = "references")
public void submitNewCustomers(String reference){

    visit(customerHomePageUrl);
    customerHomepagePage().selectCustomer();

    customerSearchPage().typeInCustomerNumber(reference);

}

I only wish to pass the customer reference, not the name i.e. just 464758 to the DataProvider. Is there a way I can split the file using a further delimiter on the semi-colon ; ?

1 Answer 1

0

Just apply split while returning inside the overridden method next()

public class DpOne {
        final static String DELIMITER = ",";
    
        @DataProvider(name="references")
        public Iterator<Object[]> refDataProvider(){
            try{
              Scanner scanner = new Scanner( new File("temp.csv"));
              scanner.useDelimiter(DELIMITER);
              
                return new Iterator<Object[]>() {
                    @Override
                    public boolean hasNext() {
                        return scanner.hasNext();
                    }
    
                    @Override
                    public Object[] next() {
                        //apply split here
                        return new Object[]{scanner.next().split(";")[1]};
                    }
                };
            }catch(FileNotFoundException e){
                e.printStackTrace();
                return null;
            }
        }
        
        @Test(dataProvider = "references")
        public void submitNewCustomers(String reference){
    
            System.out.print(reference);
        }
        
        
    
    }

Will display output as shown below

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.