简体   繁体   English

在构造函数中带有2个参数的参数化运行器类

[英]Parameterized runner class with 2 arguments in constructor

I wish to use a Parameterized Junit class to read from a .csv file. 我希望使用参数化Junit类从.csv文件读取。 I want to:- 我想要:-

  1. Read a 'placeID' (a String) and append it to a base url to form a webpage 读取“ placeID”(字符串)并将其附加到基本网址以形成网页
  2. Assert that the Place name 'name' (a String) is as I expect it to be for the place 断言地名“ name”(字符串)与我期望的地名相同

The tab delimited .csv file contains 2 records as follows (will have 100's records eventually): 制表符分隔的.csv文件包含2条记录,如下所示(最终将有100条记录):

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. 我想拥有相对URL,然后在单独的文件中测试数据会更好。

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 place_ids.csv必须位于: \\src\\test\\resources\\<your package>\\place_ids.csv

Update your pom with CSVReader dependency: 使用CSVReader依赖项更新pom:

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

Update: 更新:

csv file: CSV文件:

132, Some text
133, Other text

Your example above has one word per line. 上面的示例每行只有一个单词。 The code above was compile and tested. 上面的代码已经过编译和测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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