java - 使用 BufferedReader 从 csv 列表返回两个参数并以逗号分隔
问题描述
请原谅初学者的问题,但我首先是一名测试人员,并且正在努力理解如何解决这个问题。我只是尝试通过使用缓冲输入流读取器读取部分网站 url 及其相应的联合组织登录 ID 并从每行返回两个值(用逗号分隔)来填充一些测试数据。
这是我的csv:
website1.uk, website1syndicator
website2.uk, website2syndicator
website3.uk, website3syndicator
这是我阅读 csv 并使用一个 String 元素填充 List 的课程:
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 文件的每一行以创建一个类对象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());
});
}
我希望这会有所帮助……</p>
推荐阅读
- reactjs - 如何使用 mongodb 向嵌套文档添加修饰符
- docker-compose - 如何使用自定义 SSL 证书文件在 AWS ubuntu 上设置 Traefik 和闪亮服务器(dockerized)?
- game-development - 面向数据的设计和缓存局部性
- python - 从发票中提取数据点和表格数据 - 第三方 API
- flutter - SliverGrid中的flutter UI渲染库错误
- api - 无法使用带有工作 api 的 http.get 获取数据
- javascript - 为什么即使在 express 中调用 next() 之后,底层代码也会在一个块中执行?
- angular - window.scroll(0,0) 不适用于 Angular 10
- javascript - 嵌入消息未发送到指定通道
- c++ - 尝试将控制台输出重定向到文本文件时,C++ 程序不会启动