我正在使用Selenium来自动化我们门户的UI。其中一个对话框具有文本。
"Record with UUID:530d79e2-4d9a-4e8e-9114-da1431f0dd52 inserted successfully."
当UUID每次都在变化时,我该如何断言这个文本呢?下面我提到了一个解决办法。
Assert.assertTrue(string.contains("Record with UUID:");
Assert.assertTrue(string.contains("inserted successfully.");
但在我看来这是个糟糕的方法。有什么建议用更干净的方式来做吗?
发布于 2017-10-27 09:10:06
最简单的方法是使用String#matches函数,例如:
Assert.assertTrue(string.matches("Record with UUID:[0-9a-z\\-]+ inserted successfully."));
您可以使用更高级的正则表达式来验证UUID,例如,来自以下答案:UUID的java正则表达式
Assert.assertTrue(
string.matches("Record with UUID:[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12} inserted successfully.")
);
或者另一个:
Assert.assertTrue(
string.matches("Record with UUID:[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12} inserted successfully.")
);
此页面:正则表达式测试页面可用于测试各种输入文本的正则表达式。
https://stackoverflow.com/questions/46978933
复制