使用 BufferedReader 从 csv 列表返回两个参数并以逗号分隔

Returning two arguments from a csv list using BufferedReader and splitting on comma

请原谅初学者的问题,但我首先是一名测试人员,并且正在努力理解如何解决这个问题。我只是试图通过使用缓冲输入流 reader 读取部分网站 url 及其相应的辛迪加登录 ID 并从每一行返回两个值(用逗号分隔)来填充一些测试数据。

这是我的 csv:

 website1.uk, website1syndicator
 website2.uk, website2syndicator
 website3.uk, website3syndicator

这是我 class 读取 csv 并用一个字符串元素填充列表的方法:

public class AbstractTestAllSites extends PageBase {

private static Logger log = LoggerFactory.getLogger(AbstractTestAllSites.class);

private static List<String> allWebsiteNames;

static {
    try (InputStream websiteListInputStream = AbstractTestAllSites.class.getResourceAsStream("/websites/my_sites.csv")) {
        readAllWebsiteNamesFrom(websiteListInputStream);
    } catch (IOException e) {
        log.error("Failed to read websitelist!", e);
    }
}

private static void readAllWebsiteNamesFrom(InputStream input) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
    List<String> websites = new ArrayList<String>();
    String listLine;
    while ((listLine = reader.readLine()) != null) {
        listLine = listLine.trim();
        if (!(listLine.startsWith("#") || isBlank(listLine))) {
            websites.add(listLine);
        }
    }
    allWebsiteNames = unmodifiableList(websites);
}

@Parameterized.Parameters
public static final List<String> data() {
    return allWebsiteNames;
}

}

然后我可以像这样将网站端点传递到我的测试中:

private static final String url = "http://mydomain.";
private String website;
private String syndicator;
public static WebDriver driver;

public TestAllSitesTest(String website, String syndicator){
    this.website = website;
    this.syndicator = syndicator;
}

@Before
public void getNextWebsite(){
    driver.get(url + this.website);
}
//run my tests here...

...并遍历它们直到完成。但是我如何传递两个参数以便我可以访问 syndicator 变量 - 可能需要一个 HashMap 或类似的然后在逗号上拆分但有点挣扎。

如果您想知道如何拆分 csv 文件的每一行以创建 class TestAllSitesTest 的对象(它有一个采用网站和辛迪加器的构造函数),您可以按如下方式进行(在您的代码中的所需位置,这只是一个显示示例的主要方法):

public static void main(String[] args) {
    // create two ArrayLists, first one containing lines, second containing desired objects
    List<String> websites = new ArrayList<String>();
    List<TestAllSitesTest> testAllSitesTests = new ArrayList<TestAllSitesTest>();

    // add csv lines to the first ArrayList
    websites.add("website1.uk, website1syndicator");
    websites.add("website2.uk, website2syndicator");
    websites.add("website3.uk, website3syndicator");

    // iterate the list containing the csv lines
    websites.forEach((String website) -> {
        // split one line into the desired two parts, eliminating comma and space
        String[] splitWebsite = website.split(", ");
        // create a new object passing the parts of the split line as constructor parameters
        TestAllSitesTest test = new TestAllSitesTest(splitWebsite[0], splitWebsite[1]);
        testAllSitesTests.add(test);
    });

    // print the resulting objects
    testAllSitesTests.forEach((TestAllSitesTest t) -> {
        System.out.println("Website: " + t.getWebsite()
                + ", Syndicator: " + t.getSyndicator());
    });
}

希望对您有所帮助……