簡體   English   中英

在構造函數中帶有2個參數的參數化運行器類

[英]Parameterized runner class with 2 arguments in constructor

我希望使用參數化Junit類從.csv文件讀取。 我想要:-

  1. 讀取“ placeID”(字符串)並將其附加到基本網址以形成網頁
  2. 斷言地名“ name”(字符串)與我期望的地名相同

制表符分隔的.csv文件包含2條記錄,如下所示(最終將有100條記錄):

132
The Big House

我當前收到一個非法參數異常。 實現這一目標的巧妙方法是什么? 我想擁有相對URL,然后在單獨的文件中測試數據會更好。

我的代碼:

@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();

    }
}

嘗試這個:

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);
    }
}

place_ids.csv必須位於: \\src\\test\\resources\\<your package>\\place_ids.csv

使用CSVReader依賴項更新pom:

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

更新:

CSV文件:

132, Some text
133, Other text

上面的示例每行只有一個單詞。 上面的代碼已經過編譯和測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM