Java 循环文件并使用正则表达式查找文本
我正在使用Selenium来测试web页面,并希望用一种更简单的方法来更新测试用例(对于这个问题并不重要) 我现在用这个循环行:Java 循环文件并使用正则表达式查找文本,java,regex,selenium,Java,Regex,Selenium,我正在使用Selenium来测试web页面,并希望用一种更简单的方法来更新测试用例(对于这个问题并不重要) 我现在用这个循环行: driver.get("http://vg.no"); //open the web page try { BufferedReader reader = new BufferedReader(new FileReader("//Users//file.txt")); try { String l
driver.get("http://vg.no"); //open the web page
try {
BufferedReader reader = new BufferedReader(new FileReader("//Users//file.txt"));
try {
String line = null;
while ((line = reader.readLine()) != null) {
driver.findElement(By.cssSelector(line)).click();; //find and click on the data specified in every line in the document
}
} finally {
reader.close();
}
} catch (IOException ioe) {
System.err.println("oops " + ioe.getMessage());
}
文本文件内容示例:
a[href*='//nyheter//meninger//']
img[class*='logo-red']
img[class*='article-image']
我想将其重建为一个基于正则表达式启动不同命令的解决方案。
我试图让它以这种方式工作:
vg.no //this will start driver.get("vg.no")
img[class*='logo-red'] //this will start driver.findElement(By.cssSelector("img[class*='logo-red']")).click()
img[class*='article-image']
ItAvisen.no
img[class*='article-image']
img[class*='article-image']
是否有一种方法可以使用正则表达式根据文本文件中的内容启动代码的dirrerent部分,并将文本文件中的部分内容用作变量
在收到cvester的反馈后,其工作方式如下: 查找img的匹配项[class*='logo-red']
String regexp = "img\\[class\\*=\\'*\\'(.*)\\]";
boolean match = line.matches(regexp);
它仍然是基于线路的吗? 在这种情况下,您可以逐行读取,并对您识别的每个案例使用
String.matches(String regex)
如果您可以指定更具体的信息,我可能会为您提供更好的解决方案。随着项目的发展,您将很难维护此文件,我建议您使用属性文件,您可以将其保存在键值对中。如果您需要更多信息,请返回我正在考虑使用单独的文件和经常使用的值的变量,但我想我必须先解决这部分,才能使用文本文件写出完整的测试。我将尝试使用该解决方案。谢谢