【问题标题】:Returning two arguments from a csv list using BufferedReader and splitting on comma使用 BufferedReader 从 csv 列表返回两个参数并以逗号分隔
【发布时间】:2018-08-24 11:50:50
【问题描述】:

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

这是我的 csv:

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

这是我阅读 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 或类似的,然后在逗号上拆分但有点挣扎。

【问题讨论】:

  • 我不明白。你有什么问题?
  • 您能否换一种说法,因为我很难理解您遇到的问题。

标签: java csv junit4 bufferedreader


【解决方案1】:

如果您想知道如何拆分 csv 文件的每一行以创建 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());
    });
}

我希望这会有所帮助……

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多