Java 当实际消息中的行发生更改时,如何将实际消息与预期消息进行比较
我正试图通过java-selenium比较下面的消息。但是在执行过程中,实际消息中的行正在更改。请告诉我如何处理它 例: 我尝试了以下方法,但失败了:Java 当实际消息中的行发生更改时,如何将实际消息与预期消息进行比较,java,selenium,selenium-webdriver,string-comparison,Java,Selenium,Selenium Webdriver,String Comparison,我正试图通过java-selenium比较下面的消息。但是在执行过程中,实际消息中的行正在更改。请告诉我如何处理它 例: 我尝试了以下方法,但失败了: if(actMsg.contains(expMsg){ System.out.println("both the messages are same") } 请告诉我如何解决此问题。据我了解,实际消息的顺序无关紧要,您只需检查实际消息中是否存在所有预期消息?在这种情况下,将每一行保存在一个单独的变量/数组中,然后验证每个预期消息在实际使用中是否
if(actMsg.contains(expMsg){
System.out.println("both the messages are same")
}
请告诉我如何解决此问题。据我了解,实际消息的顺序无关紧要,您只需检查实际消息中是否存在所有预期消息?在这种情况下,将每一行保存在一个单独的变量/数组中,然后验证每个预期消息在实际使用中是否存在。 `
for(i=1,i将字符串
s转换为流
s
Arrays.stream(actMsg.split("\n"));
然后以下是我如何解决您的示例:
List<String> expMsg = Arrays.asList("Line1", "Line2", "Line3", "Line4", "Line5", "Line6");
String actMsg = "Line1.\n"
+ "Line3\n"
+ "Line2\n"
+ "Line5\n"
+ "Line4\n" + "\n"
+ "Line6.";
boolean containsAll = expMsg.stream()
//select only elements which are in actMsg
.filter(actMsg::contains) //e -> actMsg.contains(e)
//all strings from expMsg should be present
.count() == expMsg.size();
System.out.println(containsAll);
List expMsg=Arrays.asList(“第1行”、“第2行”、“第3行”、“第4行”、“第5行”、“第6行”);
String actMsg=“Line1。\n”
+“第3行\n”
+“第2行\n”
+“第5行\n”
+“第4行\n”+“\n”
+“第6行。”;
布尔containsAll=expMsg.stream()
//仅选择actMsg中的元素
.filter(actMsg::contains)//e->actMsg.contains(e)
//expMsg中的所有字符串都应存在
.count()==expMsg.size();
系统输出打印项次(containsAll);
拆分expMsg
中的所有行并检查所有行是否都在actMsg我尝试了以下方法,但失败了:public void verify(String expMsg,String actMsg){Stream expected=Arrays.Stream(expMsg.Split(“\n”);Stream actual=Arrays.Stream(actMsg.Split(\n”);assertstreamquals(expected,actual);}静态void AssertStreamQuals(流s1,流s2){Iterator iter1=s1.Iterator(),iter2=s2.Iterator();而(iter1.hasNext()&&iter2.hasNext())assertEquals(iter1.next(),iter2.next());assert!iter1.hasNext()&&&&!iter2.hasNext();}。请建议添加.sorted()
在比较之前,先对两个流执行expMsg
操作,如果expMsg
有更多不同的行怎么办?你想让它返回true吗?@user7294900这是一个好问题。但问题中没有提到这个细节。因此,我开始时的想法是,总是只有这些行,这使得其他检查变得多余。
Arrays.stream(actMsg.split("\n"));
List<String> expMsg = Arrays.asList("Line1", "Line2", "Line3", "Line4", "Line5", "Line6");
String actMsg = "Line1.\n"
+ "Line3\n"
+ "Line2\n"
+ "Line5\n"
+ "Line4\n" + "\n"
+ "Line6.";
boolean containsAll = expMsg.stream()
//select only elements which are in actMsg
.filter(actMsg::contains) //e -> actMsg.contains(e)
//all strings from expMsg should be present
.count() == expMsg.size();
System.out.println(containsAll);