简体   繁体   中英

Parameterized runner class with 2 arguments in constructor

I wish to use a Parameterized Junit class to read from a .csv file. I want to:-

  1. Read a 'placeID' (a String) and append it to a base url to form a webpage
  2. Assert that the Place name 'name' (a String) is as I expect it to be for the place

The tab delimited .csv file contains 2 records as follows (will have 100's records eventually):

132
The Big House

I'm currently getting an Illegal argument exception. What's a slicker way of achieving this? I guess having the relative URL and then test data in seperate files would be better.

My code:

@RunWith(Parameterized.class)
public class PlaceTest {

public static WebDriver driver;
private String placeId;
private String name;
private PropertyPage propertyPage;

public PlaceTest(String page, String name) {
    this.placeId = page;
    this.name = name;
}

@Parameterized.Parameters
public static Collection data() {
    return csvFileAsCollectionOfStringArrays(
            System.getProperty("user.dir") +
                    "/src/test/resources/" +
                    "place_ids.csv");
}

private static Collection<String[]> csvFileAsCollectionOfStringArrays(String csvFileName) {

    List<String[]> csvRows = new ArrayList<String[]>();
    String rawCSVRow;
    BufferedReader csvFileReader = null;
    String delimiter = "\t";

    System.out.println("Reading data from " + csvFileName);

    try {
        csvFileReader = new BufferedReader(new FileReader(csvFileName));

    } catch (FileNotFoundException e) {
        System.out.println("Could not find file " + csvFileName);
        e.printStackTrace();
    }

    int rowNumber = 1;
    try {

        if (csvFileReader != null) {
            while ((rawCSVRow = csvFileReader.readLine()) != null) {
                String delimitedItems[] = rawCSVRow.split(delimiter);
                csvRows.add(delimitedItems);
                rowNumber++;
            }
        }


    } catch (IOException e) {
        System.out.println("Error reading row number " + rowNumber);
        e.printStackTrace();
    }

    try {
        assert csvFileReader != null;
        csvFileReader.close();
    } catch (IOException e) {
        System.out.println("Error closing file " + e.getMessage());
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    return csvRows;

}

@BeforeClass
public static void startDriver() {
    driver = Driver.get();
}

@Before
public void getNextPage() {
    propertyPage = new PropertyPage(driver);
    driver.get(TestWebApp.getURL() + this.placeId);
}

@Test
public void checkNamePresent() {
    WebElement placeName = propertyPage.checkName();
    assertEquals("Expected match on name", this.name, placeName.getText());
}

@AfterClass
public static void quitDriver() {
    driver.quit();

    }
}

Try this:

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import au.com.bytecode.opencsv.CSVReader;

@RunWith(Parameterized.class)
public class PlaceTest {

    private String placeId;
    private String name;

    public PlaceTest(String page, String name) {
        this.placeId = page;
        this.name = name;
    }

    @Parameterized.Parameters
    public static Collection<String[]> data() {
        CSVReader reader = new CSVReader(new InputStreamReader(PlaceTest.class.getResourceAsStream("place_ids.csv")));
        List<String[]> lines;
        try {
            lines = reader.readAll();
            return lines;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new ArrayList<String[]>();
    }


    @Test
    public void checkNamePresent() {
        System.out.println(this.placeId + " " + this.name);
    }
}

The place_ids.csv has to be in: \\src\\test\\resources\\<your package>\\place_ids.csv

Update your pom with CSVReader dependency:

    <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.3</version>
    </dependency>

Update:

csv file:

132, Some text
133, Other text

Your example above has one word per line. The code above was compile and tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM