【发布时间】: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